How to obtain parameters of JSP pages by using actions in struts2

Source: Internet
Author: User

Instance: Now, a parameter named username is passed to the action on the JSP page.

URL: http: // localhost: 8080/studentsystem/role_list.action? Username = qk12333.com

1. Get it through the Get Set Method

Define a variable with the same name in the corresponding action class and generate the set get method. Then, the parameter automatically obtains the value.

String username;

Public String GetUserName ()
{
Return username;
}

Public void setusername (string username)
{
This. Username = username;
}

System. Out. println (username); // The result is qk12333.com.

 

2. Use servletactioncontext to get // import org. Apache. struts2.servletactioncontext;

Httpservletrequest reqeust = servletactioncontext. getrequest ();

String username = reqeust. getparameter ("username"); // string

// URL: http: /localhost: 8080/studentsystem/role_list.action? Username = qk12333.com & username = qk12333.com

String [] username = reqeust. getparametervalues ("username"); // String Array

System. Out. println (username); // The result is qk12333.com.

System. Out. println (username [0]); // The result is qk12333.com.

 

3. Use actioncontext to get // import com. opensymphony. xwork2.actioncontext;

Actioncontext context = actioncontext. getcontext ();
Map Params = context. getparameters ();
String [] username = (string []) Params. Get ("username ");

// Actioncontext obtains an object such as object or string []

System. Out. println (username [0]); // The result is qk12333.com.

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.