弹簧控制器

时间:2012-08-21 16:13:20

标签: java spring-mvc spring-annotations

我正在尝试按照Spring教程,但仍然收到404错误。以下是我的Controller类:

@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;
    }
}

当我尝试去“http:// localhost:8505 / baseSpringMVC / updatedescription.htm / Chair”时,我收到404错误。

我已经设置了我的web.xml文件,将所有* .htm流量指向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>

任何帮助都会很棒。

谢谢!

4 个答案:

答案 0 :(得分:1)

当你添加/主持它时,它不是* .htm。发布你的web.xml,以便我们调试它。

答案 1 :(得分: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">

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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