How to obtain parameters of a jsp page in struts2 action

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 = 1321312

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); // result 1321312

 

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 = 1321312 & username = 34343

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

System. out. println (username); // result 1321312

System. out. println (username [0]); // The result is 1321312.


 


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 1321312.

 

From: Black e's column

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.