1

嗨,我已经创建了一个 Spring MVC 应用程序,我有一个名为 Estimation 的控制器,有两种方法。

我可以通过访问这个网址www.wesite.com/xyz/estimation来访问这个控制器的home 方法

但我尝试通过访问此网址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>

应用程序上下文.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>
4

1 回答 1

2

当引用路径变量时,使用类似这样的东西

 @PathVariable("productId") int productId

这个包含的变量名与请求映射中的变量名相同

 @RequestMapping(value="/{productId}", method = RequestMethod.GET)
于 2013-06-03T19:32:36.693 回答