360SDN.COM

SpringBoot集成jsp,访问时页面报Whitelabel Error Page解决办法【亲测可用】

来源:  2018-07-27 17:32:51    评论:0点击:


一样的项目,我在eclipse上运行没问题,在idea上报错
然后同事用idea写的一个springboot整合jsp的项目,运行这没问题,然后我导入后...
访问页面显示Whitelabel Error Page //type=Not Found, status=404
绝望...弄了快一天了 网上的方法照着做下来都不行,最近公司项目要用springboot和idea 说对maven支持好...刚开始练习,问题接踵而来...让我绝望
对了 ,使用restController返回json是不显示错误页面的,



目录结构有问题:
1:jsp页面应该放置在/src/main/resources/META-INF/resources/WEB-INF/JSP下面,其中/WEB-INF/JSP和配置文件application.properties中配置的MVC前缀一致
2:在返回页面的时候controller不能用restController 而是controller,spring-boot 中  @restController ==  @Controller + @ResponseBody




3:class文件包目录结构调整一下

检查地址没问题,核对jar包也正确。google,有说少jar包,按照回答添加以后也无济于事。折腾了一会找到了原因

竟然时目录文件结构问题,最后附上官网说明http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-structuring-your-code

    14.2 Locating the main application class
    We generally recommend that you locate your main application class in a root package above other classes. The @EnableAutoConfiguration annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items. For example, if you are writing a JPA application, the package of the @EnableAutoConfiguration annotated class will be used to search for @Entity items.

    Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute. You can also use the @SpringBootApplication annotation if your main class is in the root package.

    Here is a typical layout:

    com
    +- example
    +- myproject
    +- Application.java
    |
    +- domain
    | +- Customer.java
    | +- CustomerRepository.java
    |
    +- service
    | +- CustomerService.java
    |
    +- web
    +- CustomerController.java
4:另外资源文件的classpath要在pom.xml中指明一下


5:内嵌Tomcat启动要注释掉
 
<dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <!--<scope>provided</scope>-->
        </dependency>


附上controller类:

package com.navinfo.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
@RequestMapping("/page")
public class PageController {

    @RequestMapping("/a")
    public String b(Map<String, Object> model){
        model.put("msg", "张三");     
        return "regiester";
    }

    @RequestMapping("/b")
    public ModelAndView b(HttpServletRequest request){
        ModelAndView view = new ModelAndView();
        view.setViewName("regiester");

        request.setAttribute("msg", "Davie");
        return view;
    }
}
为您推荐

友情链接 |九搜汽车网 |手机ok生活信息网|ok生活信息网|ok微生活
 Powered by www.360SDN.COM   京ICP备11022651号-4 © 2012-2016 版权