The Actionform of Struts learning notes

Source: Internet
Author: User
Tags config reset

Some javabeans are defined in Struts, mainly in the Actionform as the parent class, as shown in the following diagram:

in Org.apache.struts.action bag

Public abstract class Actionform implements Serializable

public class Dynaactionform extends Actionform implements Dynabean

in Org.apache.struts.validator bag

public class Validatorform extends Actionform implements Serializable

public class Dynavalidatorform extends Dynaactionform

Implements Dynabean, Serializable

in Org.apache.struts.validator bag

public class Validatoractionform extends Validatorform implements Serializable

public class Dynavalidatoractionform extends Dynavalidatorform

Implements Dynabean, Serializable

1. The two commonly used methods in Actionform are reset () and validator ():

Restores the default value of the Actionform property, such as setting the Boolean to True/false and the string to null.

public void Reset(actionmapping mapping, HttpServletRequest request) {}

Validate only checks the data format and syntax, and does not check whether the data conforms to business logic.

Public actionerrors Validate(actionmapping mapping, HttpServletRequest request) {return (null);}

The default implementation of these two methods is to do nothing, and we can override both methods to implement the relevant logic.

Note: for each request, the controller calls the Actionform Reset () method first, and then the form data is assembled into the actionform. If Actionform is within the request scope, a new Actionform instance will be created for each new requests request. After a new instance is created, if its properties have been initialized to the default value , then it is not necessary to set the property to the default value in the Reset () method, which allows the reset () method to be empty.

For Actionform in the session range, the same Actionform instance is shared by multiple requests, and the reset () method is extremely useful in this case.

2. Where Actionform needs us to create a Formbean class inheritance Actionform, some property and Get/set methods can be defined in Actionform.

The property of Actionform must be declared before it can be used, but we often need to enter some query conditions in the query, these query conditions (property) do not need to be declared in Formbean, You can then use the Map object to encapsulate the data submitted by the entire query form, as follows:

public class Mapform extends Actionform {

Private map map = null;

public void Setmap (map map) {

This.map = map;

}

Public Map Getmap () {

return this.map;

}

Add the Get/set method to the query condition and put the data in the map

public void SetAttribute (String attributekey, Object attributevalue) {

Map.put (Attributekey, AttributeValue);

}

Public Object getattribute (String attributekey) {

Object KeyValue = Map.get (Attributekey);

return keyValue;

}

}

On the page, you can

To get the form data, which calls the getattribute ("id") method.

3. The dynamic Actionform at the beginning of theDyna does not need to create a specific Actionform class, all of the Actionform configuration can be done through the struts configuration file, such as:

<form-bean name= "OptionsForm" type= "org.apache.struts.action. dynaactionform">

<form-property name= "fruit1" type= "java.lang.String" initial= "Pear"/>

<form-property name= "Fruit2" type= "java.lang.String" initial= "Apple"/>

</form-bean>

4. Among them, Actionform containing validator is used for form verification, there are two methods of verification.

Set the action's Validate property to "true" (default is "true") in Struts-config.xml.

<action path= "/updateuser"

Type= "Com.cn.lively.action.UpdateUserAction"

Name= "UserForm"

Scope= "Request"

input= "/jsp/updateuser.jsp"

Cancellable= "true"

validate= "true" >

<forward name= "Success" path= "/jsp/validator/updateuserresults.jsp"/>

</action>

and rewrite the validate method in the corresponding Formbean, implement own data validation logic in the validate method.

is validated through the validation framework, which is divided into two steps:

Configuring the validation plug-in in Struts-config.xml,

<plug-in classname= "Org.apache.struts.validator.ValidatorPlugIn" >

<set-property property= "Pathnames"

Value= "/org/apache/struts/validator/validator-rules.xml,

/web-inf/validation.xml "/>

</plug-in>

Configuration Formbean, there are two ways:

㈠ the Formbean class to inherit the Actionform that contains validator

public class UserForm extends validatorform{

Private String UserName;

Public String GetUserName () {

return userName;

}

public void Setusername (String userName) {

This.username = UserName;

}

}

and configure the Struts-config.xml file:

<form-bean name= "userForm" type= "Com.cn.lively.formbean.UserForm" >

</form-bean>

㈡ the Formbean class to inherit the Dynavalidatorform that contains validator

<form-bean name= "userForm" type= "Org.apache.struts.validator. dynavalidatorform">

<form-property name= "UserName" type= "java.lang.String"/>

</form-bean>

configuration validation.xml file:

<formset>

<form name= "userForm" >

<field property= "UserName" depends= "required" >

<arg key= "Userform.username"/>

</field>

</form>

</formset>

Note : Notice that the userForm in the Validation.xml file is the Formbean name in the Struts-config.xml file.

Several properties about Arg are as follows:

Bundle: Specifies the resource file name, if not specified, to read from the default resource file

Key: The value obtained from the resource file actionresources.properties

Resource:key whether the information specified is from an external resource file, which is true by default. If true, represents key in the resource file specified by key for the Buddle property.

Position, the value in this arg is used to replace which part of the message, which needs to be replaced by the {n} flag.

5. About the difference between validatorform/dynavalidatorform and validatoractionform/dynavalidatoractionform

For a actionform, you can be used by more than one action, and each action may require a different validation field, and Valida

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.