Struts2
来源:互联网 发布:怎么样开淘宝店视频 编辑:程序博客网 时间:2024/06/05 10:32
一、XML配置文件的主要元素
1、package元素
1.1、 作用:
用来管理action:使用了面向对象的思想
name:用来定义包名,注意:同一个配置文件中包名是唯一的
extends:指定所继承的包
abstract:指定包是抽象的,只能被继承,含有action的包不能是抽象的,默认值是false
namespace:命名空间,访问动作的路径,默认值是" "
使用了命名空间:访问动作的路径就是 命名空间+动作名称
struts2查找动作的方法:
1:先按照用户输入的路径朝找路径:/a/b/c/d/action1.action
如果没有找到再向上一层路径找:/a/b/c
再向上一层路径找:/a/b 找到了该路径则执行第二步,找不到继续向上一级找
再向上一级路径找:/a 没找到
都没找到直接报错
2:找action:先在当前的命名空间下找,如果没有找到则去默认的(没有写namespace属性的)命名空间下找,如果还没找到就报错!
<struts> <constant name ="struts.devMode" value="true"></constant>
<package name="p1" extends="struts-default"> <action name="action1" class="com.lzh.web.action.Test1Action" method="test1"> <result name="success">/success.jsp</result> </action></struts>
1.2、action :用来定义动作
name:动作的名称,用户访问的动作的名称
class:动作方法所属的包和类
method:动作方法
<struts> <constant name ="struts.devMode" value="true"></constant><package name="p1" extends="struts-default"> <action name="action1" class="com.lzh.web.action.TestAction1" method="test1"> <result name="success">/success.jsp</result> </action></struts>
2、POJO:Plain Old Java Object 原始的老的java对象:无侵入式
3、第二种创建动作类的配置
<struts><package name="p1" extends="struts-default"> <action name="action2" class="com.lzh.web.action.TestAction2"><!--因为默认执行execute()方法所以<pre name="code" class="html">省略method--><result name="success">/success.jsp</result> </action></struts>
4、默认执行的
SUCCESS = "success" 当执行动作方法成功时所前往的页面
ERROR = "error" 当方法出现异常时所前往的页面
INPUT = ”input“ 数据回显
NONE = ”none“ 什么都不反回,相当于return null
LOGIN = " login" 回到登陆页面
二、创建动作类的第三种方式
继承Action接口的子类ActionSupport
1、第三种创建动作类的配置
<action name="action3" class="com.lzh.web.action.TestAction3"><!--因为默认执行execute()方法所以<pre name="code" class="html">省略method-- 相比于第二种不用手动重写execute方法><result name="success">/success.jsp</result>
2、省略class 和method,执行了默认的class:ActionSupport和方法 //<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
改变默认执行的class,不建议修改
<default-class-ref class=" ">
<action name ="defaultAction">
<result name = "success">/success.jsp</result>
</action>
3、UserAction中方法的配置
<action name = "addUser" class= " com.lzh.web.action.UserAction method ="addUser""><result name ="success" >/addUser.jsp</result><action name = "delUser" class= " com.lzh.web.action.UserAction <span style="font-family: Arial, Helvetica, sans-serif;">method ="delUser”</span>><result name ="success" >/delUser.jsp</result>
<action name = "*" class= " com.lzh.web.action.UserAction method ="{1}"><result name ="success" >/{1}.jsp</result>
<span style="white-space:pre"></span></action>
<a href ="${pageContext.request.contextPath}/add_User.action"> 时
<action name = "*_*" class= " com.lzh.web.action.{2}Action method ="{1}{2}"><result name ="success" >/{1}{2}.jsp</result>
<span></span></action>
开启动态执行动作方法(存在安全问题)
<a href ="${pageContext.request.contextPath}/user!add_User.action"> 时
<action name = "user" class= " com.lzh.web.action.UserAction ><result name ="success" >/success.jsp</result>
三、result结果视图
<struts> <constant name ="struts.devMode" value="true"></constant><package name="p1" extends="struts-default"> <action name="action1" class="com.lzh.web.action.TestAction1">
<!-- <div>1、result 结果视图:</div><div></div><div><span style="white-space: pre;"></span>name:结果视图名称,和动作方法的返回值一致,什么结果</div><div><span style="white-space:pre"></span>type:以什么方式转到页面?默认值是dispatcher,转发</div><div><span style="white-space:pre"></span>dispatcher:转发</div><div><span style="white-space:pre"></span>redirect:重定向</div><div><span style="white-space:pre"></span>chain:转发到另一个Action</div><div><span style="white-space:pre"><span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">转发到同一个包下的另一个Action</span></span></div><div><span style="white-space:pre"><span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"><span style="white-space:pre"><span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">转发到不同包下的另一个Action</span></span></span></span></div><div><span style="white-space:pre"></span>redirectAction:重定向到另一个Action<span style="white-space:pre"></span></div><div><span style="white-space:pre"><span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">重定向到同一个包下的另一个Action</span></span></div><div><span style="white-space:pre"><span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"><span style="white-space:pre"><span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">重定向到不同包下的另一个Action</span></span></span></span></div>--> <result name="success" type="redirect">/success.jsp</result> </action></struts>
1 0
- Struts2
- struts2
- struts2
- Struts2
- struts2
- struts2
- Struts2
- struts2
- struts2
- struts2
- Struts2
- Struts2
- Struts2
- struts2
- struts2
- STRUTS2
- Struts2
- Struts2
- Android应用程序组成
- AFN请求超时时间
- java中string和int相互转换
- iostat
- SpringBoot-SpringData-oneToOne
- Struts2
- Astyle 给代码调整格式
- MonoBehaviour生命周期
- ZooKeeper集群搭建实例,以及集成dubbo时的配置
- js中的function和this---new Function(a,b,body)方式
- IIS服务器配置外部访问端口及asp,php环境搭建
- Android开发工具篇:华为手机root
- Clob类型
- 自定义View与ViewGroup