如何为JSP配置spring boot mvc app?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何为JSP配置spring boot mvc app?相关的知识,希望对你有一定的参考价值。
我是Spring boot(和servlet 3.0)的新手。我试图用JSP作为视图创建spring mvc项目。当我从我的控制器返回一个视图时,它没有被解析为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";
}
}
在srcmainwebappWEB-INFjsp
中创建了两个.jsp文件。
谷歌搜索后我发现我需要在application.properties中指定这个,所以我在props中添加了以下内容:
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
logging.level.org.springframework: TRACE
logging.level.com: 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失败了。
我需要遵循哪些其他步骤?是否有任何关于Spring Boot-mvc和jsp的教程?
附:我可以使用web.xml使用jsp创建spring mvc app(在我的配置文件中使用JstlView)。但是我找不到任何有关使用jsp的Spring boot mvc的教程。
@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 /
+
<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>
假设它是嵌入式Tomcat,
你需要在你的pom.xml
中有以下内容
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
嵌入式Tomcat核心包没有JSP渲染支持。
您不需要ViewResolver。 pom.xml需要Yura讲述的上述依赖项,并将jsp文件放在src main webapp WEB-INF jsp中。
- 在src / main下创建webapp / WEB-INF / views {将最后一个文件夹命名为U Like}
- 添加jstl jar
- 在application.properties中添加以下两行 spring.mvc.view.prefix:/ WEB-INF / views / spring.mvc.view.suffix:.jsp Run As Spring Boot App ..U很高兴! For More你可以在github上咨询我的这个项目:https://github.com/SudipBiswasRana/Using-JSP-As-View-For-Spring-Project
以上是关于如何为JSP配置spring boot mvc app?的主要内容,如果未能解决你的问题,请参考以下文章
如何为 Spring Boot RESTful Web 服务配置多级身份验证?
gradle + spring MVC + spring boot + jsp = 404错误?
如何使用 Spring MVC & Security、Jasig CAS 和 JSP 视图在 Spring Boot 2 中配置 UTF-8?