Whitelabel错误页面Spring启动

时间:2018-01-29 13:30:57

标签: java jsp spring-boot

我最近迁移到Spring boot。我之前使用过Spring MVC。当我在应用程序启动后访问某个站点时,会抛出404页面找不到。控制器处理请求,但由于某种原因找不到jsp页面。

我的Application.java

package com.myapp.webapp;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "com.myapp")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

application.properties

# Spring MVC configuration
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

spring.datasource.url=jdbc:mysql://us-cdbr-iron-east-05.cleardb.net/heroku_4663e71bc0d567a?reconnect=true
spring.datasource.username=bb1a6d3ce29ada
spring.datasource.password=******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#spring.datasource.url=jdbc:mysql://localhost:3306/socialnet?autoReconnect=true&useSSL=false
#spring.datasource.username=root
#spring.datasource.password=123

spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none

debug=true

例如controller

package com.myapp.webapp;

//imports

@Controller
@SessionAttributes({"accountInSession", "base64Photo"})
public class MainController {

    private static final Logger logger = LoggerFactory.getLogger(MainController.class);

    @Autowired
    private AccountService accountService;

    @Autowired
    private AuthenticationTrustResolver authenticationTrustResolver;


    @RequestMapping(value = {"/login", "/"})
    public ModelAndView loginPage(@RequestParam(value = "error", required = false) String error,
                                  @RequestParam(value = "logout", required = false) String logout,
                                  @RequestParam(value = "noLogin", required = false) String noLogin) {

        ModelAndView modelAndView = new ModelAndView("/login");
        if (error != null) {
            modelAndView.addObject("error", "Incorrect mail and/or password");
            //modelAndView.setViewName("login");

        }
        if (logout != null) {
            modelAndView.addObject("msg", "You've been logged out successfully!");
            //modelAndView.setViewName("login");

        }
        if (noLogin != null) {
            modelAndView.addObject("error", "Please log in to view this page");
        }
        if (!isCurrentAuthenticationAnonymous()) {
            //modelAndView.setViewName("login");
            modelAndView.setViewName("redirect:/account");
            return modelAndView;
        }
        return modelAndView;
    }




    @RequestMapping("/test")
    public String test() {
        return "redirect:/registration";
    }
}

例如,如果我转到http://localhost:8080/test controller,请将我重定向到http://localhost:8080/registration

我使用了模型的多模块maven项目:common(适用于模型),daoservicewebapp

所以控制器位于 java/com/myapp/webapp/controllers

位于

的Jsp页面

src/main/webapp/WEB-INF/jsp

文件路径: java/com/myapp/Application.java

src/main/resources/application.properties

wepapp模块的结构 poject

2 个答案:

答案 0 :(得分:0)

您需要使用WebMvcConfigurer提供视图解析程序的实现。

WebMvcConfigurer已替换旧的,已弃用的WebMvcConfigurerAdapter

@EnableWebMvc
@Configuration
@ComponentScan
public class CustomWebConfig implements WebMvcConfigurer {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/jsp/", ".jsp");
    }
 } 

您需要提供前缀( / WEB-INF / jsp / )和后缀( .jsp )显式为registry.jsp()方法将默认前缀设为 / WEB-INF / ,后缀为 .jsp

参考Spring Framework: ViewResolverRegistry class

答案 1 :(得分:0)

我已经解决了这个问题。我从src/main/wepapp/WEB-INF/jsp/移动所有jsp文件  到src/main/resources/META-INF/resources/WEB-INF/jsp/。我不知道它之前为什么不起作用,但现在它起作用了