In jsp, Action uses the session method for instance analysis and session instance analysis.
This document analyzes the use of the session method for Action in jsp. Share it with you for your reference. The details are as follows:
In Struts2, if you need to use session in Action, you can obtain
1. Get it through the getSession method in ActionContext class
2. The Action implements the org. apache. struts2.interceptor. SessionAware interface to operate sessions.
Next, let's look at an example of using the first method to get a session in action.
Copy codeThe Code is as follows: package s2.ex. action;
Import java. util. Map;
Import com. opensymphony. xwork2.ActionContext;
Import com. opensymphony. xwork2.ActionSupport;
Public class SessionTestActionextends ActionSupport {
Public String execute (){
ActionContext actionContext = ActionContext. getContext ();
Map session = actionContext. getSession ();
Session. put ("USER_NAME", "Test User ");
Return SUCCESS;
}
}
In this example, the session is obtained through ActionContext and a key USER_NAME with the value of Test User is placed in the session.
The following is an example of a session operation that implements the org. apache. struts2.interceptor. SessionAware interface.
Copy codeThe Code is as follows: package s2.ex. action;
Import java. util. Map;
Import org. apache. struts2.interceptor. SessionAware;
Import com. opensymphony. xwork2.ActionSupport;
Public class SessionTest1Action extends ActionSupport implements SessionAware {
Private Map session;
Publicvoid setSession (Map session ){
This. session = session;
}
Public String execute (){
This. session. put ("USER_NAME", "Test User 1 ");
Return SUCCESS;
}
}
In this example, the setSession method in the SessionAware interface is implemented.
The above two methods can both get the session and achieve the same functions.
Here we recommend that you use the second method to use the session, because it is easy to perform a standalone test. In the second method, you only need to construct a Map to perform a standalone test on the actionclass.
In a project, many actions may need to use session. If each action implements org. apache. struts2.interceptor. sessionAware may be difficult. Therefore, we recommend that you use an abstract BaseAction class to implement org. apache. struts2.interceptor. sessionAware interface. All subsequent actions only need to inherit this BaseAction.
The following is an example of how to use session in JSP.
Copy codeThe Code is as follows: <% @ page contentType = "text/html; charset = UTF-8" %>
<% @ Page pageEncoding = "UTF-8" %>
<% @ Taglib prefix = "s" uri = "/struts-tags" %>
<Html>
<Head>
<Title> Session Test </title>
</Head>
<Body>
<H1> <s: property value = "# session. USER_NAME"/> </Body>
</Html>
Generally, an Object is usually placed in the session in the project, which must be a user. The user has a booleanadmin and String userName. if the user has an isAdmin method, in jsp, you can use <s: iftest = "# session. user. admin "> to determine whether a user has administrative permissions, use <s: property value =" # session. user. userName "> or get the user name.
I hope this article will help you with jsp program design.