操纵Spring MVC的表单掌握器SimpleFormController[Java编程]
本文“操纵Spring MVC的表单掌握器SimpleFormController[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
以注册历程为例,我们大概会挑选担当AbstractController来实现表单的显示,担当AbstractCommandController来实现表单的处理 ,这样是可行的,但必必要保护两个掌握器
在这种情形下,我们应当利用SimpleFormController,他承受GEt恳求时显示表单,承受POST恳求时处理表单,假如发生错误,掌握器会知道重新显示这个表单,这样用户便可以改正错误,重新提交
表单对应的POJO
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;
}
}
掌握器:
这个base类中还有一个DoSubmitAction()办法,和onSubmit()办法的辨别就是后者可以返回一个ModelAndView对象,完成向页面输出数据的功效,而前者不能向页面返回数据,这两个办法同时只有一个有效
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);
}
}
web.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>
<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>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
以上是“操纵Spring MVC的表单掌握器SimpleFormController[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |