Strut.xml
<?xml version= "1.0" encoding= "UTF-8"? ><!DOCTYPE Struts public"-//apache software foundation//dtd Struts Configuration 2.3//en" "Http://struts.apache.org/dtds/struts-2.3.dtd" > <struts> <constant name= "struts.action.extension" value= "Action,do," ></constant> < PackageName= "Default" namespace= "/"extends= "Struts-default" > <action name= "Login-ui" > <result>/login.jsp</result> < /action> <action name= "User-login"class= "User.action.UserAction" > <result name= "login-success" >/login-success.jsp</result> </ action> <action name= "Logout"class= "User.action.UserAction" method= "logout" > <result name= "logout-success" >/login.jsp</result> </action> </ Package></struts>
The business implementation in action:
Packageuser.action;ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;ImportOrg.apache.struts2.ServletActionContext;ImportOrg.apache.struts2.interceptor.ApplicationAware;ImportOrg.apache.struts2.interceptor.SessionAware; Public classUseractionImplementssessionaware,applicationaware{PrivateString username; Public voidSetusername (String username) { This. Username =username; } PublicString Execute () {//storing user information in session field//1: Get session Implementation Sessionaware interface//2: Get login information: By adding a setter method to the action//3: Storing user information in session fieldSession.put ("username", username); //Online visitors +1//1: Get the current number of people online: from application. Integer count= (integer) application.get ("Count"); if(count==NULL) {Count=0; } //2: Current people +1count++; Application.put ("Count", Count); return"Login-success"; } PublicString Logout () {//1. Number of people online -1Integer count= (integer) application.get ("Count"); if(Count! =NULL&& count>0) {Count--; Application.put ("Count", Count); } //2.session failure: Get to HttpServletRequest with Servletactioncontext. Let's use HttpServletRequest later .//get to Session:request.getSession () Call: Invalidate ()HttpServletRequest request =servletactioncontext.getrequest (); Request.getsession (). invalidate (); return"Logout-success"; } PrivateMap<string,object>session; Public voidSetsession (map<string, object>session) { This. session=session; } PrivateMap<string,object>Application; Public voidSetapplication (map<string, object>application) { This. application=Application; }}
The use of knowledge is: To obtain Web resources:
In Action, you can access the HttpSession, HttpServletRequest, HttpServletResponse, and other resources of the web in the following ways
1: How to access the Servlet API decoupling
2: How to access the Servlet API coupling
-------------Actioncontext---------
1): Actioncontext is the context object for action execution, and all objects required by action execution are saved in Actioncontext, including parameters, request, session, application, etc. .
2): Get HttpSession corresponding Map object:
Public Map getsession ()
3): Get ServletContext corresponding Map object:
Public Map getapplication ()
4): Gets the Map object corresponding to the request parameter:
Public Map getparameters ()
5): Get httpservletrequest corresponding Map object:
public object get (object key):
A method similar to Getrequest () is not provided in the Actioncontext class to obtain a httpservletrequest corresponding Map object. To get the map object for the HttpServletRequest, you can do so by passing the request parameter for the Get () method
------------------decoupling and the implementation interface of the way---------
The action class allows the STRUTS2 framework to inject parameters, request, session, and application corresponding Map objects at runtime to the action instance by implementing some specific interfaces:
-----------native servlet API servletactioncontent in a coupled form-------------
Common methods:
1: Get HttpServletRequest object directly:
Servletactioncontext.getrequest ()
2: Get HttpSession object directly
Servletactioncontext.getrequest (). GetSession ()
3: Get ServletContext object directly
Servletactioncontext.getservletcontext ()
How many people are Struts2 on the website?