Struts2 accessing or adding Request/session/application properties

Source: Internet
Author: User

The first method:

Actioncontext CTX = Actioncontext.getcontext ();      Ctx.put ("R1", list);//Add Attribute      ctx.getsession () to request. Put ("S1", list);//Add attributes to session      

    HttpSession session =null;              Session = Servletactioncontext.getrequest (). getsession ();              if (Session.getattribute ("user") = null) {                  session.removeattribute ("user");              }              Session.setattribute ("user", user);      map<string,object> session = NULL;              Session = Actioncontext.getcontext (). getsession ();              if (Session.containskey ("user")) {                  session.remove ("user");              }                            Session.put ("user", user);  

The second method:

Using the Servletrequestaware interface

    public class LoginAction4 extends Actionsupport implements Servletrequestaware {                    private httpservletrequest request ;          Private HttpSession session;          Private ServletContext Application;                                        Public String Execute () {              request.setattribute ("R1", "R1");              Session.setattribute ("S1", "S1");              Application.setattribute ("A1", "A1");              return SUCCESS;           }                            @Override public          void Setservletrequest (HttpServletRequest request) {              this.request = Request;              This.session = Request.getsession ();              This.application = Session.getservletcontext ();          }                }  

Struts2 accessing the request, session, application properties

The first method:

Using struts tag:

    <s:property value= "#request. R1"/>       <s:property value= "#session. S1"/> <s:property       value= "# Application.a1 "/>   

The second method:

Working with JSP objects

    <%=request.getattribute ("R1")%> <br/>      <%=session.getattribute ("S1")%> <br/>      < %=application.getattribute ("A1")%> <br/>  

Struts2 accessing or adding Request/session/application properties

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.