2014-06-30 54 views
2

我有以下項目結構春季啓動4層不渲染的JSP拋出404

-src 
    -main 
    -java 
     -com 
     -test 
      Application.java 
      -controllers 
      MyController.java 
    -webapp 
     -WEB-INF 
     -jsp 
      main.jsp 

我想要做類似this的東西,但在我的控制,我有以下

@Controller 
@RequestMapping("/my/**") 
public class MyController { 
    @RequestMapping("/home") 
    public String loadHomePage(Model m) { 
     m.addAttribute("name", "CodeTutr"); 
     System.out.println("Test the view controller"); 
     return "main"; 
    } 
} 

,當我去http://localhost:8080/my/home我得到了日誌消息和一個404。我在春季4想到我不需要視圖解析器,但如果我這樣做,我該如何配置它。

更新

我創建了一個application.properties有以下...

spring.view.prefix: /WEB-INF/jsp/ 
spring.view.suffix: .jsp 
application.message: Hello Phil 

但它仍然沒有工作。

更新2

所提供的樣品春天似乎也與以下的build.gradle以同樣的方式失敗....

buildscript { 
    repositories { 
    mavenLocal() 
    mavenCentral() 
    maven { url "http://repo.spring.io/snapshot" } 
    maven { url "http://repo.spring.io/milestone" } 
    } 
    dependencies { 
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.4.BUILD-SNAPSHOT") 
    } 
} 
apply plugin: 'war' 
apply plugin: 'spring-boot' 
apply plugin: 'groovy' 
war { baseName='itext' } 
repositories { 
    mavenLocal() 
    mavenCentral() 
    maven { url "http://repo.spring.io/snapshot" } 
    maven { url "http://repo.spring.io/milestone" } 
    maven { url "http://repo.spring.io/libs-release" } 
} 
dependencies { 
    compile("org.springframework.boot:spring-boot-starter-web") 
    compile("org.springframework.boot:spring-boot-starter-websocket") 
    compile("org.springframework:spring-messaging") 
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat") 
    testCompile("org.springframework.boot:spring-boot-starter-test") 
    compile 'org.codehaus.groovy:groovy-all:2.2.0' 
    compile 'com.lowagie:itext:4.2.1' 
    compile 'com.google.code.gson:gson:2.2.4' 
} 

更新

我也試圖明確...

@Bean 
public ViewResolver getViewResolver(){ 
    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
    resolver.setPrefix("/WEB-INF/jsp/"); 
    resolver.setSuffix(".jsp"); 
    return resolver; 
} 

這似乎也不起作用。

回答

1

添加此到的build.gradle似乎解決這個問題...

compile 'com.google.code.gson:gson:2.2.4' 
compile 'javax.servlet.jsp.jstl:jstl:1.2', 
     'taglibs:standard:1.1.2' 
providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper:8.0.8' 
-3

您的彈簧配置中是否定義了InternalViewResolver?

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
    <property name="prefix"> 
     <value>/WEB-INF/jsp/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 
+0

這是不需要在春季4看到更新後的帖子與鏈接。每個原始問題也不使用XML。 – Jackie

+0

不在Spring 4 w/boot中查看提供的鏈接和示例 – Jackie

+0

實際上,它在我的build.gradle中一定是錯誤的,所以我會補充一點。春天,XML仍然是一種非常古老且不贊成使用的映射bean的方式。帶有註解與XML的更新版本將反轉我的投票。 – Jackie

1

我得到了同樣的問題,並增加這一塊解決了pom.xml中:

<dependency> 
     <groupId>org.apache.tomcat.embed</groupId> 
     <artifactId>tomcat-embed-jasper</artifactId> 
     <scope>provided</scope> 
    </dependency> 

我不明白爲什麼...

0

在Github上創建了一個相同的和共享的模板 https://github.com/nagasrinu88/spring-boot-template

<dependency> 
    <groupId>org.apache.tomcat.embed</groupId> 
    <artifactId>tomcat-embed-jasper</artifactId>    
</dependency> 

得到了同樣的問題,並通過添加使用彈簧啓動1.4.0.RELEASE

1

當您添加以下的依賴上面的紋路解決,你可能沒有任何錯誤。它爲我工作。

據我所知,如果不添加一些依賴關係,jsp頁面不會與嵌入式tomcat一起呈現。

<dependencies> 
    <dependency> 
     <groupid>org.springframework.boot</groupid> 
     <artifactid>spring-boot-starter-web</artifactid> 
    </dependency> 
    <dependency> 
     <groupid>org.apache.tomcat.embed</groupid> 
     <artifactid>tomcat-embed-jasper</artifactid> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupid>javax.servlet</groupid> 
     <artifactid>jstl</artifactid> 
    </dependency> 
    </dependencies>