2.Struts2框架中的Action配置
2.1 Struts2 框架的执行过程

2.2 Action的作用
1、封装业务操作
2、数据转移
3、返回结果字符串
2.3 Action接口常量含义
SUCCESS:表示程序处理正常,并返回成功结果页。
NONE: 表示处理正常结束,但不返回给用户任何显示。
ERROR: 表示处理结果失败。
INPUT: 用户输入数据有误,通常用于表单数据校验
LOGIN: 主要权限认证 (登陆页面)
2.4 Action动态方法调用
1 public class UserAction extends ActionSupport { 2 @Override 3 public String execute() throws Exception { 4 5 return SUCCESS; 6 } 7 public String add(){ 8 System.out.println("add"); 9 return NONE; 10 } 11 12 }
Struts.xml配置
1 <package name="usr" namespace="/" extends="struts-default"> 2 <action name="user" class="com.action.UserAction"></action> 3 </package>
页面访问


*注:动态方法存在安全隐患,故在日常企业开发中不推荐该方式,可以用如下常量禁止
1 <constant name=“struts.enable.DynamicMethodInvocation”value=“false”>
2.5 Action通配符的使用
java代码不变,只需要修改其struts.xml文件即可, 使用通配符* ,可以简化struts.xml配置
struts.xml配置:
<package name="usr" namespace="/" extends="struts-default"> <action name="*user" class="com.action.UserAction" method="{1}"> </action> </package>
2.6 Result结果类型
Action处理请求后, 返回字符串(逻辑视图名), 需要在struts.xml 提供 <result>元素定义结果页面
1、 全局结果页面
<global-results> <result name="error">/error.jsp</result> <result name="login">/login.jsp</result> </global-results>
2、 结果页面跳转类型
* 在struts-default.xml 定义了 一些结果页面类型
* 使用默认type 是 dispatcher 转发 (request.getRequestDispatcher.forward)
1) dispatcher :Action 转发给 JSP
2) chain : Action调用另一个Action (同一次请求)
<result name="success" type="chain">hello</result> hello是一个Action的name
3) redirect : Action重定向到 JSP
4) redirectAction :Action重定向到另一个Action
<result name="success" type="redirectAction">hello</result>

浙公网安备 33010602011771号