下面的图说明了location的来历:
6.2.2 Redirect类型
6.2.2.1 说明
Redirect属于重定向。如果用redirect类型,则在reuqest作用域的值不能传递到前
台。
6.2.2.2 例子
页面:resulttype/testRedirect.jsp Action:RedirectAction
配置文件:struts-resulttype.xml
6.2.3 redirectAction类型
6.2.3.1 说明
1、 把结果类型重新定向到action 2、 可以接受两种参数
a) actionName: action的名字 b) namespace:命名空间
6.2.3.2 例子
第一种方式:
7 Action原型模式
7.1 回顾servlet
在servlet中,一个servlet只有一个对象,也就是说servlet是单例模式。如果把一个集合写在servlet属性中,则要考虑线程安全的问题。
7.2 Action多例模式
但是在struts2的框架中,并不存在这种情况,也就是说struts2中的action,只要访问一次就要实例化一个对象。这样不会存在数据共享的问题。这也是struts2框架的一个好处。
7.3 实验
可以写一个类,如下: package cn.itcast.struts2.action.moreinstance; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings(\ public class MoreInstanceAction extends ActionSupport{ public MoreInstanceAction(){ System.out.println(\配置文件为: } public String execute(){ System.out.println(\ return SUCCESS; } }
8.1 Execute方法的弊端
假设有这样的需求:
1、 有一个action为PersonAction。
2、 在PersonAction中要实现增、删、改、查四个方法。 3、 但是在action中方法的入口只有一个execute方法
4、 所以要想完成这样的功能,有一种方法就是在url链接中加参数。
那么在action中的代码可能是这样的:
PatternAction public class PatternAction extends ActionSupport{ private String method; public String execute(){ if(method.equals(\ //增加操作 }else if(method.equals(\ //修改操作 }else if(method.equals(\ //删除操作 }else{ //查询操作 } return \ } }
可以看出这样写结构并不是很好。而通配符的出现解决了这个问题。
8.2 method属性
Pattern.jsp 访问PersonAction的add方法:
Struts-pattern.xml
8.3 动态调用方法
在url中通过action名称!方法名称可以动态调用方法。 Pattern.jsp 动态调用PatternAction中的add方法:
struts-pattern.xml 说明:这样的情况在配置文件中不需要method属性
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库struts2(3)在线全文阅读。
相关推荐: