Take the registration process as an example, we may choose to inherit abstractcontroller to implement the form display, inherit Abstractcommandcontroller to implement the form processing, this is feasible, but must maintain two controllers
In this case, we should use Simpleformcontroller, when he accepts a GET request, displays the form, processes the form when it accepts a POST request, and if an error occurs, the controller will know to display the form again so that the user can modify the error and resubmit
The Pojo of the form corresponding to
package model;
public class Student ...{
private String name;
private String sex;
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getSex() ...{
return sex;
}
public void setSex(String sex) ...{
this.sex = sex;
}
}
Controller:
There is also a dosubmitaction () method in this base class, and the OnSubmit () method is the difference between the latter can return a Modelandview object, the ability to output data to the page, while the former can not return data to the page, Both methods have at the same time only one valid
Package Action;
import model.Student;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
public class RegisterStudentController extends SimpleFormController ...{
public RegisterStudentController()...{
this.setCommandClass(Student.class);
}
protected ModelAndView onSubmit(Object object, BindException arg1) throws Exception ...{
Student stu=(Student)object;
return new ModelAndView(getSuccessView(),"student",stu);
}
}
Xml
<?xml version= "1.0" encoding= "UTF-8"
<web-app 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
<context-param>
& Lt;param-name>contextconfiglocation</param-name>
<param-value>/web-inf/train-service.xml,/ Web-inf/train-data.xml,/web-inf/train-servlet.xml</param-value>
</context-param>
<servlet
<servlet-name>train</servlet-name>
<servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class>
<load-on-startup>0</ Load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>train</ Servlet-name>
<url-pattern>*.mvc</url-pattern>
</servlet-mapping>
<filter>
<filter-name>character</filter-name>
<filter-class>action.characterfilter</ Filter-class>
</filter>
<filter-mapping>
<filter-name>character</filter-name& Gt
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
; Welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>