现在介绍的是SimpleFormController。这个类是用来处理输入中的表单输入的。不多说,直接看例子。
环境同以前的Spring例子。
首先是web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<display-name>
SpringTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
然后是在/WEB-INF/jsp/下创建一个form.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta. http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Form. Page</title>
</head>
<body>
<center>
<form. action="user.do" method="post">
Username:<input type="text" name="username" /><br/>
Password:<input type="password" name="password" /><br/>
<input type="submit" value="Submit" />
</form>
</center>
</body>
</html>
同样需要输入username和password两个属性。
在src下面写一个User类:
package src;
public class User {
private String username;
private String password;
public User(){
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
然后写一个UserFormController.java:
package src;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
public class UserFormController extends SimpleFormController{
public UserFormController(){
setCommandClass(User.class);
}
public ModelAndView onSubmit(Object command) throws Exception{
User user = (User)command;
String username = user.getUsername();
String password = user.getPassword();
if(username.equals("Jim")&&password.equals("12345")){
return new ModelAndView(getSuccessView(), "info", user);
}else{
return new ModelAndView(getFormView());
}
}
}
这里面,我们继承了SimpleFormController类,并且覆盖了onSubmit()方法,因为需要在成功的页面显示输入的数据。如果不需要显示的话,覆盖doSubmitAction()方法就可以了。SimpleFormController类有两个方法:getSuccessView()方法返回成功后的视图名,而getFormView()方法返回需要重新输入的视图名。其中,successView和formView两个属性稍后在test-servlet.xml中配置。
下面就是test-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="/user.do" class="src.UserFormController">
<property name="successView">
<value>user</value>
</property>
<property name="formView">
<value>form</value>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
当验证成功,即username为Jim,password为12345时,我们将看到user.jsp,如果失败,则返回到form.jsp重新输入。
下面就是user.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta. http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Information Page</title>
</head>
<body>
<center>
<b>Welcome ${info.username }!</b><br/>
<b>Your password is:${info.password }</b>
</center>
</body>
</html>
这里我们可以将用户信息显示出来。注意user.jsp和form.jsp都放在/WEB-INF/jsp/下面。这个例子就完成了。