2016-04-15 126 views
0

以下具有pom.file的應用程序使用Spring Boot & Spring MVC & Rest模板。如果我部署的應用程序(或同春啓動Application.java類開始),然後導航到http://localhost:8080/app-name/user/{userID}然後我得到這個異常:Spring MVC返回視圖url的構建不正確

HTTP狀態404 -/POC-應用/用戶/ WEB-INF /視圖/用戶.jsp

正如您所看到的,它將追加視圖「用戶」。下面是代碼,配置和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 

春季啓動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 - 這工作完全正常

+1

你的觀點的前綴應該是'/ WEB-INF /視圖/'...請注意領導'/'... –

+0

你好@ M.Deinum非常感謝。它幫助!這再次證明,簡單的事情可能會導致大問題:)順便說一句,如果你可以添加此作爲答案,我可以愉快地標記爲接受答案。 – huzeyfe

回答

2
spring.view.prefix: WEB-INF/views/ 

您已經配置的前綴作爲相對URL而不是絕對URL。因此它將被添加到傳入的請求中。要解決這個問題,可以在網址的開頭添加一個/,以確保網址的絕對性。

spring.view.prefix: /WEB-INF/views/