Struts2中action獲取request、response、session的方式

第一種方式,非IoC(Spring中的控制反轉)方式: java

  1. package com.action;
  2. import java.util.Map;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.apache.struts2.ServletActionContext;
  6. import com.opensymphony.xwork2.ActionContext;
  7. import com.opensymphony.xwork2.ActionSupport;

  8. public class BaseAction extends ActionSupport {
  9. private static final long serialVersionUID = 7620009925942346125L;
  10. ActionContext context = ActionContext.getContext();
  11. HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
  12. HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
  13. Map session = context.getSession();
  14. //SessionMap session = (SessionMap) context.get(ActionContext.SESSION);
  15. }

咱們日常所說的session通常是HttpSession,但在struts2中被封裝成了Map類型。

第二種方式,IoC方式: apache

  1. package com.action;
  2. import java.util.Map;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.apache.struts2.dispatcher.SessionMap;
  6. import org.apache.struts2.interceptor.ServletRequestAware;
  7. import org.apache.struts2.interceptor.ServletResponseAware;
  8. import org.apache.struts2.interceptor.SessionAware;
  9. import com.opensymphony.xwork2.ActionContext;
  10. import com.opensymphony.xwork2.ActionSupport;

  11. public class BaseAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
  12. private static final long serialVersionUID = 7620009925942346125L;
  13. ActionContext context = ActionContext.getContext();
  14. HttpServletRequest request;
  15. HttpServletResponse response;
  16. SessionMap session;
  17. //獲取request,response,session方式一,非IoC方式,不用實現SessionAware, ServletRequestAware, ServletResponseAware
  18. //ActionContext context = ActionContext.getContext();
  19. //HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
  20. //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
  21. //Map session = context.getSession();
  22. //SessionMap session = (SessionMap) context.get(ActionContext.SESSION);
  23. //獲取request,response,session方式一,IoC方式,必須實現SessionAware, ServletRequestAware, ServletResponseAware
  24. public void setSession(Map map) {
  25. this.session = (SessionMap) map;
  26. }
  27. public void setServletRequest(HttpServletRequest request) {
  28. this.request = request;
  29. }
  30. public void setServletResponse(HttpServletResponse response) {
  31. this.response = response;
  32. }
  33. }

這樣咱們在寫action時直接繼承這個BaseAction,那些request、response、session之類就能夠正常地用了,good。等我下午有時間反編譯一下別人封裝好的BaseAction,看看是否是這樣搞的,哈哈。

ps:
平時咱們在action中要把值設進session而後在jsp頁面去的話,通常是這樣(struts2不行): 瀏覽器

  1. public String findAllUsers() throws Exception {
  2. List<User> userList = userService.findAllUsers();
  3. HttpSession se = request.getSession();
  4. se.setAttribute("userList", userList);
  5. session.put("userList", userList);
  6. //session.put("aaa", "bbb");
  7. //session.put(key, value);
  8. //request.setAttribute("userList", userList);
  9. return SUCCESS;
  10. }

在struts2中,設進session的話則應該變成這樣了,由於session是一個map類型: session

  1. public String findAllUsers() throws Exception {
  2. List<User> userList = userService.findAllUsers();
  3. session.put("userList", userList);
  4. //request.setAttribute("userList", userList);
  5. return SUCCESS;
  6. }

聽說,若是直接到jsp頁面的話,通常推薦用request而不用session,多人單機同時操做的話保險一點,雖然一個瀏覽器同一時間只有一個session。

在jsp頁面取值的話: jsp

  1. <table class="table_report">
  2. <tr>
  3. <th>用戶ID</th>
  4. <th>用戶名稱</th>
  5. <th>用戶性別</th>
  6. <th>用戶年齡</th>
  7. <th>用戶地址</th>
  8. <th>用戶電話</th>
  9. <th>用戶郵箱</th>
  10. </tr>
  11. <!-- struts2最正規的取值方式 -->
  12. <%-- <s:iterator id="user" value="%{#session.userList}">--%>
  13. <s:iterator id="user" value="#session.userList">
  14. <%-- <s:iterator id="user" value="#request.userList">--%>
  15. <tr>
  16. <td>${user.id}</td>
  17. <td>${user.name}</td>
  18. <td>${user.sex}</td>
  19. <td>${user.age}</td>
  20. <td>${user.address}</td>
  21. <td>${user.phone}</td>
  22. <td>${user.email}</td>
  23. </tr>
  24. </s:iterator>
  25. </table>
  26. <%-- 用完要清空 --%>
  27. <%request.removeAttribute("userList");%>
  28. <%--<%session.removeAttribute("userList");%>--%>  
相關文章
相關標籤/搜索