Analysis of Action using session method in jsp

  • 2021-06-28 13:40:20
  • OfStack

This paper gives an example of Action using session method in jsp.Share it for your reference.Specifically as follows:

In Struts2, if you need to use session in Action, you can get it in two ways

1. Obtained by the method getSession in ActionContext class

2. How Action implements the org.apache.struts2.interceptor.SessionAware interface to operate on session

Let's start with an example of session in action using the first approach

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, session is obtained from ActionContext and an key is placed in session as USER_NAME, value is the content of Test User.

Below is an example of implementing the org.apache.struts2.interceptor.SessionAware interface to operate on session

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 interface SessionAware is implemented.

session can be obtained in both ways, and the functions that can be achieved are the same.

The second way to use session is recommended here because it is easy to do monomer testing. In the second way, only one Map needs to be constructed to perform monomer testing on actionclass.

session may be required for many actions in a project. It may be cumbersome for each action to implement org.apache.struts2.interceptor.SessionAware, so it is recommended that an abstract BaseAction class be used to implement the org.apache.struts2.interceptor.SessionAware interface. All actions need to inherit this BaseAction in the future.

Below is an example of how to use session in JSP.

<%@ 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"/></h1>
</body>
</html>

1 Usually an Object will be placed in session in a project. Es89EN, user have an booleanadmin and String userName. If there is an isAdmin method in user, it can be passed in jsp < s:iftest="#session.user.admin" > To determine if users have administrative privileges, pass < s:property value="#session.user.userName" > Or to get the user name.

I hope that the description in this paper will be helpful to everyone's jsp program design.


Related articles: