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!

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

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">

6 Comments

Tried that as suggested below. I'll add my *-servlet.xml file
Try adding this to your Spring config file: <mvc:annotation-driven />
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)
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.
Can you please clarify so I can cut and paste.
|
0

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

@RequestMapping(value="/{description}", method=RequestMethod.POST)

5 Comments

That's not working either. It's coming from a link, so I believe it has to be a GET.
Do you have below line:<context:component-scan base-package="controller package" />
Yes, that's within the baseSpringMVC-servlet.xml file
No, I am at work and they block those sites here. It's coming from the Spring tutorial though
Do you have <mvc:annotation-driven /> in your configuration
0

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

3 Comments

static.springsource.org/spring/docs/3.0.x/reference/… section 15.3.2. Says you can have the top level controller with each method having another mapping
Yes you can, but it at least should not have the ".htm" extension in it. You should make the URL equivalent to "localhost:8505/baseSpringMVC/updatedescription/Chair".

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.