2013-06-03 70 views
1

嗨我已經創建了一個Spring MVC應用程序,我有兩種方法稱爲估計控制器。春天MVC寧靜的路徑

我可以去這個網址www.wesite.com/xyz/estimation

訪問該控制器的家庭法但我嘗試去這個網址訪問homeById方法www.wesite.com/xyz/estimation/1 我收到404錯誤,請求的資源不可用。

可以任何身體請在它上面灑一些光。

@Controller 
@RequestMapping("/estimation") 
public class EstimationController { 

@RequestMapping("") 
public ModelAndView home(HttpServletRequest request, HttpServletResponse response) { 

    ModelAndView mv = new ModelAndView("productEstimate"); 
    return mv; 

    } 

    @RequestMapping(value="/{productId}", method = RequestMethod.GET) 
    public ModelAndView homeById(HttpServletRequest request, HttpServletResponse response,@PathVariable int productId) { 

    ModelAndView mv = new ModelAndView("productEstimate"); 

    return mv; 
} 

的web.xml

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     WEB-INF/classes/spring/applicationContext.xml, 
     WEB-INF/classes/spring/hibernateContext.xml 
    </param-value> </context-param> 

<servlet> 
    <servlet-name>projectName</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value></param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 


<servlet-mapping> 
    <servlet-name>projectName</servlet-name> 
    <url-pattern>/estimation/*</url-pattern> 
</servlet-mapping> 

的applicationContext.xml

<mvc:annotation-driven /> 

<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 
     <property name="resourceLoaderPath" value="/views/" /> 
</bean> 

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> 
     <property name="cache" value="true"/> 
     <property name="prefix" value=""/> 
     <property name="layoutUrl" value="layout.vm"/> 
     <property name="suffix" value=".vm"/> 
</bean> 
+1

你可以得到兩個如果映射不匹配的錯誤或者如果視圖無法呈現(但是我發現您對這兩個請求都使用相同的視圖,所以如果第一個視圖正常工作,第二個視圖也應該如此)。看看你是否有錯誤,例如:警告:找不到使用URI的HTTP請求映射 – namero999

回答

2

當提到路徑變量使用這樣的事情

@PathVariable("productId") int productId 

這個封閉的變量名是一樣的,在請求映射變量名

@RequestMapping(value="/{productId}", method = RequestMethod.GET)