The Actionform of Struts learning notes

Source: Internet
Author: User
Tags reset

Struts defines some JavaBeans, which are mainly extended to Actionform as the parent class, as shown in the following figure:

org.apache.struts.action Package

Public abstract class Actionform implements Serializable

public class Dynaactionform extends Actionform implements Dynabean

Org.apache.struts.validator Package

public class Validatorform extends Actionform implements Serializable

public class Dynavalidatorform extends Dynaactionform

Implements Dynabean, Serializable

Org.apache.struts.validator Package

public class Validatoractionform extends Validatorform implements Serializable

public class Dynavalidatoractionform extends Dynavalidatorform

Implements Dynabean, Serializable

1. The two more 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 that the data conforms to the 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 rewrite the two methods to implement the relevant logic.

Note: for each request, the controller calls the Actionform Reset () method first, and the form data is assembled into the actionform. If Actionform is in the request scope, a new Actionform instance is created for each new request requests. After the 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, so that the reset () method can be empty.

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

2. Among them, Actionform needs us to create a Formbean class inheritance actionform, in Actionform can define some property and Get/set method.

Actionform's property must be declared before it can be used, but in the query we often need to enter some query conditions, these query conditions (property) actually do not need to declare in the 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;

}

Increase the Get/set method of the query condition (property) and put the data into 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 use the

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

3. Where the dynamic Actionform at the beginning of theDyna do not need to create a specific Actionform class, you can complete the Actionform configuration by simply using 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, the actionform containing validator is used for form verification, and there are two ways to verify it.

The Validate property of the setting action in Struts-config.xml is "true" (the default is "true").

<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 the validate method is rewritten in the corresponding Formbean to implement its own data validation logic in the validate method.

is validated by the validation framework, which is two steps:

Configure 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>

The configuration Formbean has the following two methods:

㈠ makes its Formbean class 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>

㈡ makes its Formbean class 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 : Note that the UserForm in the Validation.xml file is the name of the Formbean in the Struts-config.xml file.

Several properties about Arg are as follows:

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

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

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

Position, the value in this ARG replaces which part of the information, the part that needs to be replaced with the {n} flag.

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

For a actionform, it can be used by multiple action, and each action may require different validation fields, 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.