[英]Whitelabel Error Page 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
(用于模型), dao
, service
和webapp
。
因此,位于java/com/myapp/webapp/controllers
位于的Jsp页面
src/main/webapp/WEB-INF/jsp
以及文件路径: java/com/myapp/Application.java
src/main/resources/application.properties
您需要使用WebMvcConfigurer
提供视图解析器的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 。
我已经解决了这个问题。 我将所有jsp文件从src/main/wepapp/WEB-INF/jsp/
移至src/main/resources/META-INF/resources/WEB-INF/jsp/
。 我不知道为什么以前不起作用,但是现在可以起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.