RequestMapping不起作用

时间:2013-12-20 08:12:33

标签: java spring

我使用netbean构建一个Spring项目并遇到了一些问题。

我只是放了一个表单和一个提交输入,它应该由StudentController处理,但是当点击按钮时我得到了404。 我做错了吗?

的index.jsp

<body>
    <form method="GET" action="student">
        <input type="submit" value="submit">
    </form>
</body>

StudentController.java

@Controller
public class StudentController {

@RequestMapping(value = "/student",method = RequestMethod.GET)
public ModelAndView student(){
    return new ModelAndView("student", "command", new Student());
}
}

web.xml (由NetBean生成)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

applicationContext.xml (由NetBean生成)

<?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:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
</beans>

dispatcher-servlet.xml (由NetBean生成)

<?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:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!--
The index controller.
-->
<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />
</beans>

很抱歉在这里发布所有代码,因为我真的不知道问题出在哪里。请帮我。提前谢谢!

3 个答案:

答案 0 :(得分:1)

您的Spring servlet映射到*.htm

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

但您的表单会向/student发送请求。 *.htm/student不匹配,显然,您获得了404。

答案 1 :(得分:0)

我认为这是因为调度程序servlet仅侦听.htm个请求,但您请求localhost:8080/Student/student

尝试

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
     <url-pattern>/*</url-pattern>
</servlet-mapping>

答案 2 :(得分:0)

由@JB Nizet建议,我删除了

  1. ControllerClassNameHandlerMapping
  2. urlMapping
  3. indexController
  4. 来自dispatcher-servlet.xml文件,只需将其替换为<mvc:annotation-driven />

    此外,<context:component-scan base-package="com.mycompany.myapp.controllers"></context:component-scan>已添加到dispatcher-servlet.xml文件中。

    此外,您需要指定xsi:schemaLocation,因此请添加

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

    xsi:schemaLocation属性。

    在完成所有这些之后,我将index.jsp编辑为:(让学生成为student.htm)

        <form method="GET" action="student.htm">
            <input type="submit" value="submit">
        </form>
    

    简单地在StudentController.java中写下这些:

    @RequestMapping(value="/index.htm", method=RequestMethod.GET)
    public String index(){
        return "index";
    }
    
    @RequestMapping(value = "/student.htm",method = RequestMethod.GET)
    public ModelAndView student(){
        return new ModelAndView("student", "command", new Student());
    }
    

    之后,问题得到解决 也许我应该考虑使用Eclipse而不是NetBean来构建Spring。