Spring MVC宁静之路

时间:2013-06-03 13:52:20

标签: spring rest model-view-controller

您好我已经创建了一个Spring MVC应用程序,我有一个名为Estimation的Controller,有两种方法。

我可以通过访问此网址 www.wesite.com/xyz/estimation

来访问此控制器的主要方法

但我尝试访问此 www.wesite.com/xyz/estimation/1 homeById 方法 我收到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 个答案:

答案 0 :(得分:2)

在引用路径变量时,请使用类似

的内容
 @PathVariable("productId") int productId

此随附的变量名称与请求映射

中的变量名称相同
 @RequestMapping(value="/{productId}", method = RequestMethod.GET)