The action in JSP uses the session method instance to analyze _jsp programming

Source: Internet
Author: User

In this paper, we analyze the method of action using session in JSP. Share to everyone for your reference. Specifically as follows:

In Struts2, if you need to use session in action, you can get it in the following two ways

1. Through the method in Actioncontext class GetSession get

2.Action implement the Org.apache.struts2.interceptor.SessionAware interface to operate the session

Let's look at an example of a session in action that takes the first approach

Copy Code code 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, we get the session through Actioncontext and place a key in the session user_name, which is the content of test user.

Here is an example of implementing the Org.apache.struts2.interceptor.SessionAware interface to the session operation

Copy Code code 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 interface Sessionaware is implemented.

The above two ways can be a session, can achieve the same function.

It is recommended that the session be used in the second way because it is easy to do the monomer test, and in the second way, only a map can be constructed to test the actionclass.

There may be a lot of action in a project that requires session, if each action is implemented Org.apache.struts2.interceptor.SessionAware This interface may seem cumbersome, so it is recommended to make an abstract Baseaction class to implement the Org.apache.struts2.interceptor.SessionAware interface, all subsequent action as long as the inheritance of this baseaction can be.

Here is an example of how to use the session in a JSP.

Copy Code code as follows:
<%@ page contenttype= "text/html; Charset=utf-8 "%>
<% @page pageencoding= "Utf-8"%>
<% @taglib prefix= "s" uri= "/struts-tags"%>
<title>session test</title>
<body>
</body>

Generally in the project will often put an object in the session, must say User,user has a booleanadmin and string userName, if the user has a ISAdmin method, in the JSP can be through <s: Iftest= "#session. User.admin" > To determine whether the user has administrative rights, through the <s:property value= "#session. User.username" > or to obtain the user name.

I hope this article will help you with the JSP program design.

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.