Struts2总结之第二章
关于Action的配置分析
- package 作用: 是用于声明一个包。用于管理action。
属性 | 作用 |
---|---|
name | 它用于声明一个包名,包名不能重复,也就是它是唯一的。 |
namespace | 它与action标签的name属性合并确定了一个唯一访问action的路径。(/hello) |
extends | 它代表继承的包名。(通常会继承struts-default.xml文件下的name="struts-default"的包) |
abstrace | 它可以取值为true/false,如果为true,代表这个包是用于被继承的。 |
-
action作用:用于声明 一个action
属性 作用 name 就是action的一个名称,它是唯一的(在同包内) 它与package中的namespace确定了访问action的路径。 class Action类的全名 method 要访问的Action类中的方法的名称,方法无参数 ,返回值为String. -
result 用于确定返回结果类型
属性 作用 name 它与action中的方法返回值做对比,确定跳转路径。 -
关于action配置的默认值问题
<package namespace=“默认值”>
- namespace的默认值是 “”
<action class=“默认值” method=“默认值”>
- class的默认值是 com.opensymphony.xwork2.ActionSupport
- method的默认值是 execute
<result name=“默认值”>
-
name的默认值是 “success”
-
.关于访问action的路径问题
-
当我们现在的action的配置是:
<package name="p1" extends="struts-default" namespace="/"> <action name="hello" class="com.syj.action.HelloAction"> <result >/hello.jsp</result> </action> </package>
-
当我们输入http://localhost/Struts2/a/b/c/hello的时候我们也可以访问到指定的action
-
原因:
struts2中的action被访问时,它会首先查找
- namespace="/a/b/c" action的name=hello 没有.
- namespace="/a/b action的name=hello 没有
- namespace="/a" action的name=hello 没有
- namespace="/" action的name=hello 查找到了.
如果最后也查找不到,会报404错误.
-
默认的action。作用:处理其它action处理不了的路径。
我们可以自己创建一个action,将刚创建的action作为我们默认的action刚输入的路径不存在或者错误的时候Struts2就能帮助我们展示默认action 指定的跳转页面
-
默认action的配置
在package标签中添加:<default-action-ref name=“action的名称” />
配置了这个,当访问的路径,其它的action处理不了时,就会执行name指定的名称的action。
-
例如:
<package name="p1" extends="struts-default" namespace="/"> <!-- 配置默认的action --> <default-action-ref name="default"></default-action-ref> <action name="hello" class="com.syj.action.HelloAction"> <result >/hello.jsp</result> </action> <!-- 默认的action --> <action name="default" class="com.syj.action.DefaultAction"> <result >/default.jsp</result> </action> </package>
-
-
action默认的处理类
-
在action配置时,如果class不写。默认情况下是 com.opensymphony.xwork2.ActionSupport。
如果method不写,默认情况下是execute() 方法
-
当设置为:
<default-class-ref class="com.syj.action.DefaultAction"></default-class-ref> <action name="good" > <result >/index.jsp</result> </action>
当访问http://localhost/Struts2/hello的时候,名(name)为good的action的class就不再是ActionSupport类而是com.syj.action.DefaultAction类,在action中不写method是方法默认去找execute ,result 的name默认是success
<default-class-ref class="com.syj.action.DefaultAction"></default-class-ref> //这一段告诉本包下的action不写class就默认是DefaultAction类
-
action的常量配置
-
Struts2的常量都生命在default.properties里
-
常量可以进行设置的位置
-
struts.xml(应用最多)
格式:
<constant name="常量名称" value="常量值"></constant>
-
struts.properties(基本不使用)
-
web.xml(了解)
配置常量,是使用StrutsPrepareAndExecuteFilter的初始化参数来配置的.
<!-- 设置后缀 --> <init-param> <param-name>struts.action.extension</param-name> <param-value>do,,</param-value> </init-param>
-
-
常用常量
struts.action.extension=action,, //这个常量用于指定strus2框架默认拦截的后缀名. <constant name="struts.i18n.encoding" value="UTF-8"/> //相当于request.setCharacterEncoding("UTF-8"); 解决post请求乱码 <constant name="struts.serve.static.browserCache" value="false"/> //false不缓存,true浏览器会缓存静态内容,产品环境设置true、开发环境设置false <constant name="struts.devMode" value="true" /> //提供详细报错页面,修改struts.xml后不需要重启服务器 (要求)
struts.xml文件的分离:
-
为了方便开发,将功能进行模块化划分,例如order订单、product产品等,在各个模块的struts.xml中为了方便我们可以将struts.xml进行分离开发,最后引入到总的struts.xml中
<include file="order.xml"/>导入其它的配置文件。 <include file="product.xml"/>导入其它的配置文件。
Action的创建和方法访问
Action的创建
Action的创建一共有三种方式:
-
1、创建一个POJO类.
- 简单的Java对象(Plain Old Java Objects),指的是没有实现任何接口,没有继承任何父类(除了Object)
- 优点:无耦合。
- 缺点:所以工作都要自己实现。
- 在struts2框架底层是通过反射来操作:
- struts2框架 读取struts.xml 获得 完整Action类名
- obj = Class.forName(“完整类名”).newInstance();
- Method m = Class.forName(“完整类名”).getMethod(“execute”); m.invoke(obj); 通过反射 执行 execute方法
-
2、创建一个类,实现Action接口. com.opensymphony.xwork2.Action
-
优点:耦合低。提供了五种结果视图,定义了一个行为方法。
-
缺点:所以工作都要自己实现。
-
五种视图
public static final String SUCCESS = "success"; // 数据处理成功 (成功页面) public static final String NONE = "none"; // 页面不跳转 return null; 效果一样 public static final String ERROR = "error"; // 数据处理发送错误 (错误页面) public static final String INPUT = "input"; // 用户输入数据有误,通常用于表单数据校验 (输入页面) public static final String LOGIN = "login"; // 主要权限认证 (登陆页面)
-
-
3、创建一个类,继承自ActionSupport类. com.opensymphony.xwork2.ActionSupport(ActionSupport类实现了Action接口)
- 优点:表单校验、错误信息设置、读取国际化信息 三个功能都支持.
- 缺点:耦合度高。
- 在开发中,第三种会使用的比较多.
关于action的访问:
-
1、通过设置method的值,来确定访问action类中的哪一个方法.
<!-- 当访问的是book_add,这时就会调用bookAction类中的add方法 --> <action name="book_add" class="com.syj.action.bookAction" method="add"> </action> <!-- 当访问的是book_update,这时就会调用bookAction类中的update方法 --> <action name="book_update" class="com.syj.action.bookAction" method="update"> </action> <action name="book_search" class="com.syj.action.bookAction" method="search"> </action> <action name="book_delete" class="com.syj.action.bookAction" method="delete"> </action>
-
2、使用通配符来简化配置
-
在struts.xml文件中
<action name="*_*" class="com.syj.action.{1}Action" method="{2}">
-
在jsp页面上
//book.jsp <a href="${pageContext.request.contextPath}/book_add">添加图书</a><br> <a href="${pageContext.request.contextPath}/book_update">更新图书</a><br> <a href="${pageContext.request.contextPath}/book_search">查找图书</a><br> <a href="${pageContext.request.contextPath}/book_delete">删除图书</a><br> //product.jsp <a href="${pageContext.request.contextPath}/product_add">添加产品</a><br> <a href="${pageContext.request.contextPath}/product_update">更新产品</a><br> <a href="${pageContext.request.contextPath}/product_search">查找产品</a><br> <a href="${pageContext.request.contextPath}/product_delete">删除产品</a><br>
解析:
当访问book add时,这时的路径是 book_add,那么对于struts.xml文件中.
第一个星就是 book
第二个星就是 add对于{1}Action---->bookAction
对于method={2}—>method=add -
使用通配符来配置注意事项:
1.必须定义一个统一的命名规范。
2.不建议使用过多的通配符,阅读不方便。
-
-
动态方法调用 (了解)
在struts.xml文件中
<action name="book" class="cn.itcast.action.bookAction"></action>
访问时路径: http://localhost/struts2_day01_2/book!add
就访问到了bookAction类中的add方法。
对于book!add 这就是动态方法调用。注意:struts2框架支持动态方法调用,是因为在default.properties配置文件中设置了动态方法调用为true.
struts.enable.DynamicMethodInvocation = true
Struts2中的serveltAPI介绍
对于struts2框架,不建议直接使用servlet api,但是struts2框架依旧为我们提供了获取serveltAPI的方法
一共有三方式:
-
第一种:通过ActionContext来获取
-
第一步:获取一个ActionContext对象。
ActionContext context=ActionContext.getContext();
-
第二步:获取servlet api
注意:通过ActionContext获取的不是真正的Servlet api,而是一个Map集合。
1.context.getApplication() 2.context.getSession() 3.context.getParameter();---得到的就相当于request.getParameterMap() 4.context.put(String,Object) 相当于request.setAttribute(String,String);
-
-
第二种:注入方式获取(这种方式是真正的获取到了servlet api)
-
1、要求action类必须实现指定接口。
ServletContextAware : 注入ServletContext对象 ServletRequestAware : 注入 request对象 ServletResponseAware : 注入response对象
-
2、声明一个属性,用于接受传递过来的request对象。
private HttpServletRequest request;
-
3、声明一个web对象之后,使用接口中的方法的参数对声明的web对象赋值.
public void setServletRequest(HttpServletRequest request) { this.request = request; }
-
4、分析是如何将request注入到自定义中的requst中的
-
是使用struts2中的一个interceptor完成的.
-
<interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
-
if (action instanceof ServletRequestAware) { //判断action是否实现了ServletRequestAware接口 HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST); //得到request对象. ((ServletRequestAware) action).setServletRequest(request);//将request对象通过action中重写的方法注入。 }
-
-
-
通过ServletActionContext获取.
-
在ServletActionContext中方法都是static。
-
getRequest(); getResposne(); getPageContext();
-
Result的结果集类型
-
标签
属性 作用 name 与action中的method的返回值匹配,进行跳转. type 是用于定义跳转方式 -
对于type属性它的值有以下几种:
在struts-default.xml文件中定义了type可以取的值:
取得default是true
<result-types> <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" /> </result-types>
-
重点: chain dispatcher redirect redirectAction stream
属性 作用 dispatcher 它代表的是请求转发,也是默认值。它一般用于从action跳转到页面。 chain 它也相当于请求转发。它一般情况下用于从一个action跳转到另一个action。 redirect 它代表的是重定向 它一般用于从action跳转到页面 redirectAction 它代表的是重定向 它一般用于从action跳转另一个action。 stream 代表的是服务器端返回的是一个流,一般用于下载。
局部结果页面与全局结果页面
局部结果页面 和 全局结果页面 (可以将结果抽取出来,当action没哟配置result的时候就会去寻找全局的result)
<action name="result" class="cn.itcast.struts2.ResultAction">
<!-- 局部结果 当前Action使用 -->
<result name="success">/result.jsp</result>
</action>
<global-results>
<!-- 全局结果 当前包中 所有Action都可以用-->
<result name="success">/result.jsp</result>
</global-results>