2012-11-30 126 views
3

我有一個spring應用程序,我無法解決問題。當我使用PathVariable時,我的RequestMapping中的值正在添加到URI中,並且DispatchHandler變得混亂。使用PathVariable的Spring RequestMapping導致404

例如,當我要求/Dashboard/project/1131/

我得到HTTP Status 404 - /Dashboard/project/1131/WEB-INF/jsp/projectDetail.jsp

項目/ 1131被添加到因某些原因

路徑。當我要求/Dashboard/projects春天發現我的.jsp,並提出它。/project/{projectId}不起作用 - 我得到上述行爲。

這裏是我的控制器

@Controller 
public class ProjectController { 

protected final Log logger = LogFactory.getLog(getClass()); 

@RequestMapping(value = "projects", method=RequestMethod.GET) 
public String projects() { 
    return "/projects"; 
} 

@RequestMapping(value="/project/{projectId}", method=RequestMethod.GET) 
public String project(@PathVariable("projectId") String projectId, ModelMap map) { 
    map.put("projectId", projectId); 
    logger.info("Project Id: " + projectId); 
    return "/projectDetail"; 
} 

} 

我的配置

的web.xml

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

<!-- Webapp name --> 
<display-name>Dashboard</display-name> 

<!-- default file name --> 
<welcome-file-list> 
    <welcome-file>home</welcome-file> 
</welcome-file-list> 

<!-- spring listener (all spring jars need to be in WEB-INF/lib --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- set servlet to DispatcherService --> 
<servlet> 
    <servlet-name>dashboard</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<!-- hand all requests to dispatcher service --> 
<servlet-mapping> 
    <servlet-name>dashboard</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

</web-app> 

的AppConfig

@Configuration 
public class AppConfig { 

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

} 

儀表板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:mvc="http://www.springframework.org/schema/mvc" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

<!-- the packages to scan for annotations --> 
<context:component-scan base-package="com.dennisstevens.dashboard.controller" /> 
<context:component-scan base-package="com.dennisstevens.dashboard" /> 
<context:component-scan base-package="com.dennisstevens.dashboard.domain" /> 

<!-- Tell Spring that MVC components are @Annotation Driven --> 
<mvc:annotation-driven /> 

<!-- Tell Spring Dispatch Handler to look in WebContent/resources/ for resources --> 
<mvc:resources mapping="/resources/**" location="/resources/" /> 

<!-- Tell Spring to scan for config annotations --> 
<context:annotation-config/> 

<!-- TODO: Figure out how to load this in AppConfig --> 
<bean id="messageSource" 
    class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="messages" /> 
</bean> 

</beans> 

這一定很簡單 - 但我查看了文檔並在互聯網上搜索了幾個小時。看起來我正在從我所能看到的一切做起。

+0

無關,但爲什麼在3.0應用程序中使用2.5模式? –

+0

謝謝。我從一個2.5的例子開始,並沒有清理那個引用。我會清理它。 –

回答

2

我認爲在resolver.setPrefix("/WEB-INF/jsp/");的前導斜槓應該爲您解決它。

+0

這很有道理。我做了改變,它工作。謝謝。 –

+0

除現在我還沒有找到資源。在DispatcherServlet中使用URI [/儀表板/項目/資源/js/jquery/jquery-1.8.3.min.js]的名稱爲「儀表板」的HTTP請求沒有找到映射。在這種情況下,我確實有資源前面的斜槓。 –

+0

我也修正了這個問題。我必須使用c:url來創建絕對路徑。