The struts composition of the progressive Struts1 (iii) layer

Source: Internet
Author: User
Tags integer

This blog we say the main composition of struts we, through the previous several blogs, we know this framework the most important steps: Get the path, encapsulate the form, get the steering list, turn logic processing, turn, and this corresponds to: Actionservlet, Actionform, Actionmapping, Action, Actionforward.

Actionservlet

This has been explained in the previous blog, you can access the preload Actionservlet.

Actionform

The main role of Actionform is to encapsulate form data, which is somewhat similar to the entity classes in the. NET development, and its use has the following limitations: The Get/set property name in Actionform must be consistent with the form field ; must inherit from Org.apache.struts.action.ActionForm.

Actionform In addition to the encapsulation of data surprises, you can also perform data validation, assembly initialization properties, of course, this need to implement validate and reset function.

Actionform Converter

It also automates the logical conversion of data, using a bean-utils converter, as mentioned in the previous blog, which is located in Initother () in Actionservlet, as follows:

if (convertnull) {  
           convertutils.deregister ();  
           Convertutils.register (new Bigdecimalconverter (null), bigdecimal.class);  
           Convertutils.register (new Bigintegerconverter (null), biginteger.class);  
           Convertutils.register (new BooleanConverter (null), boolean.class);  
           Convertutils.register (new Byteconverter (null), byte.class);  
           Convertutils.register (new Characterconverter (null), character.class);  
           Convertutils.register (new Doubleconverter (null), double.class);  
           Convertutils.register (new Floatconverter (null), float.class);  
           Convertutils.register (new Integerconverter (null), integer.class);  
           Convertutils.register (new Longconverter (null), long.class);  
           Convertutils.register (new Shortconverter (null), Short.class);  
       }

The purpose of this code snippet is to initialize the converter to a map that implements the method of the Convert interface:

public object Convert (Class type, Object value);

Such as a Boolean converter, the code is as follows:

Example of a Boolean converter public Object convert (Class type, Object value) {if (value = = null) {  
            if (Usedefault) {return (defaultvalue);  
            else {throw new Conversionexception ("No value specified");  
        } if (value instanceof Boolean) {return (value);  
            try {String stringvalue = value.tostring ();  
                if (Stringvalue.equalsignorecase ("yes") | |  
                Stringvalue.equalsignorecase ("y") | |  
                Stringvalue.equalsignorecase ("true") | |  
                Stringvalue.equalsignorecase ("on") | |  
            Stringvalue.equalsignorecase ("1")) {return (boolean.true);  
                       else if (stringvalue.equalsignorecase ("no") | |  
                       Stringvalue.equalsignorecase ("n") | |  
             Stringvalue.equalsignorecase ("false") | |          Stringvalue.equalsignorecase ("Off") | |  
            Stringvalue.equalsignorecase ("0")) {return (boolean.false);  
            else if (Usedefault) {return (defaultvalue);  
            else {throw new conversionexception (stringvalue);  
            } catch (ClassCastException e) {if (Usedefault) {return (defaultvalue);  
            else {throw new conversionexception (e); }  
        }  
      
    }

You can see that it has a limited type of conversion, what if I want to convert other types? Fortunately, this provides a custom converter function, see the blog, "Custom converter."

Dynamic Actionform

It exists in the form of a configuration file, such as a configuration in Struts-config:

<form-beans>  
    <form-bean name= "DynaForm"  type= "Org.apache.struts.action.DynaActionForm" >  
        <form-property name= "username" type= "java.lang.String"/> <form-property  
        "age" name= " Java.lang.Integer "/>  
    </form-bean>  
</form-beans>

Note that the dynamic Actionform reads slightly differently:

<body> 

 
  user name: ${dynaform.map.username};   Note how old it gets    : ${dynaform.map.age}  
</body>

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.