0

I am trying to follow the Spring tutorial, but keep receiving a 404 error. Below is my Controller class:

@Controller
@RequestMapping("/updatedescription.htm")
public class UpdateDescriptionController {
    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    @Autowired
    private ProductManager productManager;

    @RequestMapping(value="/{description}", method=RequestMethod.GET) 
    public String updateDescription(@PathVariable("description") String description, Model model) {       
        return "redirect:/hello.htm";
    }

    @Autowired
    public UpdateDescriptionController(ProductManager productManager) {
        this.productManager = productManager;
    }
}

When I try and go to "http://localhost:8505/baseSpringMVC/updatedescription.htm/Chair" I get a 404 error.

I've setup my web.xml file to point all *.htm traffic to the DispatcherServlet...

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:javaee="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5">   
  <servlet>
    <servlet-name>baseSpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>baseSpringMVC</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

baseSpringMVC-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:oxm="http://www.springframework.org/schema/oxm"
        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/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">

    <!-- the application context definition for the springapp DispatcherServlet -->

    <context:component-scan base-package="springapp.controllers"/>

    <!-- Load dummy data -->
    <bean id="productManager" class="springapp.model.SimpleProductManager">
        <property name="products">
            <list>
                <ref bean="product1"/>
                <ref bean="product2"/>
                <ref bean="product3"/>
            </list>
        </property>
    </bean>

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

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

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

    <!--  pull in messages from <classpath>/messages.properties -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"/>
    </bean>

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

</beans>

Any help would be great.

Thank you!

tereško
  • 58,060
  • 25
  • 98
  • 150
Dan
  • 979
  • 1
  • 8
  • 29

4 Answers4

1

It's not *.htm when you add /Chair onto the end of it. Post your web.xml so we can debug it.

nsayer
  • 16,925
  • 3
  • 33
  • 51
1

Your class-level request mapping should not have ".htm". That is, change:

@RequestMapping("/updatedescription.htm") 

to

@RequestMapping("/updatedescription") 

If that doesn't fix it, we may need to see your Spring configuration file.

EDIT:

The top of your Spring config file, including the MVC namespace, should look something like this:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:oxm="http://www.springframework.org/schema/oxm" 
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-3.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
GriffeyDog
  • 8,186
  • 3
  • 22
  • 34
  • Tried that as suggested below. I'll add my *-servlet.xml file – Dan Aug 21 '12 at 19:28
  • Try adding this to your Spring config file: `` – GriffeyDog Aug 21 '12 at 20:04
  • Stacktrace: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 14 in XML document from ServletContext resource [/WEB-INF/baseSpringMVC-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: The prefix "mvc" for element "mvc:annotation-driven" is not bound. at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) – Dan Aug 22 '12 at 12:16
  • You need to define the mvc namespace at the top of the file. So you need `xmlns:mvc="http://www.springframework.org/schema/mvc"` and within `xsi:schemalocation` you need `http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd`. – GriffeyDog Aug 22 '12 at 14:22
  • Can you please clarify so I can cut and paste. – Dan Aug 22 '12 at 19:22
  • Ok... do I still need to add the tag? – Dan Aug 23 '12 at 15:47
0

Change method to POST, test it and let me know so that we can debug further

@RequestMapping(value="/{description}", method=RequestMethod.POST)
Java P
  • 2,241
  • 6
  • 31
  • 45
0

Remove the top level RequestMapping annotation on the controller. Your updateDescription method will dictate the request mapping.

KnownSubset
  • 139
  • 7
  • http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-handlermapping section 15.3.2. Says you can have the top level controller with each method having another mapping – Dan Aug 21 '12 at 17:28
  • updated link http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-ann-requestmapping – Dan Aug 21 '12 at 17:34
  • Yes you can, but it at least should not have the ".htm" extension in it. You should make the URL equivalent to "http://localhost:8505/baseSpringMVC/updatedescription/Chair". – KnownSubset Aug 21 '12 at 17:36