目錄 -------------------------------------------------------------------------------- 登錄例子 區別 時間屬性 驗證機制 -------------------------------------------------------------------------------- 在Struts1中,我們經常接觸ActionForm為靜態ActionForm。靜態是指開發者在使用struts框架時必須寫對應的ActionForm。 在介紹動態ActionForm使用之前,我們回憶struts常用的ActionForm的方式,以便做對比。 舉個完整的登錄例子。 第一:配置web.xml [html] <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> 第二:新建jsp頁面(在login文件夾) [html] view plaincopyprint? <form action="../login.do" method="get"> 用戶名: <input name="username" type="text"><br> 密 碼: <input name="password" type="text"><br> <input type="submit" > </form> <form action="../login.do" method="get"> 用戶名: <input name="username" type="text"><br> 密 碼: <input name="password" type="text"><br> <input type="submit" > </form> 第三:新建ActionForm [html] package com.login; import java.util.Date; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; public class LoginForm extends ActionForm{ public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private String username; private String password; } package com.login; import java.util.Date; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; public class LoginForm extends ActionForm{ public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private String username; private String password; } 第四:新建Action [html] package com.login; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginForm loginForm=(LoginForm)form; boolean flag=false; if("admin".equals(loginForm.getUsername())&&"admin".equals(loginForm.getPassword())){ flag=true; } request.setAttribute("username",loginForm.getUsername()); if(flag) { return mapping.findForward("success"); }else{ return mapping.findForward("false"); } } } package com.login; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginForm loginForm=(LoginForm)form; boolean flag=false; if("admin".equals(loginForm.getUsername())&&"admin".equals(loginForm.getPassword())){ flag=true; } request.setAttribute("username",loginForm.getUsername()); if(flag) { return mapping.findForward("success"); }else{ return mapping.findForward("false"); } } } 第五:配置struts核心配置文件struts-config.xml [html] <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <!-- 靜態form --> <form-beans> <form-bean name="loginForm" type="com.login.LoginForm"></form-bean> </form-beans> <action-mappings> <action path="/login" type="com.login.LoginAction" name="loginForm" scope="request"> <forward name="success" path="/login/success.jsp"></forward> <forward name="false" path="/login/false.jsp"></forward> </action> </action-mappings> </struts-config> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <!-- 靜態form --> <form-beans> <form-bean name="loginForm" type="com.login.LoginForm"></form-bean> </form-beans> <action-mappings> <action path="/login" type="com.login.LoginAction" name="loginForm" scope="request"> <forward name="success" path="/login/success.jsp"></forward> <forward name="false" path="/login/false.jsp"></forward> </action> </action-mappings> </struts-config> 使用以上常用方式,最大的麻煩就是,幾乎為每個表單創建一個ActionForm。導致ActionForm數量多。 而使用動態ActionForm,最大的優點就是,不必手動為每個表單創建ActionForm。 使用動態ActionForm的方式: 上述的第一步、第二步不變、第三步取消、修改第四步、第五步。 為了讓方便大家理解,首先修改第五步: [html] 、 <!-- 動態form --> <form-beans> <form-bean name="loginForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" type="java.lang.String"></form-property> <form-property name="password" type="java.lang.String"></form-property> </form-bean> </form-beans> <!-- 動態form --> <form-beans> <form-bean name="loginForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" type="java.lang.String"></form-property> <form-property name="password" type="java.lang.String"></form-property> </form-bean> </form-beans> 第五步中使用form-property指明表單中的屬性,其中標簽內的name是表單jsp中的控件name值,兩者必須相同。type是指控件值的類型。 修改第四步: [html] package com.login; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm loginForm=(DynaActionForm)form; String username=(String)loginForm.get("username"); String password=(String)loginForm.get("password"); boolean flag=false; if("admin".equals(username)&&"admin".equals(password)){ flag=true; } request.setAttribute("username",username); if(flag) { return mapping.findForward("success"); }else { return mapping.findForward("false"); } } } package com.login; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm loginForm=(DynaActionForm)form; String username=(String)loginForm.get("username"); String password=(String)loginForm.get("password"); boolean flag=false; if("admin".equals(username)&&"admin".equals(password)){ flag=true; } request.setAttribute("username",username); if(flag) { return mapping.findForward("success"); }else { return mapping.findForward("false"); } } } 靜態ActionForm方式,使用get/set方法,而動態ActionForm方式,使用的map的get key方式。其中的key就是標簽name值。 使用動態ActionForm的另一個優點是,若是更改表單和ActionForm時,不需要重新部署。而使用靜態ActionForm時,則需要修改ActionForm.java文件,還必須重新部署。 但是使用動態ActionForm,配置文件太長。不如靜態ActionForm清晰明了。 使用時間屬性 動態ActionForm使用時間類型 第一種方式:使用java.sql.Date類型。但是界面必須是yyyy-MM-dd這種形式。 第二種方式:使用String類型。但是在後台自己轉換。 靜態ActionForm使用時間類型 ActionForm有自行匹配,檢驗的功能。自動把表單上的數據轉化成ActionForm相應的數據類型。但是默認的java.sql.Date類型,但是對於java.util.Date類型則不能自動轉化。 所以為了使用java.util.Date類型,我們手動開發時間轉化類,然後注入到框架中 [html] package com.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.beanutils.Converter; public class UtilDateConvert implements Converter { @Override public Object convert(Class type, Object value) { if(value==null){ return value; } if(value instanceof Date){ return value; } Date dateConvert=null; if(value instanceof String){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd"); try { dateConvert =(Date)sdf.parse((String)value); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return dateConvert; } } package com.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.beanutils.Converter; public class UtilDateConvert implements Converter { @Override public Object convert(Class type, Object value) { if(value==null){ return value; } if(value instanceof Date){ return value; } Date dateConvert=null; if(value instanceof String){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd"); try { dateConvert =(Date)sdf.parse((String)value); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return dateConvert; } } 其中轉化類中定義什麼格式,界面輸入什麼格式。如上文中,定義yyyy/MM/dd 界面輸入1988/10/11.若輸入1988-10-11格式,則提示轉化錯誤。另外轉化類中可以定義yyyy-MM-dd格式。 [html] package com.util; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.apache.commons.beanutils.ConvertUtils; public class UtilDateServlet extends HttpServlet { @Override public void init() throws ServletException { // TODO Auto-generated method stub ConvertUtils.register(new UtilDateConvert(), Date.class); } } package com.util; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.apache.commons.beanutils.ConvertUtils; public class UtilDateServlet extends HttpServlet { @Override public void init() throws ServletException { // TODO Auto-generated method stub ConvertUtils.register(new UtilDateConvert(), Date.class); } } 在配置文件中配置servlet [html] <servlet> <servlet-name>dateConvert</servlet-name> <servlet-class>com.util.UtilDateServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet>