为什么Spring中的RequestMapping获取null值

时间:2016-02-09 07:23:34

标签: java spring jsp spring-mvc

当我提交表单时,它会处理并进入控制器类。但在RequestMapping中,我没有得到这个值。当我提交表单时,它会处理并进入控制器类。但在RequestMapping中,我没有得到这个值。 当我提交表单时,它会处理并进入控制器类。但在RequestMapping中,我没有得到这个值。

的index.jsp

<form method="POSt" action="login.htm">
            Email : <input type="text" path="txtemail"/>
            Password : <input type="text" path="txtpwd"/><br>
            <input type="submit" value="Save"/>
           </form>

controller.java

@Controller
public class controller extends model{
@RequestMapping(value="/login",method = RequestMethod.POST)
 public String login(HttpServletRequest request, 
        @RequestParam(value="txtemail", required=false)String email,
        @RequestParam(value="txtpwd", required=false) String password){

     controller_save obj=new controller_save();
     obj.setEmail(email);
     obj.setPassword(password);   
     return "index";
 }   
}

的web.xml

<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>
        <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
        <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>

2 个答案:

答案 0 :(得分:1)

尝试替换它:

 <form method="POSt" action="login.htm">

要:

  <form method="POST" action="/login">

使用login代替login.htm,因为您有value="/login"

 @RequestMapping(value="/login",method = RequestMethod.POST)  

并写下这样的行:

   Email : <input type="text" name="txtemail" path="txtemail"/>
   Password : <input type="text" name="txtpwd" path="txtpwd"/><br>

并且:

  @RequestParam(value="txtemail", required=false) String txtemail,
  @RequestParam(value="txtpwd", required=false) String txtpwd){

答案 1 :(得分:0)

使用login而不是login.htm或/ login in form <form method="POST" action="login">