struts2(Action,resulst,struts.xml)
1.web.xml中配置:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>配置Struts2框架默认加载的Action包结构
<param-name>actionPackages</param-name>
<param-value>cn.itcast.controller</param-value>
</init-param>
<init-param>配置struts2框架的配置提供者类
<param-name>configProviders</param-name>
<param-value>common.MyConfigurationProvider</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>classpath:struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注意:<init-param>元素可以配置struts2的常量,也可以配置struts.properties文件,如果不配置struts.properties文件,就可以在<init-param>中配置struts2的常量
2.Action类的创建方式:
(1).直接创建一个Java类
(2).实现Action接口,实现默认excute方法
(3).继承ActionSupport类
Action接口中定义了五个常量 一个方法.
常量:
1. public static final String SUCCESS = "success";
2. public static final String NONE = "none"; 如果方法返回值是null
3. public static final String LOGIN = "login";
4. public static final String INPUT = "input"; 如果程序在运行中发现异常.在加载过程中,如果拦截器抛出异常,那么就会要求跳转到input视图.如果input视图存在,就会跳转到input指定的页面.
5. public static final String ERROR = "error";
3.sturts.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--配置struts2框架一些基础的bean,下面是默认的struts2的bean,struts-default.xml文件中默认配置的类-->
<bean class="com.opensymphony.xwork2.ObjectFactory" name="xwork" />
<bean type="com.opensymphony.xwork2.ObjectFactory" name="struts" class="org.apache.struts2.impl.StrutsObjectFactory" />
<package name="user" namespace="/abc" extends="struts-default">
<default-action-ref name="user"></default-action-ref>
<action name="user" >
<result>/success.jsp</result>
</action>
</package>
</struts>
在struts.xml文件中使用<include file="">包含指定的xml文件夹。
Action通配符配置:
可以使用*通配置来简化配置。
在使用时可以使用{1},{2},...来标识是第几个星.
<a href="${pageContext.request.contextPath }/user/customer_add.action">添加客户</a>
<a href="${pageContext.request.contextPath }/user/customer_del.action">删除客户</a>
struts.xml
<action name="customer_*" class="cn.itcast.struts2.demo4.CustomerAction" method="{1}"></action> --- {1}就是第一个* 匹配内容
注意:package包中,可以有一个默认的action配置,<default-action-ref=""/>只能是具体的Action名,不可以是通配符
动态方法调用:
在访问action时可以使用!来指定要指行的方法.
要想使用struts2的动态方法调用需要配置
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
如果它的值为false,不能使用动态方法调用.
actionName!methodname这是动态方法调用的格式。
在开发中不建议使用这种方式.
可以在struts-default.xml文件中查看,默认为true
action的名称搜索顺序
1.获得请求路径的URI,例如url是:
http://server/struts2/path1/path2/path3/test.action
2.首先寻找namespace为/path1/path2/path3的package,
如果存在这个package,则在这个package中寻找名字为test的action,
如果不存在这个package则转步骤3;
3.其次寻找默认namespace
http://server/struts2/test.action即:默认命名空间为"",action名称为"test.action"
如果没有,则转步骤4;
4.寻找根namespace
http://server/struts2/test.action 即:根命名空间为:"/",Action名称为"test.action"
5.寻找namespace为/user/user1
http://server/struts2/path/path1/test.action 即:命名空间为:"/user/user1",Action名称为"test.action"
一直寻找,直到找到为止,没有就跳转input视图;
优先级:1.完全匹配路径
2.默认命名空间匹配
3.根命名空间匹配
4.多级式命名空间匹配
Action配置中默认配置:
<action name="" class="" method="">
<result name="">/success.jsp
class的默认值是 ActionSupport类 com.opensymphony.xwork2.ActionSupport
method的默认值是 execute
result的name属性默认值是:success
result的类型:
chain---------->链 在action内部实现跳转,可以共享action数据.
dispatcher----->请求转发 默认(请求转发到jsp)
redirect------->重定向.
redirectAction----->得定向到某个action。 可以直接访问某个action。
dispatcher与redirect一般用于跳转到页面.
chain与redirectAction用晋城跳转到Action。
stream----响应回的是流. 可以用于下载.
全局result配置:
<package>
<global-results>
<result></restul>
</global-result>
<action></action>
</package>
注意:全局配置对所有Action都有效,不在同一个package,也有效,只有在Action内没有此result名称时,才会使用全局result
4.struts常量配置:
sturts2配置常量的3种方式:
1.struts.xml中配置<constants/>
2.struts.properties,通过key-value方式配置
3.web.xml文件中通过<init-param/>方式配置
5.struts2文件加载顺序:
1.default.properties
2.struts-default.xml
3.struts-plugin.xml
4.struts.xml
5.struts.properties
6.web.xml中的<init-param/>
6.struts获取servlet API的三种方式:
(1).通过ActionContext
ActionContext context=ActionContext.getContext();
context.get();//相当于request.getAttribute();
context.put();//相当于request.setAttribute();
context.getAttribute();//相当于request.getAttribute();
context.getParamters();//相当于request.getParamterMap();
context.getSession();//相当于request.getSession();
context.getApplication();//相当于request.getApplication();
(2).实现接口,使用注入的方式获取servletAPI
当我们自己的Action为实现了ServletRequestAware接口,录Action被加载时。
struts2框架发现这个action实现了接口,就会通过注入的方式将web对象注入到
action指定的方法中。我们在action中就可以得到这个web对象.
(3).使用ServletActionContext
其下获取servletAPI的方法是静态方法,我们在使用时直接使用类名调用.
ServletActionContext是ActionContext的子类.
static PageContext getPageContext()
static HttpServletRequest getRequest()
static HttpServletResponse getResponse()
static ServletContext getServletContext()

浙公网安备 33010602011771号