2013-10-23 43 views
1

我的控制器的方法中有幾個SYSOUT,它們出現在控制檯日誌中...爲我驗證所有@RequestMapping的行爲如預期。環境bean的@Autowiring也在工作(也可以通過SYSOUT正確顯示)。Spring MVC:控制器的RequestMapping工作,但返回總是給出404

但是,方法的返回(我使用返回字符串類型的方法)只會導致404s。未找到* .jsps。項目正在使用Maven; IDE是eclipse kepler,FWIW。

My ViewResolver是沼氣標準。看不到斷開連接。

我的web.xml:

 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>BluPrint</display-name>

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<context-param>
<param-name>groupId</param-name>
<param-value>${project.groupId}</param-value>
</context-param>

<context-param>
<param-name>artifactId</param-name>
<param-value>${project.artifactId}</param-value>
</context-param>

</web-app>

我servlet.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xjp="http://www.corpabc.com/schema/xjp"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.corpabc.com/schema/xjp http://www.corpabc.com/schema/xjp/beans.xsd">

<context:component-scan base-package="com.corpabc.bluprint" />

<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />

<import resource="classpath:corpabc/xjp/configuration/properties.xml" />

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

<bean id="dataSourceDB2" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/BluPrint" />
<property name="resourceRef" value="true" />
</bean>

<xjp:environment artifactId="${artifactId}" groupId="${groupId}" />

</beans>

我的控制器:

package com.corpabc.bluprint.controllers; 
import java.util.Map; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 
import corpabc.xjp.configuration.env.Environment; 

/** 
* 
* Handles requests for the application. 
*/ 

@Controller 
@RequestMapping("/*") 
public class BluPrintController { 

    @Autowired 
    private Environment xjpEnvironment; 

    @RequestMapping("/init") 
    protected String catchInit(Map<String, Object> model) { 
     System.out.println("Got into init method. XJP Environment: "+xjpEnvironment); 
     model.put("xjp", this.xjpEnvironment); 
     return "envtest"; 
    } 

    @RequestMapping("/*") 
    protected String catchAllOthers(Map<String, Object> model) { 
     System.out.println("Got into catch-all method: "); 
     return "defaultPage"; 
    } 
} 

envtest.jsp/WEB-INF/jsp/下......但進入網址~localhost:8080/bluprint/init ......我在這裏得到了404

defaultPage.jsp不存在......我希望一個沒有找到狀態,這就是我得到的,當我進入~localhost:8080/bluprint/ 。不知道它是否應該是404,具體來說,但這就是我所得到的,無論如何。

+0

你如何運行你的應用程序?如果使用WAR文件,您可以查看它並驗證文件是否存在? – Admit

回答

10

我懷疑你的問題在你的servlet映射中。/*將通過調度程序servlet強制所有內容,包括jsps。嘗試失去*。我會找到在Servlet規範和更新相關的部分....

Servlet規範:

12.2映射 在Web應用程序部署描述符的規範,以下語法用於定義映射: 以'/'字符開頭並以'/ *'後綴結尾的字符串用於路徑映射。

  • 用「*」。前綴開始的字符串被用作擴展映射。

  • 空字符串(「」)是一種特殊的URL模式,它準確地映射到 應用程序的上下文根,即表單的請求。在這種情況下,路徑信息是'/',servlet路徑和上下文路徑是空字符串(「」)。

  • 只包含'/'字符的字符串表示應用程序的「默認」servlet。在這種情況下,servlet路徑是請求URI減去上下文路徑,路徑信息爲空。

  • 所有其他字符串僅用於精確匹配。

所以,如果你指定/ *,它覆蓋*的.jsp映射,所以jsp請求被路由返回到您的分發程序Servlet,而不是擊中的jsp。

+0

**正確的錢。**謝謝! –

相關問題