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