Struts2自学入门(二)

注:黄色字体为网络引用内容




一、Action和ActionSupport的区别


Actionsupport这个工具类在实现了Action接口的基础上还定义了一个validate()方法,重写该方法,它会在execute()方法之前执行,如校验失败,会转入input处,必须在配置该Action时配置input属性。
另外,Actionsupport还提供了一个getText(String key)方法还实现国际化,该方法从资源文件上获取国际化信息.
这样在自定义标签时可以定义一个变量为new actionsupport对象实现国际化。 


二、Struts.xml的基础配置

1、package:

name:规定了package的名字,没有实质性的作用,但可以有效的区分代码模块,增加开发效率

namespace:规定了此package的命名空间,即在访问此包Action的时候需要在Action路径前加上命名空间namespace

     extends:规定了此包继承哪个包。(有待学习)

     abstract:用来定义包为抽象的,也就是不能包含Action的定义,但是抽象包可以被其他包继承,因此里面可以定义其他包需要的元素,比如ResultType、 Interceptor等等。struts-default包,它就是个抽象的包

 2、 Action:

name:规定了此Action的名字以及访问路径。

class:规定了Action所访问的操作类

method:规定了此Action访问操作类的哪个方法,默认为excute方法

 3、 result:

name:规定了此Action的返回值名字

xml-value:规定了此result的返回到哪个页面 如result.jsp  在前面加上”${pageContext.request.contextPath}/” 是绝对路径。


三、Action的javaBean匹配

1、单匹配

  Action需要继承implements ModelDriven<T> 前端页面的参数名直接写Bean的成员属性 在接口的public User getModel() 里 return bean

注意: 此方法需要在Action实例化Bean 如Private User user = new User(); 但无需写Get和Set方法

2、多匹配

Action需要声明匹配的Bean 如 private User user  再加入Get和Set方法。 前端页面的参数名前需要加上Bean名称 如:User.username/User.userpassowrd


四、Action的集合匹配(有待进一步学习)

1、String数组:声明:Sting[] arr; 再加入Get和Set方法,前台页面如下:

<form action="hobby" method="post">
	爱好:
	<input type="checkbox" name="hobby" value="唱歌"/>唱歌
	<input type="checkbox" name="hobby" value="跳舞"/>跳舞
	<input type="checkbox" name="hobby" value="踢球"/>踢球
	<input type="checkbox" name="hobby" value="睡觉"/>睡觉
	<input type="checkbox" name="hobby" value="玩游戏"/>玩游戏
	<input type="submit" value="提交"/>
</form>

 操作方法里如下

		for(String h:hobby){
			System.out.println(h);
			
		}

2、List集合:声明 List<Student> students; 再加入Get和Set方法 前台页面如下:

<form action="student" method="post">
	<table>
		<tr>
			<th>姓名</th>
			<th>性别</th>
			<th>年龄</th>
		</tr>
		<tr>
			<td><input type="text" name="students[0].name"/></td>
			<td><input type="text" name="students[0].sex"/></td>
			<td><input type="text" name="students[0].age"/></td>
		</tr>
		<tr>
			<td><input type="text" name="students[1].name"/></td>
			<td><input type="text" name="students[1].sex"/></td>
			<td><input type="text" name="students[1].age"/></td>
		</tr>
		<tr>
			<td colspan="3">
				<input type="submit" value="提交"/>
			</td>
		</tr>
	</table>
</form>
 操作方法里如下

		for(Student s:students){
			
			System.out.println(s.toString());
		}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章