1

Hi I have created a Spring MVC Application, I have Controller called Estimation with two methods.

I can access the home method of this controller by going to this url www.wesite.com/xyz/estimation

but I try to access homeById method by going to this url www.wesite.com/xyz/estimation/1 I get a 404 error, The requested resource is not available.

can any body please shed some light on it.

@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
  • 1
    You can get that error both if the mapping is not matched or if the view cannot be rendered (but I see you are using the same view for both requests, so if the first works, so should the second). See if you have an error like WARNING: No mapping found for HTTP request with URI Commented Jun 3, 2013 at 16:41

1 Answer 1

2

When referring the path variable use something like this

 @PathVariable("productId") int productId

This enclosed variable name is same as the variable name in the request mapping

 @RequestMapping(value="/{productId}", method = RequestMethod.GET)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.