首页 > 代码库 > struts2访问web资源
struts2访问web资源
通过ActionContext访问
public class TestActionContextAction { public String execute(){ //获取 ActionContext 对象 ActionContext actionContext = ActionContext.getContext(); //application Map<String, Object> applicationMap = actionContext.getApplication(); applicationMap.put("applicationKey", "applicationValue"); //获取属性 Object date = applicationMap.get("date"); System.out.println("date: " + date); //2. session Map<String, Object> sessionMap = actionContext.getSession(); sessionMap.put("sessionKey", "sessionValue"); //清空session /*if(sessionMap instanceof SessionMap){ SessionMap sm = (SessionMap) sessionMap; sm.invalidate(); System.out.println("session 失效了. "); }*/ //request Map<String, Object> requestMap = (Map<String, Object>) actionContext.get("request"); requestMap.put("requestKey", "requestValue"); //parameter Map<String, Parameter> parameters = actionContext.getParameters(); System.out.println(parameters.get("name")); return "success"; } }
Aware方式
public class TestAwareAction implements ApplicationAware, SessionAware, RequestAware ,ParameterAware{ public String execute(){ //1. 向 application 中加入一个属性: applicationKey2 - applicationValue2 application.put("applicationKey", "applicationValue"); //2. 从 application 中读取一个属性 date, 并打印. System.out.println(application.get("date")); request.put("requestKey", "requestValue"); session.put("sessionKey", "sessionValue"); return "success"; } public String save(){ return null; } private Map<String, Object> application; @Override public void setApplication(Map<String, Object> application) { this.application = application; } private Map<String, String[]> parameters; @Override public void setParameters(Map<String, String[]> parameters) { this.parameters = parameters; } private Map<String, Object> request; @Override public void setRequest(Map<String, Object> request) { // TODO Auto-generated method stub this.request = request; } private Map<String, Object> session; @Override public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub this.session = session; } }
jsp页面
application : ${applicationScope.applicationKey } <br><br> session: ${sessionScope.sessionKey } <br><br> request: ${requestScope.requestKey } <br><br> parameters ${parameters.name }
struts.xml
<action name="TestActionContext" class="com.TestActionContextAction"> <result name="success">/actionContext.jsp</result> </action> <action name="TestAware" class="com.TestAwareAction"> <result>/aware.jsp</result> </action>
struts2访问web资源
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。