Struts学习笔记 第6页 共39页
将空项目中lib目录中的除junit和spring-test之外的所有文件复制到项目的WebRoot/WEB-INF/lib目录下 4、修改对应的web.xml,建立struts2的filter(参考struts自带的项目),添加如下配置:
三、 Namespace
Namespace决定了action的访问路径,默认为“”,可以接收所有路径的action,如果没有找到相应的namespace时,则使用namespace为空的action
Namespace可以写为/,或者/xxx,或者/xxx/yyy,对应的action访问路径为/index.action、/xxx/index.action、或者/xxx/yyy/index.action.
Namespace最好也用模块来进行命名。
namespace :对应与项目名称后面的\例如Struts2_0100_Introduction后面的\ (http://localhost:8080/Struts2_0100_Introduction/)
四、
五、 Action 具体视图的返回可以由用户自己定义的Action来决定 具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容,有三种手段: Struts学习笔记 第7页 共39页 第三种:Action 继承com.opensymphony.xwork2.ActionSupport类,而这个类又实现了com.opensymphony.xwork2.Action接口,我们重写execute()方法就可以了。 import com.opensymphony.xwork2.ActionSupport; public class IndexAction3 extends ActionSupport { } 注:第三种Action是我们需要使用的方式,因为这个类不担实现了com.opensymphony.xwork2.Action接口,更重要的是它已经帮我封装了许多其它有用的方法。 六、 路径问题的说明 @Override public String execute() { //return \ return this.SUCCESS;//SUCCESS常量值为:\ } struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。 虽然可以用redirect方式解决,但redirect方式并非必要。 解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径) 或者使用myeclipse经常用的,指定basePath 还有另一种方式,就是在
<% String path = request.getContextPath();// String basePath = request.getScheme()+\+request.getServerName()+\+request.getServerPort()+path+\; %>
Action执行的时候并不一定要执行execute方法,我们可以指定Action执行哪个方法: 1、 方法一(通过methed属性指定执行方法):
可以在配置文件中配置Action的时候用method=来指定执行哪个方法
2、 动态方法调用DMI(推荐)
Struts学习笔记 第9页 共39页
可以在url地址中动态指定action执行那个方法。Url地址如下: http://localhost:8080/Struts2_0500_ActionMethod/user/user!add 方法:action + ! + 方法名 注:只要Action对象中有这个方法,并且返回类型为String就可以调用。 这样Struts.xml配置文件中不需要配置methed属性。代码如下:
使用通配符,将配置量降到最低, 不过,一定要遵守\约定优于配置\的原则 1、 通配符 星号(*) 表示所有 {数字} 表示第几个通配符 例如:Student* 那么{1}代表第一个星号(*) 2、 实例 *_* 那么{1}代表第一个星号(*) ,{2}代表第二个星号(*) public String add() { return SUCCESS; }
九、 Action的属性接收参数
Action中三种传递并接受参数:
Struts学习笔记 第10页 共39页
1、 在Action添加成员属性接受参数
例如请求的URL地址:
http://localhost:8080/Struts2_0700_ActionAttrParamInput/user/user!add?name=a&age=8 其中传递了两个参数:name和age,其值分别为:a、8,此Action执行的是add()方法。 那我们只要在user这个Action对象中添加这两个成员属性并生成set、get方法。
public class UserAction extends ActionSupport { } 2、 域模型(Domain Model)
就是利用对象域来进行传递和接受参数 例如请求的URL地址:
http://localhost:8080/Struts2_0800_DomainModelParamInput/user/user!add?user.name=a&user.age=8 其中,访问的是namespace=”/user” action的name=”user” Action所执行的方法method=”add”
利用对象域user来传递参数,为对象的属性赋值(user.name=a user.age=8) 注:需要一个对象user 并且这个对象需要有两个成员属性,且具有get、set方法。 然后在Action中添加一个User对象的成员属性。并且有get、set方法,就可以了。
private String name; private int age; public String add() { } public String getName() { } public void setName(String name) { } public int getAge() { } public void setAge(int age) { this.age = age; } return age; this.name = name; return name; System.out.println(\ + name); System.out.println(\ + age); return SUCCESS; //User对象 public class User { private String name; private int age; public String getName() { } public void setName(String name) { this.name = name; return name;
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Struts2开发手册- 马士兵 - 图文(2)在线全文阅读。
相关推荐: