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

4 回答 4

1

当您将 /Chair 添加到它的末尾时,它不是 *.htm。发布您的 web.xml,以便我们对其进行调试。

于 2012-08-21T16:37:50.187 回答
1

您的类级请求映射不应包含“.htm”。也就是说,改变:

@RequestMapping("/updatedescription.htm") 

@RequestMapping("/updatedescription") 

如果这不能解决问题,我们可能需要查看您的 Spring 配置文件。

编辑:

Spring 配置文件的顶部,包括 MVC 命名空间,应如下所示:

<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">
于 2012-08-21T19:25:49.487 回答
0

删除控制器上的顶级 RequestMapping 注释。您的 updateDescription 方法将指示请求映射。

于 2012-08-21T17:24:03.883 回答
0

将方法更改为 POST,对其进行测试并让我知道,以便我们进一步调试

@RequestMapping(value="/{description}", method=RequestMethod.POST)
于 2012-08-21T17:14:03.307 回答