<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foudation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean type="demo.AddStudentForm" name="addStudentForm"> </form-bean> </form-beans> <action-mappings> <action path="/addStudent" type="demo.AddStudentAction" name="addStudentForm"> <forward name="addStudentSuccess" path="/addStudentSuccess.jsp"></forward> <forward name="addStudentFailure" path="/addStudentFailure.jsp"></forward> </action> </action-mappings> </struts-config>
1.检查<action-mappings>,查看action中的name属性。
2.根据name查看<form-bean>中的配置信息。
3.如果找到,重用
4.未找到,构建一个form-bean实例,且保存在一定的作用范围(request,session)
5.调用reset()
6.从客户提交的参数中取值,调用set方法取值
7.查看action中的validate属性(默认为true)是否为ture,若是,进行校验,校验不通过,跳转到错误页面,错误页面由action中的input属性指定。
8.若校验成功,派发请求到Action中
package demo; import java.io.IOException; import java.io.PrintWriter; import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ActionServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Properties prop=new Properties(); prop.load(this.getClass().getClassLoader() .getResourceAsStream("form.properties")); String formClassFormname=prop.getProperty("formClassFormname"); String formName=prop.getProperty("formName"); FormUtil.fillForm(request, formClassFormname, formName); request.getRequestDispatcher("/addStudentSuccess.jsp").forward(request, response); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doPost(req, resp); } }
package demo; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import com.sun.org.apache.commons.beanutils.BeanUtils; public class FormUtil { public static void fillForm(HttpServletRequest request,String formClassFormname,String formName) { //1.实例化ActionForm的子类 //2.提取表单参数 //3.将表单参数和bean中的参数进行匹配 //4.将form保存在session中 try { ActionForm actionForm=(ActionForm)Class.forName(formClassFormname).newInstance(); Enumeration params=request.getParameterNames(); Field [] fields=actionForm.getClass().getDeclaredFields(); while(params.hasMoreElements()){ String paraname=(String) params.nextElement(); for(Field field:fields){ String fieldName=field.getName(); if(fieldName.endsWith(paraname)){ String value=request.getParameter(fieldName); try { BeanUtils.setProperty(actionForm, fieldName, value); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } request.getSession().setAttribute(formName, actionForm); System.out.println(actionForm.toString()); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package demo; public class AddStudentForm extends ActionForm { private String sname; private String major; private float score; private java.sql.Date birth; public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } public java.sql.Date getBirth() { return birth; } public void setBirth(java.sql.Date birth) { this.birth = birth; } @Override public String toString() { // TODO Auto-generated method stub return "name="+sname+" major="+major+" score="+score+" birth"+birth.toString(); } }