取得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();
}
}