004 - 取得Map类型的request,session,application - 模块包含 - 默认Action
1. 取得Map类型的request,session,application
?
struts.xml跟普通的action的配置一样
?
?
//GetMapRequestSessionApplicationAction.java
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
?
?
public class GetMapRequestSessionApplicationAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
?
private Map<String,Object> request;
private Map<String,Object> session;
private Map<String,Object> application;
?
public String execute(){
????? request.put("request1", "request values1");
????? session.put("session1", "sesssion values1");
????? application.put("application1", "application values1");
?
????? return SUCCESS;
}
?
//IoC DI 由struts2给我们把request,session,application注入进来
?public void setSession(Map<String, Object> session) {
?????? this.session = session;
}
public void setRequest(Map<String, Object> reqeust) {
?????? this.request = reqeust;
}
public void setApplication(Map<String, Object> application) {
?????? this.application = application;
}
}
?
?
在要用到request,session,application里的东西时,可以在要用到它们的jsp中用下面方式拿到
(value stack可以直接拿到,context stack须在前面加#,如下面方式)
?
//xxx.jsp
?
?<body>
?????? <s:property value="#request.request1"/> <br>
?????? <s:property value="#session.session1"/> <br>
?????? <s:property value="#application.application1"/> <br>
??
?? <s:debug></s:debug><br>
?</body>
?
?
2. 模块包含在struts.xml中加入下面语句即可:
?
<include file="xxx.xml" />
?
xxx.xml是另一个模块的配置
?
?
3. <default-action-ref name="index" />?在struts.xml中配置了<default-action-ref name="index" /> 后,当访问该包下的某个Action时,如果没找到匹配的,则默认跳到index
?
? <struts> ????? <constant name="struts.devMode" value="true" /> ?????? <package name="default" namespace="/" extends="struts-default">???????????????? <default-action-ref name="index" /> ????????????????? <action name="index">??????????????<result>/default.jsp</result> ???????????????? </action>? </package></struts>?