如何为JSP配置spring

如何为JSP配置spring

本文介绍了如何为JSP配置spring boot mvc app?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring boot(和servlet 3.0)的新手。我试图用JSP作为视图创建spring mvc项目。当我从我的控制器返回一个视图时,它没有被解析为JstlView。

I am new to Spring boot ( and servlet 3.0 ). I am trying to create spring mvc project with JSP as view. When I return a view from my controller it is not getting resolved as JstlView.

这是我做的:

@SpringBootApplication
public class MyApp extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }

}







@Controller
public class MainController {

    @RequestMapping( value="/main" , method = RequestMethod.GET  )
    public String main(){
        return "main";
    }

    @RequestMapping( value="/" , method = RequestMethod.GET  )
    public String welcome(){
        return "welcome";
    }
}






src\main\webapp\WEB-INF \ jsp 中创建了两个.jsp文件。


Created both .jsp files in src\main\webapp\WEB-INF\jsp .

谷歌搜索后我发现我需要在application.properties中指定这个,所以我在道具中添加了以下内容:

After googling I found that I need to specify this in application.properties So I added following in props:

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp


logging.level.org.springframework: TRACE
logging.level.com: TRACE

即使在此之后它也无效。这是跟踪。

Even after this it is not working. Here is the trace.

2016-04-24 19:54:49.016 TRACE 7880 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Invoking [MainController.welcome] method with arguments []
2016-04-24 19:54:49.016 TRACE 7880 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Method [welcome] returned [welcome]
2016-04-24 19:54:49.020 DEBUG 7880 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2016-04-24 19:54:49.020 DEBUG 7880 --- [nio-8080-exec-1] o.s.w.servlet.view.BeanNameViewResolver  : No matching bean found for view name 'welcome'
2016-04-24 19:54:49.022 DEBUG 7880 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory     : Invoking afterPropertiesSet() on bean with name 'welcome'
2016-04-24 19:54:49.022 TRACE 7880 --- [nio-8080-exec-1] o.s.w.s.v.InternalResourceViewResolver   : Cached view [welcome]
2016-04-24 19:54:49.022 DEBUG 7880 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.InternalResourceView: name 'welcome'; URL [/WEB-INF/jsp/welcome.jsp]] based on requested media type 'text/html'
2016-04-24 19:54:49.022 DEBUG 7880 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'welcome'; URL [/WEB-INF/jsp/welcome.jsp]] in DispatcherServlet with name 'dispatcherServlet'
2016-04-24 19:54:49.022 TRACE 7880 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : Rendering view with name 'welcome' with model {} and static attributes {}
2016-04-24 19:54:49.026 DEBUG 7880 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : Forwarding to resource [/WEB-INF/jsp/welcome.jsp] in InternalResourceView 'welcome'
2

正如您在跟踪中看到的,这是尝试将/jsp/welcome.jsp解析为InternalResourceView而不是JstlView。最终它以404失败。

As you see in the trace, this is trying to resolve /jsp/welcome.jsp as InternalResourceView instead of JstlView. Eventually it fails as 404.

我需要遵循哪些其他步骤? SpringBoot-mvc是否有任何jsp教程?

What other steps I need to follow? Is there any tutorial for SpringBoot-mvc with jsp ?

P.S。我可以使用web.xml使用jsp创建spring mvc app(在我的配置文件中使用JstlView)。但我找不到任何有关jsp的Spring启动mvc的教程。

P.S. I can create spring mvc app with jsp using web.xml ( using JstlView in my config file ). But I can't find any tutorial for Spring boot mvc with jsp.

推荐答案

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);
    return resolver;
}

也需要,你的网页应该是/ webapp / WEB-INF / jsp /

also needed and your pages should be at /webapp/WEB-INF/jsp/

+

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

这篇关于如何为JSP配置spring boot mvc app?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:03