首页 > 代码库 > Struts2--属性设置方式
Struts2--属性设置方式
Struts2自动获取/设置数据的方式一共分为两种
- 属性驱动(FieldDriven)
- 模型驱动(ModelDriven)
- 属性驱动
属性又分为两种:
|- 基本数据类型
|- JavaBean属性类型
基本数据类型:实例
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 <form action="hello" method="get"> 9 姓名;<input type="text" name="name" size="10px" value="fuwh" readonly="readonly"/> 10 年龄:<input type="text" name="age" size="10px" value="23" readonly="readonly"/>11 <button type="submit">提交</button>12 </form>13 </body>14 </html>
package com.fuwh.struts;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ModelDriven;public class HelloAction implements Action{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String execute() throws Exception { // TODO Auto-generated method stub System.out.println("执行了HelloAction的默认方法"); return SUCCESS; }}
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 欢迎${age }岁的${name }光临 9 </body>10 </html>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <package name="mypack" extends="struts-default"> 8 <action name="hello" class="com.fuwh.struts.HelloAction"> 9 <result name="success">hello.jsp</result>10 </action>11 </package>12 </struts>
JavaBean属性类型
1 package com.fuwh.struts; 2 3 public class User implements java.io.Serializable{ 4 5 private static final long serialVersionUID = 1L; 6 7 private String name; 8 private int age; 9 10 public String getName() {11 return name;12 }13 public void setName(String name) {14 this.name = name;15 }16 public int getAge() {17 return age;18 }19 public void setAge(int age) {20 this.age = age;21 }22 23 }
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 <form action="hello" method="get"> 9 姓名;<input type="text" name="user.name" size="10px" value="fuwh" readonly="readonly"/> 10 年龄:<input type="text" name="user.age" size="10px" value="23" readonly="readonly"/>11 <button type="submit">提交</button>12 </form>13 </body>14 </html>
1 package com.fuwh.struts; 2 3 import com.opensymphony.xwork2.Action; 4 5 public class HelloAction implements Action{ 6 7 private User user 8 9 public User getUser() {10 return user;11 }12 public void setUser(User) {13 this.user = user;14 }15 @Override16 public String execute() throws Exception {17 // TODO Auto-generated method stub18 System.out.println("执行了HelloAction的默认方法");19 return SUCCESS;20 }21 22 }
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 欢迎${user.age }岁的${user.name }光临 9 </body>10 </html>
struts.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <package name="mypack" extends="struts-default"> 8 <action name="hello" class="com.fuwh.struts.HelloAction"> 9 <result name="success">hello.jsp</result>10 </action>11 </package>12 </struts>
模型驱动
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 <form action="hello" method="get"> 9 姓名;<input type="text" name="name" size="10px" value="fuwh" readonly="readonly"/> 10 年龄:<input type="text" name="age" size="10px" value="23" readonly="readonly"/>11 <button type="submit">提交</button>12 </form>13 </body>14 </html>
1 package com.fuwh.struts; 2 3 import com.opensymphony.xwork2.Action; 4 import com.opensymphony.xwork2.ModelDriven; 5 6 public class HelloAction implements Action,ModelDriven{ 7 8 private User user=new User(); 9 10 @Override11 public String execute() throws Exception {12 // TODO Auto-generated method stub13 System.out.println("执行了HelloAction的默认方法mae");14 return SUCCESS;15 }16 @Override17 public User getModel() {18 // TODO Auto-generated method stub19 System.out.println("执行了getModel方法");20 return this.user;21 }22 }
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 欢迎${age }岁的${name }光临 9 </body>10 </html>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <package name="mypack" extends="struts-default"> 8 <action name="hello" class="com.fuwh.struts.HelloAction"> 9 <result name="success">hello.jsp</result>10 </action>11 </package>12 </struts>
Struts2--属性设置方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。