您好我已经创建了一个Spring MVC应用程序,我有一个名为Estimation的Controller,有两种方法。
我可以通过访问此网址 www.wesite.com/xyz/estimation
来访问此控制器的主要方法但我尝试访问此
@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>
答案 0 :(得分:2)
在引用路径变量时,请使用类似
的内容 @PathVariable("productId") int productId
此随附的变量名称与请求映射
中的变量名称相同 @RequestMapping(value="/{productId}", method = RequestMethod.GET)