1、web.xml配置:
<filter>
<filter-name>Struts2</filter-name>
<filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、struts.xml中写的是要处理的action
<package name="cheliang" namespace="/cheliang" extends="struts-default">
<action name="cheliang" class="com.java1234.action.CheLiangAction">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
也可以通过include分模块配置:
<struts>
<include file="cheliang.xml"></include> //在 cheliang.xml中配置action
<include file="zichan.xml"></include>
</struts>
--通配符:
<action name="grade_*" class="com.java1234.action.GradeAction" method="{1}">
<result name="success">success.jsp</result>
</action>//{1}就表示grade_*后面的*
动态方法调用:在struts.xml中配置:
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<action name="student" class="com.java1234.action.StudentAction">
<result name="success">success.jsp</result>
</action>
--这样请求就可以写成student!list进行调用(这样调用现在不提倡)
Struts2生成的action实例不是单例
result配置:
type默认是dispatcher内部转发;redirect(重定向);chain(链条);redirectAction(重定向到action)
result全局配置:
<global-results>
<result name="error">error.jsp</result>
</global-results>
拦截器:
<interceptors>
<interceptor name="myInterceptor" class="com.java1234.interceptor.MyInterceptor"></interceptor>
</interceptors>
<action name="hello" class="com.java1234.action.HelloAction">
<result name="success">success.jsp</result>
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
还有一种写法 :
<interceptors>
<interceptor name="myInterceptor" class="com.java1234.interceptor.MyInterceptor"></interceptor>
<interceptor name="loginInterceptor" class="com.java1234.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"></default-interceptor-ref>
这样的话在action中就不需要引用了
在默认的execute()方法中获取上下文:
ActionContext actionContext=ActionContext.getContext();
通过这个actionContext可以获取session ,application
Ognl是对象图导航语言的缩写,它是一种强大的表达式语言,Ognl访问ValueStack数据:<s:property value="name"/>
Ognl访问ActionContext的数据,要访问某个范围的数据要用#
#parameters 请求参数 request.getParameter()
#request 请求作用域的数据 request.getAttribute()
#session 会话作用域的数据 session.getAttribute()
#application 应用程序作用域的数据
#attr 顺序查找值
ognl访问javaBean对象:<s:property value="student.name"/>
<s:property value="student.age"/><br/>
ognl访问List集合:<s:property value="students[0].name"/>
<s:property value="students[0].age"/><br/>
<s:property value="students[1].name"/>
<s:property value="students[1].age"/><br/>
ognl访问Map:<s:property value="studentMap['goodStudent'].name"/>
<s:property value="studentMap['goodStudent'].age"/><br/>
<s:property value="studentMap['badStudent'].name"/>
<s:property value="studentMap['badStudent'].age"/><br/>