看了网上很多关于Spring Boot处理jsp时404错误,很不幸,没有一例解决了我的问题,最终通过传统的启动方式解决了,这里记录一下我的解决方案吧。
问题
先来看看我的pom.xml
配置文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Spring Boot应用热部署,在Idea中还要把Build->Compiler->build project automatically打开
同时打开registry(ctrl + alt + shift + /),开启compiler.automake.allow.when.app.running选项,
测试:修改java源文件时,直接recompiler xxx.java文件,Idea就会restart应用,这个过程很快,与第一次启动不一样
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后创建Controller
、启动类,进入启动类后,鼠标右键运行,命令行显示一切正常,打开index
页面如下:
截取了一段spring日志如下:
2019-06-24 15:34:29.761 DEBUG 11864 --- [nio-8081-exec-2] o.s.w.servlet.view.BeanNameViewResolver : No matching bean found for view name 'index'
2019-06-24 15:34:29.763 DEBUG 11864 --- [nio-8081-exec-2] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.JstlView: name 'index'; URL [/WEB-INF/views/index.jsp]] based on requested media type 'text/html'
2019-06-24 15:34:29.763 DEBUG 11864 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.JstlView: name 'index'; URL [/WEB-INF/views/index.jsp]] in DispatcherServlet with name 'dispatcherServlet'
2019-06-24 15:34:29.765 DEBUG 11864 --- [nio-8081-exec-2] o.s.web.servlet.view.JstlView : Forwarding to resource [/WEB-INF/views/index.jsp] in InternalResourceView 'index'
2019-06-24 15:34:29.767 DEBUG 11864 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/rw/WEB-INF/views/index.jsp]
2019-06-24 15:34:29.767 DEBUG 11864 --- [nio-8081-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /WEB-INF/views/index.jsp
2019-06-24 15:34:29.771 DEBUG 11864 --- [nio-8081-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/WEB-INF/views/index.jsp]
2019-06-24 15:34:29.771 DEBUG 11864 --- [nio-8081-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/WEB-INF/views/index.jsp] are [/**]
2019-06-24 15:34:29.772 DEBUG 11864 --- [nio-8081-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/WEB-INF/views/index.jsp] are {}
2019-06-24 15:34:29.772 DEBUG 11864 --- [nio-8081-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/WEB-INF/views/index.jsp] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@68f5c0ef]]] and 1 interceptor
2019-06-24 15:34:29.772 DEBUG 11864 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/rw/WEB-INF/views/index.jsp] is: -1
2019-06-24 15:34:29.772 DEBUG 11864 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
跳转jsp页面会出404异常,@ResponseBody
注释接口正常
这里我就不讨论网上很多其它人的解决方法了,这里尝试过如下步骤:
- 添加
tomcat-embed-jasper
依赖 - 将
tomcat-embed-jasper
依赖scope设置为default - webapp目录要放在
src/main
目录下 pom.xml
文件packing要为war
上述步骤均不能解决问题,唯一能勉强运行的方式就是添加spring-boot-maven-plugin
,然后mvn spring-boot:run
运行项目。为什么强调勉强运行呢?因为通过maven启动,我没办法调试代码,不要跟我说在spring-boot-maven-plugin
中将-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:53080,suspend=y,server=n
增加到jvmArguments
中,尝试过,失败!
解决办法
既然右键运行页面找不到,我们还无从获取到jsp页面编译后的位置,那就用传统artifact
来开发吧。
- 首先在当前module中添加web支持,将web资源目录指向webapp下
- 创建
artifact
run/debug configuration
->add local tomcat
-> 添加刚才创建的artifact
至此,问题解决完毕,可能比起右键运行麻烦一些,但能解决既要debug又要页面不报404错误还不能不用Idea问题才是关键,希望对大家有一点帮助。