Struts1.x ActionForm

Source: Internet
Author: User
1. ActionForm
Principle
Inherited from org. apache. struts. action. ActionForm, after jsp sends a request, instantiate the servlet and load the struts-config.xml. You can run the action method to obtain the name of the actionform in mapping. First, you can check whether the corresponding actionform exists in the session and request. If yes, you can directly return the actionform. Otherwise, the corresponding instance class is constructed based on the name reflection, and set the data in the request to actionform. For example


          
2. Use
ActionForm usage requirements
1. org. apache. struts. action. ActionForm accumulation must be extended.
2. Define the read/write public attributes that are consistent with the name of the input control in the html form
Optional parameter
Reset: initialize required attributes
Validate: validate required attributes
            
Function
Field Collector
Struts automatically sets the corresponding values based on the attributes transmitted from http. All development does is match the control name on the form with the public attributes on the ActionForm.
Data buffer
Although JavaScript can be used to control data verification on the web end, such compatibility and script unavailability always occur. The validate provided by ActionForm can block this problem.

Type conversion
Because key-value is transmitted from http, and value is usually text. In ActionForm, attributes have their own defined data types, such as boolean or string. Beanutils is a third-party development kit. Different data types call different conversion packages.
Principle: when requesting servlet, the init method registers the corresponding conversion class, that is, the corresponding converters of several basic data types. The most basic principle of the converter is to implement the convert class. For example, this data needs to be converted to the boolean type. When it is passed, the data type and data base will be passed. If the data is of the boolean type, it is returned directly; if not, it is (boolean) value. tostring () is strongly converted. Because the target data type is already known at this time, the conversion will not result in data loss.

Custom Type Conversion. The preceding conversion can only be performed for basic data types. For some custom data types, you need to define the corresponding converter for conversion. Here, we will use the transformation idea to simulate the implementation principle of struts. Then, register the servlet to the corresponding map structure during initialization.

Demo
Java converter
Package cfl. convert. util; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; import javax. servlet. http. httpServlet; import org. apache. commons. beanutils. converter;/** initialize the Converter-Chen Fanglin-July 15, October 15, 2014 */public class personalConvert implements Converter {@ Overridepublic Object convert (Class type, Object value) {// TODO Auto-generated method stubif (value instanceof Date) {return value;} if (value instanceof String) {try {return new SimpleDateFormat ("yyyy-MM-dd "). parse (String) value);} catch (ParseException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} return null ;}}



Servlet
Package cfl. convert. util; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import org. apache. commons. beanutils. convertUtils; public class personalConvertServlet extends HttpServlet {@ Overridepublic void init () throws ServletException {// TODO Auto-generated method stubSystem. out. print ("success"); ConvertUtils. register (new personalConvert (), java. util. date. class );}}



Configure custom servlet initialization and register the corresponding converter

<Servlet> <servlet-name> personConvert </servlet-name> <servlet-class> cfl. convert. util. personalConvertServlet </servlet-class> <load-on-startup> 3 </load-on-startup> </servlet>




DynaActionForm
To replace ActionForm with configuration, you need to define a series of attributes. However, when verification and other attributes are involved, third-party jar packages (validator) must be used for implementation.

1. Configure attributes

Demo

<Form-beans> <form-bean name = "dynaForm" type = "org. apache. struts. action. dynaActionForm "> <form-property name =" username "type =" java. lang. string "> </form-property> <form-property name =" age "type =" java. lang. integer "> </form-property> </form-bean> </form-beans>



In this way, data can also be obtained from the actionform through the EL expression. That is to say, there is no difference between the dynamic form and the static form, but some third-party jar is required in implementation.



  
    
3. Summary
Actionform processes form data separately based on form forms, which reduces the burden on jsp and the coupling between data and display logic. Easy to develop. Note that the name of the input control in the form must be consistent with the public attributes of the actionform class. It is also this convention that reduces the difficulty of programming and development, as is often said in development: Convention is better than configuration.

Struts1.x ActionForm

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.