以下pom.file
的应用程序使用Spring Boot& Spring MVC&休息模板。如果我部署应用程序(或者从Spring启动Application.java类开始)并导航到http://localhost:8080/app-name/user/{userID}
,那么我会得到以下异常:
HTTP状态404 - /poc-app/user/WEB-INF/views/user.jsp
正如您所看到的,它正在使用“user”附加视图。以下是代码,配置和pom.xml
文件:
@Controller
public class UserController {
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String listAllUsers(Model model) {
model.addAttribute("users", getUsers());
return "user-list";
}
@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
public String showUserDetail(@PathVariable("userId") int userId, Model model) {
//not checking the list with id, this is just for test
UserBean bean1 = new UserBean();
bean1.setBinNumber("123456");
bean1.setFullName("User One");
bean1.setGuid("abcdef");
bean1.setShortName("User1");
model.addAttribute("user",bean1);
return "user";
}
private List<UserBean> getUsers(){
List<UserBean> list = new ArrayList<UserBean>();
UserBean bean1 = new UserBean();
bean1.setBinNumber("123456");
bean1.setFullName("User One");
bean1.setGuid("abcdef");
bean1.setShortName("User1");
UserBean bean2 = new UserBean();
bean2.setBinNumber("987654");
bean2.setFullName("User Two");
bean2.setGuid("xyzabc");
bean2.setShortName("User2");
UserBean bean3 = new UserBean();
bean3.setBinNumber("555555");
bean3.setFullName("User Three");
bean3.setGuid("hghghg");
bean3.setShortName("User3");
list.add(bean1);
list.add(bean2);
list.add(bean3);
return list;
}
}
application.properties文件
server.port: 8080
management.port: 8001
management.address: 127.0.0.1
spring.view.prefix: WEB-INF/views/
spring.view.suffix: .jsp
Spring Boot Application.java
@ComponentScan("com.company.project")
@Configuration
@SpringBootApplication
public class ProjectAdminWebPocApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(VoucherAdminWebPocApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(VoucherAdminWebPocApplication.class);
}
@Bean
public ErrorPageFilter errorPageFilter() {
return new ErrorPageFilter();
}
@Bean
public FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(filter);
filterRegistrationBean.setEnabled(false);
return filterRegistrationBean;
}
}
的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.project</groupId>
<artifactId>poc-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>poc-app</name>
<description>Proof of concept project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
注意:如果我导航到http://localhost:8080/app-name/user没有问题 - 这完全正常
答案 0 :(得分:2)
spring.view.prefix: WEB-INF/views/
您的前缀配置为相对URL而不是绝对URL。因此,它将被添加到传入请求中。要解决此问题,请通过在其开头添加/
来使URL绝对。
spring.view.prefix: /WEB-INF/views/