使用dom4j解析struts配置文件,利用反射技术从配置文件中读取FormBean,示例代码如下:

1、com.wgy.form.LoginForm类:

  1. public class LoginForm {  
  2.     private String name;  
  3.     private String password;  
  4.  
  5.     public String getName() {  
  6.         return name;  
  7.     }  
  8.  
  9.     public void setName(String name) {  
  10.         this.name = name;  
  11.     }  
  12.  
  13.     public String getPassword() {  
  14.         return password;  
  15.     }  
  16.  
  17.     public void setPassword(String password) {  
  18.         this.password = password;  
  19.     }  
  20.  
  21.     @Override 
  22.     public String toString() {  
  23.         return "LoginForm [name=" + name + ", password=" + password + "]";  
  24.     }  

2、struts-config.xml文件,位于src下:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE struts-config PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"  
  4.     "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> 
  5.  
  6. <struts-config> 
  7.     <form-beans> 
  8.         <form-bean name="loginForm" type="com.wgy.form.LoginForm" /> 
  9.     </form-beans> 
  10. </struts-config> 

3、使用dom4j解析上述文件:

  1. public class XmlUtils {  
  2.     public static String[] parseXml() throws Exception {  
  3.         SAXReader reader = new SAXReader();  
  4.         InputStream inStream = XmlUtils.class.getClassLoader()  
  5.                 .getResourceAsStream("struts-config.xml");  
  6.         Document document = reader.read(inStream);  
  7.         Element root = document.getRootElement();  
  8.         Element formBean = root.element("form-beans").element("form-bean");  
  9.         String name = formBean.attributeValue("name");  
  10.         String type = formBean.attributeValue("type");  
  11.         return new String[] { name, type };  
  12.     }  

4、编写Servlet控制器:

  1. public class ActionServlet extends HttpServlet {  
  2.  
  3.     private static final long serialVersionUID = 1L;  
  4.  
  5.     protected void doGet(HttpServletRequest request,  
  6.             HttpServletResponse response) throws ServletException, IOException {  
  7.         doPost(request, response);  
  8.     }  
  9.  
  10.     protected void doPost(HttpServletRequest request,  
  11.             HttpServletResponse response) throws ServletException, IOException {  
  12.         try {  
  13.             String[] values = XmlUtils.parseXml();  
  14.             String formName = values[0];  
  15.             String formClassFullName = values[1];  
  16.             FormUtils.fillForm(request, formClassFullName, formName);  
  17.         } catch (Exception e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.     }  
  21.  

5、将上述servlet配置到web.xml文件中:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  5.     id="WebApp_ID" version="2.5"> 
  6.     <display-name>Struts1Test</display-name> 
  7.     <welcome-file-list> 
  8.         <welcome-file>index.jsp</welcome-file> 
  9.     </welcome-file-list> 
  10.     <servlet> 
  11.         <servlet-name>ActionServlet</servlet-name> 
  12.         <servlet-class>com.wgy.servlet.ActionServlet</servlet-class> 
  13.     </servlet> 
  14.     <servlet-mapping> 
  15.         <servlet-name>ActionServlet</servlet-name> 
  16.         <url-pattern>*.do</url-pattern> 
  17.     </servlet-mapping> 
  18. </web-app>  

6、编写FormUtils类,利用反射封装FormBean

  1. public class FormUtils {  
  2.  
  3.     @SuppressWarnings("unchecked")  
  4.     public static void fillForm(HttpServletRequest request,  
  5.             String formClassFullName, String formName) {  
  6.         try {  
  7.             LoginForm loginForm = (LoginForm) Class.forName(formClassFullName)  
  8.                     .newInstance();  
  9.             Field[] fields = loginForm.getClass().getDeclaredFields();  
  10.             Enumeration<String> parameterNames = request.getParameterNames();  
  11.             while (parameterNames.hasMoreElements()) {  
  12.                 String parameterName = parameterNames.nextElement();  
  13.                 for (Field field : fields) {  
  14.                     String fieldName = field.getName();  
  15.                     if (fieldName.equals(parameterName)) {  
  16.                         String value = request.getParameter(fieldName);  
  17.                         try {  
  18.                             BeanUtils.setProperty(loginForm, fieldName, value);  
  19.                         } catch (InvocationTargetException e) {  
  20.                             e.printStackTrace();  
  21.                         }  
  22.                     }   
  23.                 }  
  24.             }  
  25.             System.out.println(loginForm.toString());  
  26.         } catch (Exception e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  

7、编写测试页面index.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.     pageEncoding="UTF-8"%> 
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  4. <html> 
  5. <head> 
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  7. <title>index</title> 
  8. </head> 
  9. <body> 
  10.     <form action="<%=request.getContextPath() %>/login.do" method="post"> 
  11.         username:<input type="text" name="name" /> <br /> 
  12.         password:<input type="text" name="password" /> <br /> 
  13.         <input type="submit" value="登录" /> 
  14.     </form> 
  15. </body> 
  16. </html>