A point of view on Actionform in struts1.2

Source: Internet
Author: User

Turn from: http://hi.csdn.net/space-62502-do-thread-id-4019.html think summary of Good, also collection study under.

Application of Actionform

(1), creating a form class must inherit one of the four husband classes. Like inheriting Actionform.

(2), each attribute in a form class will correspond to every element one by one in the no form form in the page

For example

A form is:
<form>
<input type= "text" name= "username" ></input>
<input type= "password" name= "password" ></input>
<input type= "text" name= "email" ></input>
</form>

A form class corresponding to it

public class UserForm extends actionform{
Private String username;
private String password;
Private String Email;

The getter and setter methods are omitted below

}

A appaction that refers to the form

<form-beans>
<form-bean name= "UserForm" type= "form. UserForm "></form-bean>
</form-beans>
<action-mappings>
<action path= "/test" type= "action. Testaction "Name=" UserForm "attribute=" uf "scope=" Request "></action>
</action-mappings>

3. The value of the Name property in the action referencing Form-bean is the value of name in Form-bean
4. This UserForm is placed by default in the session, using scope to specify where the form object is stored, and natural attribute is used to give the alias of the form object stored in that range, if not set attribute

The alias of the form object in the range, if not set attribute
The attribute property then uses the value of name as the default

2. The principle and the ordinary HTML form use form to work in a similar, different

Actionservlet when parsing struts-config, there is a property name in resolving to an action,

Then the requestprocessor in Actionservlet will find the corresponding Form-bean based on the value of the name and then create an instance of the corresponding form class.

In our defined storage range, FC will do the following when the form is submitted to the appaction of the action before it reaches FC

1. Locate the action in the corresponding memory-stored configuration object according to the path
2. According to the attribute attribute in action, a corresponding form instance is obtained from the session
3. The form instance calls the Reset method to empty itself
4. Use the values in the form to populate the form instance
5. If you want the form to be validated then the form will call the Validate method to validate it according to the validation rules we have defined

(iii) How struts forms work with form

1. HTML elements defined using Struts's HTML tag library are actually server-side Java code, Java code is compiled code and HTML is interpreted code

, so the HTML elements defined in the HTML tag library using struts are more rigorous, as long as an element or even an attribute is not defined,

The compilation cannot then throw an exception, for example, when defining a form using the Struts HTML tag Library, the Action property is determined at compile time if the commit path defined by the Action property is empty or an error,

Then the server throws a 500 exception when compiling, unlike the normal HTML Form Action property, which is determined at the time of submission.


2. The principle is similar to the normal HTML form using form, except that when the server compiles the form form, it sends a request to the address specified by the action.

To see if it is correct, so this time has actually submitted a form, when the form arrives at FC with a few things done on the third, the difference is that the form and the values in the form will be filled with each other,

This is the mechanism by which struts adds a return to the form

Example:

The form is:

Import the Struts HTML tag library first

Chinese

English

Russian

The form class is: public class UserForm extends actionform{private String username;  Private double salary=1000.00;     Private string[] Lang; Omit getter and Setter methods}

The configuration file is:


<form-beans>
<form-bean name= "UserForm" type= "form. UserForm "></form-bean>
</form-beans>
<action-mappings>
<action path= "/test" type= "action. Testaction "Name=" UserForm "attribute=" UF ">
<forward name= "Success" Path= "/show.do" ></forward>
</action>
<action path= "/show" type= "action. Showaction ">
<forward name= "Success" Path= "/test.do" ></forward>
</action>
</action-mappings>

Experience:
1. Before the page is displayed, the server sends a request to text.do, which is the submission table


2. When the above request to FC will use the value in the form to fill the form and back to the user so that the user sees the page display result is a salary text box has a value of 1000


3. When the user submits the form again, the server will first check that the value of the form element you submitted is the same as the value of the corresponding property of the form in the session, and if the same is used to populate the form directly with the form,

If it is not the same, assign the value of the submitted form element to the corresponding property in the form and then use the form to populate the form

4. This example has a problem, that is, when the user first selected two languages, and then submit, the second time the user a language is not selected, and then submitted, this time echo results unexpectedly, the value of the check box is the first time the user selected value,

The reason is that the check box has an attribute, if the user does not choose anything, then submit the form when the check box is not submitted, if we use GET request submitted, we can obviously see this situation form submitted properties only username and salary two

, these two elements are committed even if no value is present, because of this, when the form arrives at FC, the server only sees the submitted two table cells username and salary, and then checks the properties in the form to check only username and salary two

Without checking Lang, if the username and salary in the form are not the same as the username and salary attributes that correspond in the form, The username and salary values in the form are then assigned to the username and salary properties in the forms.

Then use form to fill the form, if the same, then use the original form to populate the form, in both cases, the Lang attribute in the form will not be checked, not to mention the change,

So Lang used the last value, naturally echo when the value of the check box in the form is the last value


5. There are two ways to solve this problem: After submitting the form, the Form object in the session is deleted, and each commit creates a new form object that is added to the end of the page with a
<%session.removeattribute ("UF")%>
The second workaround is to overwrite the Reset method of the parent class in UserForm, which will be done before the form is populated, so that we can reset the properties we want to reset in reset

(iv) Relationship between form and entity object
Sometimes we just copy the values from the form into the entity object for convenience and then store the entity object in the database.

, which gives us a lot of convenience in programming, but the premise is that the entity object needs to copy the attributes, the form to copy the past properties, and form corresponding to the form elements of the three of them must be one by one corresponding.

This allows us to encapsulate the form in a form and then copy the same values from the entity object into the entity object.

Example:


Entity


public class user{


private String name;
private String password;
private double salary;
Private String address;


Omit getter and Setter methods


}


Form


public class userform{


private String name;
private String password;
Private String salary;


Omit getter and Setter methods


}

Form:
<form>


<input type= "text" name= "name" ></input>
<input type= "password" name= "password" ></input>
<input type= "text" name= "salary" ></input>


</form>

1. Assign values from the form to UserForm


2. Copy the values in the UserForm to the user object:
The following statement is done in a method of action, so the form can be used directly
Beanutils.copyproperties (User,form);


3. Store the user object in the database

A point of view on Actionform in struts1.2

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.