Three methods for transferring JSP foreground values to the action Background in struts2 and the use of valuestack

Source: Internet
Author: User
There are three methods for passing parameters in the Action receiving form in struts2:

For example, the login form login. jsp:

<Form action = "login" method = "Post" name = "form1">
 

Username: <s: textfieldname = "username"/> <br/>
    Password: <s: passwordname = "password"/> <br/>
                            <S: Submit value = "Submit"/>
    </Form>

1. Define form attributes in the action class. The two attributes must have the same name. Provides the setter and getter methods. You can receive parameters passed in the form.

This method of receiving parameters is convenient and simple, but the structure is not very good. When many parameters are passed in the form, the entire action class is filled with setter and getter methods, the program structure is not very beautiful.

2. encapsulate the parameters passed in the form into a class, and then call the attributes.

For example, encapsulate the parameters to be sent from the login. jsp page (for example, encapsulated in the users class)

Private string username;
Private string password;

Public String GetUserName (){
 Return username;
}
Public void setusername (string username ){
 This. Username = username;
}
Public String GetPassword (){
 Return password;
}
Public void setpassword (string password ){
 This. Password = password;
}

Then define the object of the class in the Action method, as shown in figure

Public class loginaction extends actionsupport {

Private users;

Public users getusers (){

Return users;

}

Public void setusers (users ){

This. Users = users;

}

}

To pass the value through this method, you must also perform some processing on the JSP page. The from1 attribute name in login. jsp should be changed to this:

Login form login. jsp:

<Form action = "login" method = "Post" name = "form1">
 Username: <s: textfieldname = "users. username"/> <br/>
  Password: <s: passwordname = "users. Password"/> <br/>
              <S: Submit value = "Submit"/>
  </Form>

This method is a common method in struts development!

3. Implement the modeldriven interface to receive form data

First, the action class must implement the modeldriven interface and encapsulate the data transmitted from the form. The action class must instantiate the object and override the GetModel () method.

Public class loginaction extends actionsupport implementsmodeldriven <users> {

Private users = new users ();

Public users GetModel (){

Return users;

}

}

4. Use of valuestack

 <Table border = "1" width = "360">
<Caption>
Author: Li Gang's book
</Caption>
<! -- Outputs the books object in valuestack iteratively, where status is the serial number of iteration -->
<S: iterator value = "books" status = "Index">
<S: If test = "# index. Odd = true">
<Tr style = "background-color: # cccccc">
</S: If>
<S: else>
<Tr>
</S: else>
<TD>
Title:
</TD>
<TD>
<S: property/>
</TD>
</Tr>
</S: iterator>
</Table>


Struts2 encapsulates all attribute values in the Struts. valuestack request attribute and can be obtained through request. getattribute ("struts. valuestack. All the attributes of action are encapsulated in the valuestack object, which is similar to map. The attribute names in action can be understood as the value names in valuestack. You can use valuestack. findvalue ("name") to set the value.

 

 

Bookservice. Java

Public class bookservice
{
// Simulate a database
Private string [] books =
New String [] {
"Crazy Java handouts ",
"Lightweight Java EE Enterprise Application practices ",
"Crazy Ajax handouts ",
"Crazy XML handout ",
"Struts 2 authoritative guide"
};

Public String [] getleebooks ()
{
Return books;
}


}

 


Getbooksaction. Java

Import com. opensymphony. xwork2.action;
Import com. opensymphony. xwork2.actioncontext;

Public class getbooksaction implements action
{
Private string [] books;

Public void setbooks (string [] books)
{
This. Books = books;
}

Public String [] getbooks ()
{
Return books;
}

Public String execute () throws exception
{
String user = (string) actioncontext. getcontext (). getsession (). Get ("user ");
If (user! = NULL & User. Equals ("crazyit "))
{
Bookservice BS = new bookservice ();
Setbooks (BS. getleebooks ());
Return success;
}
Else
{
Return login;
}
}
}

 


Showbooks. jsp

<Table border = "1" width = "360">

<%

// Obtain the valuestack object of the encapsulated output information
Valuestack Vs = (valuestack) request. getattribute ("struts. valuestack ");

// Call the finevalue method of valuestack to obtain the value of the books attribute in the action.
String [] books = (string []) vs. findvalue ("books ");

// Iteratively output all book information
For (string book: Books ){
%>


<Tr>
<TD> title: </TD>
<TD> <% =Book%> </TD>
</Tr>
<% }%>


</Table>

Original blog address:

Http://blog.csdn.net/woshixuye/article/details/7782469

Related Article

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.