熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Struts2中訪問web元素

2022-06-13   來源: Java開源技術 

  取得Map類型requestsessionapplication真實類型 HttpServletRequest HttpSession ServletContext的引用:

  前三者依賴於容器

  前三者IOC (只用這種)

  後三者依賴於容器

  後三者IOC

  一          方法一ActionContext方式

  一般在Action類的構造方法或execute()方法中獲取

  [java]

  public class LoginAction extends ActionSupport {

  private Map request;

  private Map session;

  private Map application;

  public LoginAction() {

  request = (Map)ActionContextgetContext()get(request);

  session = ActionContextgetContext()getSession();

  application = ActionContextgetContext()getApplication();

  }

  public String execute() {

  requestput(r r);

  sessionput(s s);

  applicationput(a a);

  return SUCCESS;

  }

  }

  public class LoginAction extends ActionSupport {

  private Map request;

  private Map session;

  private Map application;

  public LoginAction() {

  request = (Map)ActionContextgetContext()get(request);

  session = ActionContextgetContext()getSession();

  application = ActionContextgetContext()getApplication();

  }

  public String execute() {

  requestput(r r);

  sessionput(s s);

  applicationput(a a);

  return SUCCESS;

  }

  }

  然後在Jsp頁面中獲取相關web元素

  [html]

  <body>

  User Login Success!

  <br />

  <s:property value=#requestr/> | <%=requestgetAttribute(r) %> <br />

  <s:property value=#sessions/> | <%=sessiongetAttribute(s) %> <br />

  <s:property value=#applicationa/> | <%=applicationgetAttribute(a) %> <br />

  <s:property value=#attra/><br />

  <s:property value=#attrs/><br />

  <s:property value=#attrr/><br />

  <s:debug></s:debug>

  <br />

  </body>

  <body>

  User Login Success!

  <br />

  <s:property value=#requestr/> | <%=requestgetAttribute(r) %> <br />

  <s:property value=#sessions/> | <%=sessiongetAttribute(s) %> <br />

  <s:property value=#applicationa/> | <%=applicationgetAttribute(a) %> <br />

  <s:property value=#attra/><br />

  <s:property value=#attrs/><br />

  <s:property value=#attrr/><br />

  <s:debug></s:debug>

  <br />

  </body>

  注因為requestsessionapplication對象Struts將在放入到Action Context中

  因此需要使用#key來訪問對象們

  後面的是java腳本代碼的訪問方式

  一          方式二Ioc(控制反轉)—推薦使用

  讓Action類實現RequestAwareSessionAwareApplicationAware接口然後重寫他們的set方法(setRequestsetSessionsetApplication)通過依賴注入控制反轉(原來自己控制現在由別人來控制值)

  [java]

  import orgapachestrutsinterceptorApplicationAware;

  import orgapachestrutsinterceptorRequestAware;

  import orgapachestrutsinterceptorSessionAware;

  import comopensymphonyxworkActionSupport;

  public class LoginAction extends ActionSupport implements RequestAwareSessionAware ApplicationAware {

  private Map<String Object> request;

  private Map<String Object> session;

  private Map<String Object> application;

  //DI dependency injection依賴注入

  //IoC inverse of control控制反轉

  public String execute() {

  requestput(r r);

  sessionput(s s);

  applicationput(a a);

  return SUCCESS;

  }

  @Override

  public void setRequest(Map<String Object> request) {

  thisrequest = request;

  }

  @Override

  public void setSession(Map<String Object> session) {

  thissession = session;

  }

  @Override

  public void setApplication(Map<String Object> application) {

  thisapplication = application;

  }

  }

  import orgapachestrutsinterceptorApplicationAware;

  import orgapachestrutsinterceptorRequestAware;

  import orgapachestrutsinterceptorSessionAware;

  import comopensymphonyxworkActionSupport;

  public class LoginAction extends ActionSupport implements RequestAwareSessionAware ApplicationAware {

  private Map<String Object> request;

  private Map<String Object> session;

  private Map<String Object> application;

  //DI dependency injection依賴注入

  //IoC inverse of control控制反轉

  public String execute() {

  requestput(r r);

  sessionput(s s);

  applicationput(a a);

  return SUCCESS;

  }

  @Override

  public void setRequest(Map<String Object> request) {

  thisrequest = request;

  }

  @Override

  public void setSession(Map<String Object> session) {

  thissession = session;

  }

  @Override

  public void setApplication(Map<String Object> application) {

  thisapplication = application;

  }

  }

  在視圖(JSP)頁面中獲取相關對象同方式一

  一          方式三獲取原類型

  獲取是的HttpServletRequest/HttpSession/ServletContext

  [java]

  public class LoginAction extends ActionSupport {

  private HttpServletRequest request;

  private HttpSession session;

  private ServletContext application;

  public LoginAction() {

  request = ServletActionContextgetRequest();

  session = requestgetSession();

  application = sessiongetServletContext();

  }

  public String execute() {

  requestsetAttribute(r r);

  sessionsetAttribute(s s);

  applicationsetAttribute(a a);

  return SUCCESS;

  }

  }

  public class LoginAction extends ActionSupport {

  private HttpServletRequest request;

  private HttpSession session;

  private ServletContext application;

  public LoginAction() {

  request = ServletActionContextgetRequest();

  session = requestgetSession();

  application = sessiongetServletContext();

  }

  public String execute() {

  requestsetAttribute(r r);

  sessionsetAttribute(s s);

  applicationsetAttribute(a a);

  return SUCCESS;

  }

  }

  一          方式四獲取原類型控制反轉

  首先需要Action實現orgapachestrutsinterceptorServletRequestAware接口然後重寫setServletRequest()方法獲取HttpServletRequest對象再通過HttpServletRequest對象取昨HttpSession和ServletContext對象

  [java]

  import javaxservletServletContext;

  import javaxservlethttpHttpServletRequest;

  import javaxservlethttpHttpSession;

  import orgapachestrutsinterceptorServletRequestAware;

  import comopensymphonyxworkActionSupport;

  public class LoginAction extends ActionSupport implements ServletRequestAware {

  private HttpServletRequest request;

  private HttpSession session;

  private ServletContext application;

  public String execute() {

  requestsetAttribute(r r);

  sessionsetAttribute(s s);

  applicationsetAttribute(a a);

  return SUCCESS;

  }

  @Override

  public void setServletRequest(HttpServletRequest request) {

  thisrequest = request;

  thissession = requestgetSession();

  thisapplication = sessiongetServletContext();

  }

  }

  import javaxservletServletContext;

  import javaxservlethttpHttpServletRequest;

  import javaxservlethttpHttpSession;

  import orgapachestrutsinterceptorServletRequestAware;

  import comopensymphonyxworkActionSupport;

  public class LoginAction extends ActionSupport implements ServletRequestAware {

  private HttpServletRequest request;

  private HttpSession session;

  private ServletContext application;

  public String execute() {

  requestsetAttribute(r r);

  sessionsetAttribute(s s);

  applicationsetAttribute(a a);

  return SUCCESS;

  }

  @Override

  public void setServletRequest(HttpServletRequest request) {

  thisrequest = request;

  thissession = requestgetSession();

  thisapplication = sessiongetServletContext();

  }

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28170.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.