一、在web.xml文件配置Struts2过滤器
<!-- 如果是2.1.3之前的版本,用org.apache.struts2.dispatcher.FilterDispatcher,否则,用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。 从Struts2.1.3开始,将废弃ActionContextCleanUp过滤器,而在StrutsPrepareAndExecuteFilter过滤器中包含相应的功能。 三个初始化参数: 1、config参数:指定要加载的配置文件。逗号分割。 2、actionPackages参数:指定Action类所在的包空间。逗号分割。 3、configProviders参数:自定义配置文件提供者,需要实现ConfigurationProvider接口类。逗号分割。 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>com.cjm.web.action</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
二、Struts2框架搜索Struts2常量的顺序:
1. struts-default.xml
2. struts-plugin.xml
3. struts.xml
4. struts.properties
5. web.xml
三、页面取值
取action里属性的值:
<s:property value="message"/>
取application里的值:
ActionContext.getContext().getApplication().put("n1", obj);
<s:property value="#application.n1"/>
取session里的值:
ActionContext.getContext().getSession().put("n2", obj);
<s:property value="#session.n2"/>
取URL传过来的参数:
http://localhost:8888/struts2/helloWorld!show.action?name=cjm
<s:property value="#parameters.name"/>
取request的属性值:
ServletActionContext.getRequest().setAttribute("n1", obj);
<s:property value="#request.n1"/>
取ActionContext里的值:
ActionContext.getContext().put("n4", obj);
<s:property value="#attr.n4"/>
取在页面用<s:set/>赋的值:
<s:set name="uid" value="'cjm'"/> ————此处的值要用单引号括起来,否则,将当变量处理
<s:property value="#uid"/>
<s:property value="#attr.uid"/>
在控件中取值:
<s:textfield name="name" value="%{#parameters._KEEP_MULTI_ENTITY}"/>
<s:textfield name="name" value="%{#parameters.name}"/>
四、配置
1、配置包:一个包可以继承其它包。抽象包不能包含Action定义。父包应该在子包前面定义。
2、配置命名空间:默认的命名空间是"",命名空间必须以“/”开头。命名空间只有一个级别。
3、配置Action:
当配置Action没有指定class属性时,系统自动使用ActionSupport类作为默认的Action处理类。
1)、动态方法调用
调用格式:ActionName!methodName.action
需要设置struts.enable.DynamicMethodInvocation为true
2)、为action元素指定method属性
<action name="login" class="com.cjm.LoginAction" method="login">
</action>
3)、使用通配符
<action name="*Action" class="com.cjm.LoginAction" method="{1}">
<action name="*_*" class="com.cjm.{1}" method="{2}">
<action name="*">
<result>/{1}.jsp</result>
</action>
4)、默认Action
<default-action-ref name="defaultAction">
4、配置结果:
1)、Struts2默认的结果类型是dispatcher,结果默认的name属性值为success
<result name="success" type="dispatcher">
<param name="location">/login.jsp</param>
<param name="charSet">GBK</param>
</result>
2)、redirect-action结果类型:
<result name="success" type="dispatcher">
<param name="actionName">show</param>
<param name="namespace">/sales</param>
</result>
3)、在结果里使用OGNL表达式
格式:${属性名},属性名就是对应Action实例里的属性。
<result>/${currentSkill.name}.jsp</result>
4)、全局结果
<global-results>
<result name="success">/target.jsp</result>
</global-results>
5、配置异常:
1)、全局异常
<global-exception-mappings>
<exception-mapping exception="java.sql.SQLException" result="sql"/>
<exception-mapping exception="java.sql.RuntimeException" result="runtime"/>
</global-exception-mappings>
2)、输出异常
<s:property value="exception"/> 输出异常对象本身
<s:property value="exception.message"/>
<s:property value="exceptionStack"/> 输出异常堆栈信息
6、结果类型的用法
1)、stream
public class HelloWorldAction extends BaseAction { private InputStream inputStream; public InputStream getInputStream() { return inputStream; } public String stream()throws Exception{ inputStream = new StringBufferInputStream("test result type of stream!"); return "stream"; } }
<package name="test" extends="struts-default" namespace="/test"> <action name="helloWorld" class="com.cjm.web.action.HelloWorldAction"> <result name="stream" type="stream"> <param name="contentType">text/html</param> <param name="inputName">inputStream</param> </result> </action> </package>
7、Struts2核心对象
1)、ServletActionContext
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ServletContext servletContext = ServletActionContext.getServletContext();
ValueStack valueStack = ServletActionContext.getValueStack(request);
PageContext pageContext = ServletActionContext.getPageContext();
ActionContext actionContext = ServletActionContext.getContext();
2)、ActionContext
ActionContext.getContext().getActionInvocation(); —— return ActionInvocation
ActionContext.getContext().getApplication(); —— return Map
ActionContext.getContext().getSession(); —— return Map
ActionContext.getContext().getParameters(); —— return Map
ActionContext.getContext().getValueStack(); —— return ValueStack
ActionContext.getContext().getContainer(); —— return Container
ActionContext.getContext().get(key);
3)、跟Servlet交互的接口
ParameterAware
ServletRequestAware
ServletResponseAware
SessionAware
ApplicationAware
CookiesAware