Methods for transferring values from an action to a JSP page

Source: Internet
Author: User

1. Generally, define a member variable in the action, and then provide the get/Set Method for this member variable. The value of this variable can be obtained on the JSP page.

1) define member variables in action

 
// Define a member variable private string message; // provides the get/Set Method Public String getmessage () {return message;} public void setmessage (string message) {This. message = message ;}

2) set the value on the JSP page.

 
$ {Message} or <s: property value = "message"/>

2. But the number of defined member variables is too large.CodeIt takes a long time. In this case, you can use some servlet APIs to access values: httpservletrequest, httpsession, and servletcontext. Struts2 encapsulates the three objects with map, so that we can use the map object to access data.

1) store values in actions

 
Actioncontext = actioncontext. getcontext (); // get httpservletrequestmap <string, Object> request = (MAP) actioncontext. get ("request"); Request. put ("A", "A is in request"); // get httpsession // Map <string, Object> session = (MAP) actioncontext. get ("session"); Map <string, Object> session = actioncontext. getsession (); Session. put ("B", "B is in session"); // get servletcontext // Map <string, Object> application = (MAP) actioncontext. get ("application"); Map <string, Object> application = actioncontext. getapplication (); application. put ("C", "C is in application ");

2) set the value on the JSP page.

 
$ {A }$ {B }$ {c} or $ {requestscope. A }$ {sessionscope. B }$ {applicationscope. c}

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.