IntelliJ Idea运行Spring Boot项目报404错误

看了网上很多关于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来开发吧。

  1. 首先在当前module中添加web支持,将web资源目录指向webapp下
  2. 创建artifact
  3. run/debug configuration -> add local tomcat -> 添加刚才创建的artifact

至此,问题解决完毕,可能比起右键运行麻烦一些,但能解决既要debug又要页面不报404错误还不能不用Idea问题才是关键,希望对大家有一点帮助。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值