Use of static dynamic ActionForm in Struts (1)

Source: Internet
Author: User

Examples of directory login Difference Time Property Verification Mechanism ------------------------------------------------------------------------------ in Struts1, we often contact ActionForm as a static ActionForm. Static means that the developer must write the corresponding ActionForm when using the struts framework. Before introducing the use of dynamic ActionForm, we recall the commonly used ActionForm method of struts for comparison. A complete logon example is provided. 1. Configure 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-name> action </ser Vlet-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> 2: Create a jsp page (in the login folder) [html] view plaincopyprint? <Form action = ".. /login. do "method =" get "> User name: <input name =" username "type =" text "> <br> password: <input name = "password" type = "text"> <br> <input type = "submit"> </form> <form action = ".. /login. do "method =" get "> User name: <input name =" username "type =" text "> <br> password: <input name = "password" type = "text"> <br> <input type = "submit"> </form> 3: Create an 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. strut S. 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 Str Ing username; private String password;} Fourth: Create 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. act Ion. dynaActionForm; public class LoginAction extends Action {public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, response) throws Exception {LoginForm loginForm = (LoginForm) form; boolean flag = false; if ("admin ". equals (loginForm. getUsername () & "admin ". equals (loginForm. getPassword () {flag = true;} request. setAttribute ("username", logi NForm. 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. apach E. struts. action. actionMapping; import org. apache. struts. action. dynaActionForm; public class LoginAction extends Action {public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, response) throws Exception {LoginForm loginForm = (LoginForm) form; boolean flag = false; if ("admin ". equals (loginForm. getUsername () & "admin ". equals (loginForm. getPassw Ord () {flag = true;} request. setAttribute ("username", loginForm. getUsername (); if (flag) {return mapping. findForward ("success");} else {return mapping. findForward ("false") ;}} Fifth: configure the struts core configuration file 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> <! -- Static 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-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> <! -- Static 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-mappings> </struts-config> the most common method is to create an ActionF for almost every form. Orm. The number of actionforms is large. The biggest advantage of using dynamic ActionForm is that you do not have to manually create an ActionForm for each form. Dynamic ActionForm: the steps 1 and 2 remain unchanged, 3 are canceled, and 4 and 5 are modified. To make it easier for everyone to understand, first modify Step 5: [html], <! -- Dynamic 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> <! -- Dynamic 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> in step 5, use form-property to specify the attributes in the form, the name in the tag is the control name value in the form jsp, and the two must be the same. Type refers to the type of the control value. Step 4: [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 ("u Sername ", 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. actionMa Pping; 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 f Lag = 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") ;}} static ActionForm, using the get/set method, while dynamic ActionForm, using the map get key method. The key is the tag name value. Another advantage of using dynamic ActionForm is that you do not need to redeploy it when you change the form and ActionForm. When using static ActionForm, you must modify the ActionForm. java file and redeploy it. However, when dynamic ActionForm is used, the configuration file is too long. It is not as clear as static ActionForm. Use the time attribute dynamic ActionForm to use the time type. The first method is java. SQL. Date. However, the interface must be in the format of yyyy-MM-dd. Method 2: Use the String type. However, it is converted on the background. Static ActionForm uses the time-type ActionForm for self-matching and validation. The data on the form is automatically converted to the corresponding data type of ActionForm. However, the default java. SQL. Date type cannot be converted automatically for java. util. Date type. So to use java. util. date type. We manually develop the time conversion class and inject it into the framework [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 {@ Overridepublic 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-ge Nerated catch blocke. printStackTrace () ;}} return dateConvert ;}} the format defined in the conversion class and the format entered on the interface. In the above text, enter 1988/10/11 on the Interface defining yyyy/MM/dd. If the format is 1988-10-11, a conversion error is prompted. In addition, the conversion class can define the yyyy-MM-dd format. [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 {@ Overridepublic void init () throws ServletException {// TODO Auto-generated method stubConvertUtils. register (new UtilDateConvert (), Date. class) ;}} configure servlet [html] <servlet> <servlet-name> dateConvert </servlet-name> <servlet-class> com. util. utilDateServlet </servlet-class> <load-on-startup> 2 </load-on-startup> </servlet>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.