当前位置: 移动技术网 > IT编程>软件设计>架构 > Struts2_属性驱动

Struts2_属性驱动

2018年11月06日  | 移动技术网IT编程  | 我要评论

在jsp页面提交到action中的表单元素在action中是以对象的形式存在的,

action中的对象name必须与jsp页面中的表单元素name相同,struts框架自动为action的对象赋值.

 

一.普通驱动

注意:action中的对象name必须与jsp页面中的表单元素name相同,struts框架自动为action的对象赋值.

案例:

struts.xml文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <!doctype struts public
 4     "-//apache software foundation//dtd struts configuration 2.3//en"
 5     "http://struts.apache.org/dtds/struts-2.3.dtd">
 6 
 7 <struts>
 8     <package name="hello" extends="default-struts" namespace="">
 9         <action name="hello" class="com.ahd.action.helloaction">
10             <result>/success.jsp</result>
11         </action>
12     </package>
13 </struts>

jsp页面:

1 <body>
2     <form action="hello.action">
3         用户名:<input type="text" name="username"/><br/>
4         密    码:<input type="password" name="password"/><br/>
5         <input type="submit" value="submit"/>
6         <input type="reset" name="reset"/><br/>
7     </form>
8     
9   </body>

action页面:

 1 package com.ahd.action;
 2 
 3 import com.opensymphony.xwork2.actionsupport;
 4 
 5 public class helloaction extends actionsupport{
 6     private string username;
 7     private string password;
 8     
 9     @override
10     public string execute() throws exception {
11         // todo auto-generated method stub
12         return super.execute();
13     }
14 
15     public string getusername() {
16         return username;
17     }
18 
19     public void setusername(string username) {
20         this.username = username;
21     }
22 
23     public string getpassword() {
24         return password;
25     }
26 
27     public void setpassword(string password) {
28         this.password = password;
29     }
30     
31 }

 

二.属性驱动

普通驱动如果是大项目表单提供过多元素action会比较乱,属性驱动就是将表单提供的元素封装到一个对象中,这个对象作为action中的属性,

在前端页面表单中需要提交的元素name统一写成 对象.name

案例:

实体类user

 1 package com.ahd.entity;
 2 
 3 public class user {
 4     private string username;
 5     private string password;
 6     public string getusername() {
 7         return username;
 8     }
 9     public void setusername(string username) {
10         this.username = username;
11     }
12     public string getpassword() {
13         return password;
14     }
15     public void setpassword(string password) {
16         this.password = password;
17     }
18 }

helloaction:

 1 package com.ahd.action;
 2 
 3 import com.ahd.entity.user;
 4 import com.opensymphony.xwork2.actionsupport;
 5 
 6 public class helloaction extends actionsupport{
 7     private user user;
 8     
 9     @override
10     public string execute() throws exception {
11         // todo auto-generated method stub
12         system.out.println(user.getusername()+user.getpassword());
13         return super.execute();
14     }
15 
16     public user getuser() {
17         return user;
18     }
19 
20     public void setuser(user user) {
21         this.user = user;
22     }
23     
24 }

index.jsp:

1   <body>
2     <form action="hello.action">
3         用户名:<input type="text" name="user.username"/><br/>
4         密    码:<input type="password" name="user.password"/><br/>
5         <input type="submit" value="submit"/>
6         <input type="reset" name="reset"/><br/>
7     </form>
8     
9   </body>

strust.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <!doctype struts public
 4     "-//apache software foundation//dtd struts configuration 2.3//en"
 5     "http://struts.apache.org/dtds/struts-2.3.dtd">
 6 
 7 <struts>
 8     <package name="hello" extends="default-struts" namespace="">
 9         <action name="hello" class="com.ahd.action.helloaction">
10             <result>/success.jsp</result>
11         </action>
12     </package>
13 </struts>

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网