struts2--Access Servlet

Struts2 access to Servlet:
1. Use ActionContext to
get the class object through the getContext() method.
Common methods:
put (): add value to the request field.
get(): Get the value from the request field.
getApplication(): Get the Application domain object, and the result is a map collection.
getSession(): Get the session domain object, the range map collection.
Get form data:
create page, write form, submit form to action, get form data in action
//Create ActionContext object
ActionContext context = ActionContext.getContext();
//Get form data
Map<String,Object> map = context. getParameters();
//Get the value of the data to be submitted, the return is an array type
String[] username= map.get("username");

Set values ​​to the request, session, and Application domain objects:
request: context.put("key","value");
session:
first get the session object, and then put the value: context.getSession().put( "key","value");
In application:
first get the Application object, and then put the value: context.getApplication().put("key","value");

Get the value in the domain object in jsp: ${requestScope.key}, ${sessionScope.key}, ${applicationScope.key}

2. Implement the interface: ServletContextAware, ServletRequestAware, and ServletResponseAware
implement interface methods, and declare the interface parameters as class attributes ServletContext context; HttpServletRequest request; HttpServletResponse response; can be used in the execute method.
Set values ​​to three domain objects in the execute method:
request.setAttribute("key","value");
request.getSession().setAttribute("key","value");
context.setAttribute("key" , "value");
and then get the value in the page.

3. Use the ServletActionContext class to operate directly:
HttpServletRequest request= ServletActionContext.getRequest();
ServletContext context = servletActionContext.getServletContext();
request.setAttribute("key","value");
request.getSession().setAttribute("key ","value");
context.setAttribute("key","value");
and then get the value in the page.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326540788&siteId=291194637