第一種方式,非IoC(Spring中的控制反轉)方式: java
- package com.action;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
-
- public class BaseAction extends ActionSupport {
- private static final long serialVersionUID = 7620009925942346125L;
-
- ActionContext context = ActionContext.getContext();
- HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
- HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
- Map session = context.getSession();
- //SessionMap session = (SessionMap) context.get(ActionContext.SESSION);
-
- }
咱們日常所說的session通常是HttpSession,但在struts2中被封裝成了Map類型。
第二種方式,IoC方式: apache
- package com.action;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts2.dispatcher.SessionMap;
- import org.apache.struts2.interceptor.ServletRequestAware;
- import org.apache.struts2.interceptor.ServletResponseAware;
- import org.apache.struts2.interceptor.SessionAware;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
-
- public class BaseAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
- private static final long serialVersionUID = 7620009925942346125L;
-
- ActionContext context = ActionContext.getContext();
- HttpServletRequest request;
- HttpServletResponse response;
- SessionMap session;
-
- //獲取request,response,session方式一,非IoC方式,不用實現SessionAware, ServletRequestAware, ServletResponseAware
- //ActionContext context = ActionContext.getContext();
- //HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
- //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
- //Map session = context.getSession();
- //SessionMap session = (SessionMap) context.get(ActionContext.SESSION);
-
- //獲取request,response,session方式一,IoC方式,必須實現SessionAware, ServletRequestAware, ServletResponseAware
- public void setSession(Map map) {
- this.session = (SessionMap) map;
- }
- public void setServletRequest(HttpServletRequest request) {
- this.request = request;
- }
- public void setServletResponse(HttpServletResponse response) {
- this.response = response;
- }
-
- }
這樣咱們在寫action時直接繼承這個BaseAction,那些request、response、session之類就能夠正常地用了,good。等我下午有時間反編譯一下別人封裝好的BaseAction,看看是否是這樣搞的,哈哈。
ps:
平時咱們在action中要把值設進session而後在jsp頁面去的話,通常是這樣(struts2不行): 瀏覽器
- public String findAllUsers() throws Exception {
-
- List<User> userList = userService.findAllUsers();
-
- HttpSession se = request.getSession();
- se.setAttribute("userList", userList);
-
- session.put("userList", userList);
- //session.put("aaa", "bbb");
- //session.put(key, value);
-
- //request.setAttribute("userList", userList);
-
- return SUCCESS;
- }
在struts2中,設進session的話則應該變成這樣了,由於session是一個map類型: session
- public String findAllUsers() throws Exception {
-
- List<User> userList = userService.findAllUsers();
- session.put("userList", userList);
- //request.setAttribute("userList", userList);
-
- return SUCCESS;
- }
聽說,若是直接到jsp頁面的話,通常推薦用request而不用session,多人單機同時操做的話保險一點,雖然一個瀏覽器同一時間只有一個session。
在jsp頁面取值的話: jsp
- <table class="table_report">
- <tr>
- <th>用戶ID</th>
- <th>用戶名稱</th>
- <th>用戶性別</th>
- <th>用戶年齡</th>
- <th>用戶地址</th>
- <th>用戶電話</th>
- <th>用戶郵箱</th>
- </tr>
- <!-- struts2最正規的取值方式 -->
- <%-- <s:iterator id="user" value="%{#session.userList}">--%>
- <s:iterator id="user" value="#session.userList">
- <%-- <s:iterator id="user" value="#request.userList">--%>
- <tr>
- <td>${user.id}</td>
- <td>${user.name}</td>
- <td>${user.sex}</td>
- <td>${user.age}</td>
- <td>${user.address}</td>
- <td>${user.phone}</td>
- <td>${user.email}</td>
- </tr>
- </s:iterator>
- </table>
- <%-- 用完要清空 --%>
- <%request.removeAttribute("userList");%>
- <%--<%session.removeAttribute("userList");%>--%>