0

I´m getting a mapping error on my application, any help would be greatly appreciated! =)

Error message: No mapping found for HTTP request with URI [/springapp/priceincrease.htm] in DispatcherServlet with name 'springapp'

You can find some code below:

PriceIncreaseFormController.java

@Controller
@RequestMapping(value="/priceincrease.htm")
public class PriceIncreaseFormController {

/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());

@Autowired
private ProductManager productManager;

@RequestMapping(method = RequestMethod.POST)
public String onSubmit(@Valid PriceIncrease priceIncrease, BindingResult result)
{
    if (result.hasErrors()) {
        return "priceincrease";
    }

    int increase = priceIncrease.getPercentage();
    logger.info("Increasing prices by " + increase + "%.");

    productManager.increasePrice(increase);

    return "redirect:/hello.htm";
}

@RequestMapping(method = RequestMethod.GET)
protected PriceIncrease formBackingObject(HttpServletRequest request) throws ServletException {
    PriceIncrease priceIncrease = new PriceIncrease();
    priceIncrease.setPercentage(15);
    return priceIncrease;
}

public void setProductManager(ProductManager productManager) {
    this.productManager = productManager;
}

public ProductManager getProductManager() {
    return productManager;
}

}

app-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">


   <bean id="productManager" class="com.companyname.springapp.service.SimpleProductManager">
     <property name="products">
        <list>
            <ref bean="product1"/>
            <ref bean="product2"/>
            <ref bean="product3"/>
        </list>
     </property>
   </bean>


   <bean id="product1" class="com.companyname.springapp.domain.Product">
     <property name="description" value="Lamp"/>
     <property name="price" value="5.75"/>
   </bean>

   <bean id="product2" class="com.companyname.springapp.domain.Product">
     <property name="description" value="Table"/>
     <property name="price" value="75.25"/>
   </bean>

   <bean id="product3" class="com.companyname.springapp.domain.Product">
     <property name="description" value="Chair"/>
     <property name="price" value="22.79"/>
   </bean>


   <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
     <property name="basename" value="messages"/>
   </bean>

   <!-- Scans the classpath of this application for @Components to deploy as beans -->
   <context:component-scan base-package="com.companyname.springapp.web" />

   <!-- Configures the @Controller programming model -->
   <mvc:annotation-driven/>

   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
     <property name="prefix" value="/WEB-INF/views/"></property>
     <property name="suffix" value=".jsp"></property>        
   </bean>

</beans>

web.xml

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <display-name>Springapp</display-name>

  <servlet>
    <servlet-name>springapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/app-config.xml</param-value>
    </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

</web-app>
César
  • 1
  • 2

2 Answers2

0

You forgot to add handler mapping in your application context:

<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
Akash Rajbanshi
  • 1,553
  • 11
  • 23
  • Thanks for your comment @Akash, but I added the handler mapping and it´s still not working =( I have another controller, and it works properly, sincerely I don´t know what´s causing the issue – César May 25 '15 at 08:42
  • so which method does the `priceincrease.htm` url trigger? – Akash Rajbanshi May 25 '15 at 08:45
  • I think "formBackingObject", but I´m not pretty sure, I´m a newbie on Spring and following a tutorial =) – César May 25 '15 at 08:50
  • can you pass me the link for the tutorial? – Akash Rajbanshi May 25 '15 at 08:51
  • Yes but maybe you´ll have a problem because it´s in spanish =( However, here you are http://www.uv.es/grimo/teaching/SpringMVCv4PasoAPaso/index.html – César May 25 '15 at 08:54
  • why dont you try setting the url to the method itself rather than the class? – Akash Rajbanshi May 25 '15 at 09:13
  • I guess you mean mapping each method, rather than the whole class, right? Edit to say it doesn´t work... I wrote this before the first method: @RequestMapping(value="/priceincrease.htm", method = RequestMethod.POST) and this before the second method: @RequestMapping(value="/priceincrease.htm", method = RequestMethod.GET) – César May 25 '15 at 09:18
  • yes..i think you have already that too.. – Akash Rajbanshi May 25 '15 at 09:18
  • Try only for second method `formBackingObject`...remove the `@RequesMapping` for the first method `onSubmit()` – Akash Rajbanshi May 25 '15 at 09:41
  • Tried, doesn´t work... =( Really, it´s quite a strange issue – César May 25 '15 at 09:51
  • Yes.quite strange..Seems like I would not be able to resolve this issue..Good Luck :) – Akash Rajbanshi May 25 '15 at 09:52
0

There problem in your GET method : formBackingObject. Depends upon your requirement it should return to view or make it REST method.

Case 1 : return to view (JSP,HTML,XSLT...)

@RequestMapping(value="/priceincrease.htm",method = RequestMethod.GET)
protected String formBackingObject(HttpServletRequest request,Model model) throws ServletException {
      PriceIncrease priceIncrease = new PriceIncrease();
      priceIncrease.setPercentage(15);
      model.addAttribute("priceIncrease",priceIncrease);
      return "view_name";
}

Case 2 : REST method using @ResponseBody (jackson.core Jar is necessary)

@RequestMapping(value="/priceincrease.htm",method = RequestMethod.GET)
@ResponseBody
protected PriceIncrease formBackingObject(HttpServletRequest request) throws ServletException {
    PriceIncrease priceIncrease = new PriceIncrease();
    priceIncrease.setPercentage(15);
    return priceIncrease;
}

Here you have modify controller's parent mapping (simply remove it) for both cases

@Controller
public class PriceIncreaseFormController {

}
Oomph Fortuity
  • 5,710
  • 10
  • 44
  • 89
  • Thanks for your comment @OomphFortuity. I tried both solutions that you gave, (I copied it literally, obviously changing the "view_name") but none works =( I´m really confused, don´t know what to try more... – César May 25 '15 at 15:16
  • Please check , I Have updated the answer – Oomph Fortuity May 26 '15 at 05:40
  • I tried that before, and doesn´t worked. Now I tried something new: in previous comments I said that I have another controller that works properly, so I copied it to this controller (supposing it would work), but it doesn´t. So strange... – César May 26 '15 at 08:11