当我提交表单时,它会处理并进入控制器类。但是在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>
最佳答案
尝试替换为:
<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){