标签:注意 请求 submit drive log let 泛型 list ognl
一.请求参数封装
1.属性驱动:
(1).无实体类情况:属性和动作类在一起
a.编写jsp页面,提交数据,例如name和age
1 <form action="${pageContext.request.contextPath}/demo1.action" method="post"> 2 用户名:<input type="text" name="name"/><br/> 3 年龄:<input type="text" name="age"/><br/> 4 <input type="submit" value="提交"/> 5 </form>
b.编写动作类
1 package huguangqin.com.cnblogs.action; 2 import com.opensymphony.xwork2.ActionSupport; 3 //它要想封装成功,有如下要求: 表单元素的name属性取值必须和动作类中的属性一致(get/set方法后面的部分) 4 public class UserAction extends ActionSupport { 5 private String name; 6 private Integer age;// struts2会自动转为String.表单可以提交的数据类型包括:String/String[]/资源 7 8 public String demo1() { 9 System.out.println("name=" + name + ",age=" + age); 10 return this.SUCCESS; 11 } 12 13 public String getName() { 14 return name; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 public Integer getAge() { 22 return age; 23 } 24 25 public void setAge(Integer age) { 26 this.age = age; 27 } 28 29 } 30
c.编写struts.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 导入dtd约束 --> 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 <struts> 7 <package name="u" extends="struts-default"> 8 <action name="*" class="huguangqin.com.cnblogs.action.UserAction" method="{1}"> 9 <result name="success">/success.jsp</result> 10 </action> 11 </package> 12 </struts>
(2).有实体类情况
动作类和模型数据分开
* 使用要求:
* 首先动作类中需要提供模型的get/set方法
* 模型中要提供私有成员变量的get/set方法
* 并且这些get/set方法必须和表单的name属性取值能对应上。
a.编写jsp--注意OGNL语语
1 <form action="${pageContext.request.contextPath}/demo1.action" method="post"> 2 用户名:<input type="text" name="user.name"/><br/> 3 年龄:<input type="text" name="user.age"/><br/> 4 <input type="submit" value="提交"/> 5 </form>
b.编写实体类
1 package huguangqin.com.cnblogs.entity; 2 public class User { 3 private String name; 4 private Integer age; 5 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 public Integer getAge() { 15 return age; 16 } 17 18 public void setAge(Integer age) { 19 this.age = age; 20 } 21 22 @Override 23 public String toString() { 24 return "User [name=" + name + ", age=" + age + "]"; 25 } 26 27 } 28
c.编写动作类
1 package huguangqin.com.cnblogs.action; 2 import com.opensymphony.xwork2.ActionSupport; 3 import huguangqin.com.cnblogs.entity.User; 4 5 //使用要求:首先动作类中需要提供模型的get/set方法; 6 //模型中要提供私有成员变量的get/set方法 7 //并且这些get/set方法必须和表单的name属性取值能对应上 8 public class UserAction extends ActionSupport { 9 // 提供模型的实例化对象 10 private User user = new User(); 11 12 // 提供get/set方法 13 public User getUser() { 14 return user; 15 } 16 17 public void setUser(User user) { 18 this.user = user; 19 } 20 21 public String demo1() { 22 System.out.println(user); 23 return this.SUCCESS; 24 } 25 26 } 27
d.编写struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 导入dtd约束 --> 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 <struts> 7 <package name="u" extends="struts-default"> 8 <action name="*" class="huguangqin.com.cnblogs.action.UserAction" method="{1}"> 9 <result name="success">/success.jsp</result> 10 </action> 11 </package> 12 </struts>
(3)封装到list
a.编写jsp--注意模型属性取值
1 <form action="${pageContext.request.contextPath}/demo1.action" method="post"> 2 用户名1:<input type="text" name="users[0].name"/><br/> 3 年龄1:<input type="text" name="users[0].age"/><br/> 4 用户名2:<input type="text" name="users[1].name"/><br/> 5 年龄2:<input type="text" name="users[1].age"/><br/> 6 用户名3:<input type="text" name="users[2].name"/><br/> 7 年龄3:<input type="text" name="users[2].age"/><br/> 8 <input type="submit" value="提交"/> 9 </form>
1 package huguangqin.com.cnblogs.entity; 2 public class User { 3 private String name; 4 private Integer age; 5 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 public Integer getAge() { 15 return age; 16 } 17 18 public void setAge(Integer age) { 19 this.age = age; 20 } 21 22 @Override 23 public String toString() { 24 return "User [name=" + name + ", age=" + age + "]"; 25 } 26 27 } 28
c.编写动作类
1 package huguangqin.com.cnblogs.action; 2 import java.util.List; 3 import com.opensymphony.xwork2.ActionSupport; 4 import huguangqin.com.cnblogs.entity.User; 5 //使用要求: 首先动作类中需要提供模型的get/set方法; 模型中要提供私有成员变量的get/set方法 6 //并且这些get/set方法必须和表单的name属性取值能对应上 7 public class UserAction extends ActionSupport { 8 private List<User> users; 9 10 public List<User> getUsers() { 11 return users; 12 } 13 14 public void setUsers(List<User> users) { 15 this.users = users; 16 } 17 18 public String demo1() { 19 System.out.println(users); 20 return this.SUCCESS; 21 } 22 23 } 24
d.编写Struts
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 导入dtd约束 --> 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 <struts> 7 <package name="u" extends="struts-default"> 8 <action name="*" class="huguangqin.com.cnblogs.action.UserAction" method="{1}"> 9 <result name="success">/success.jsp</result> 10 </action> 11 </package> 12 </struts>
(4)封装到map
a.编写jsp--注意模型属性
1 <form action="${pageContext.request.contextPath}/demo1.action" method="post"> 2 用户名1:<input type="text" name="users[‘one‘].name"/><br/> 3 年龄1:<input type="text" name="users[‘one‘].age"/><br/> 4 用户名2:<input type="text" name="users[‘two‘].name"/><br/> 5 年龄2:<input type="text" name="users[‘two‘].age"/><br/> 6 用户名3:<input type="text" name="users[‘three‘].name"/><br/> 7 年龄3:<input type="text" name="users[‘three‘].age"/><br/> 8 <input type="submit" value="提交"/> 9 </form>
b.编写实体类
1 package huguangqin.com.cnblogs.entity; 2 public class User { 3 private String name; 4 private Integer age; 5 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 public Integer getAge() { 15 return age; 16 } 17 18 public void setAge(Integer age) { 19 this.age = age; 20 } 21 22 @Override 23 public String toString() { 24 return "User [name=" + name + ", age=" + age + "]"; 25 } 26 27 } 28
c.编写动作类
1 package huguangqin.com.cnblogs.action; 2 import java.util.Map; 3 import com.opensymphony.xwork2.ActionSupport; 4 import huguangqin.com.cnblogs.entity.User; 5 // 使用要求: 首先动作类中需要提供模型的get/set方法; 模型中要提供私有成员变量的get/set方法 6 // 并且这些get/set方法必须和表单的name属性取值能对应上 7 public class UserAction extends ActionSupport { 8 private Map<String, User> users; 9 10 public Map<String, User> getUsers() { 11 return users; 12 } 13 14 public void setUsers(Map<String, User> users) { 15 this.users = users; 16 } 17 18 public String demo1() { 19 System.out.println(users); 20 return this.SUCCESS; 21 } 22 23 } 24
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 导入dtd约束 --> 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 <struts> 7 <package name="u" extends="struts-default"> 8 <action name="*" class="huguangqin.com.cnblogs.action.UserAction" method="{1}"> 9 <result name="success">/success.jsp</result> 10 </action> 11 </package> 12 </struts>
2.模型驱动
a.编写jsp
1 <form action="${pageContext.request.contextPath}/demo1.action" method="post"> 2 用户名:<input type="text" name="name"/><br/> 3 年龄:<input type="text" name="age"/><br/> 4 <input type="submit" value="提交"/> 5 </form>
b.编写实体类
1 package huguangqin.com.cnblogs.entity; 2 public class User { 3 private String name; 4 private Integer age; 5 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 public Integer getAge() { 15 return age; 16 } 17 18 public void setAge(Integer age) { 19 this.age = age; 20 } 21 22 @Override 23 public String toString() { 24 return "User [name=" + name + ", age=" + age + "]"; 25 } 26 27 } 28
c.编写动作类
1 package huguangqin.com.cnblogs.action; 2 import com.opensymphony.xwork2.ActionSupport; 3 import com.opensymphony.xwork2.ModelDriven; 4 import huguangqin.com.cnblogs.entity.User; 5 6 //* 使用要求: 7 //* 1.动作类必须实现ModelDriven接口,其中泛型提供的是模型对象 8 //* 2.在动作类中需要定义一个模型对象,并且必须由我们自己来实例化 9 //* 3.提供接口中抽象方法的实现,方法的返回值是我们定义的模型对象 10 //* 模型驱动没有要求生成get/set方法 11 12 public class UserAction extends ActionSupport implements ModelDriven<User> { 13 //实例化模型对象 14 private User user = new User(); 15 16 public String demo1() { 17 System.out.println(user); 18 return this.SUCCESS; 19 } 20 //实现抽象方法,返回模型对象 21 @Override 22 public User getModel() { 23 return user; 24 } 25 26 } 27
d.编写struts
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 导入dtd约束 --> 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 <struts> 7 <package name="u" extends="struts-default"> 8 <action name="*" class="huguangqin.com.cnblogs.action.UserAction" method="{1}"> 9 <result name="success">/success.jsp</result> 10 </action> 11 </package> 12 </struts>
二.结果视图的配置
1.result标签:
name:逻辑视图的名称,对应着动作方法的返回值。默认值是success。
type:结果类型,指的就是用什么方式转到定义的页面。默认是dispatcher。
type的常用取值:
dispatcher:(默认值)
使用请求转发,转向一个页面。
redirect:
使用重定向,转向一个页面。它可以用于重定向到另外一个动作。
redirectAction:
使用重定向,前往指定的动作。
2.全局结果视图:<global-results>
1 <package name="myDefault" extends="struts-default" abstract="true"> 2 <global-results> 3 <result name="login">/login.jsp</result> 4 </global-results> 5 </package>
三.访问Servlet的API的三种方式
1.使用ServletActionContext
1 public class HelloAction extends ActionSupport{ 2 3 private HttpServletRequest request; 4 private HttpServletResponse response; 5 private HttpSession session; 6 private ServletContext application; 7 8 public String sayHello(){ 9 request = ServletActionContext.getRequest(); 10 response = ServletActionContext.getResponse(); 11 session = request.getSession(); 12 application = ServletActionContext.getServletContext(); 13 14 System.out.println(request); 15 System.out.println(response); 16 System.out.println(session); 17 System.out.println(application); 18 return SUCCESS; 19 } 20 } 21
2.使用ActionContext的方式(ActionContext 是一个map结构的对象)
1 public class Hello2Action extends ActionSupport{ 2 3 private HttpServletRequest request; 4 private HttpServletResponse response; 5 private HttpSession session; 6 private ServletContext application; 7 8 public String sayHello(){ 9 request = (HttpServletRequest) ActionContext.getContext().get("com.opensymphony.xwork2.dispatcher.HttpServletRequest"); 10 response = (HttpServletResponse) ActionContext.getContext().get("com.opensymphony.xwork2.dispatcher.HttpServletResponse"); 11 session = request.getSession(); 12 application = (ServletContext) ActionContext.getContext().get("com.opensymphony.xwork2.dispatcher.ServletContext"); 13 14 System.out.println(request); 15 System.out.println(response); 16 System.out.println(session); 17 System.out.println(application); 18 return SUCCESS; 19 } 20 } 21
3.通过实现接口的方式
1 public class Hello3Action extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{ 2 3 private HttpServletRequest request; 4 private HttpServletResponse response; 5 private HttpSession session; 6 private ServletContext application; 7 8 public String sayHello(){ 9 10 session = request.getSession(); 11 12 System.out.println(request); 13 System.out.println(response); 14 System.out.println(session); 15 System.out.println(application); 16 return SUCCESS; 17 } 18 19 @Override 20 public void setServletRequest(HttpServletRequest request) { 21 this.request = request; 22 } 23 @Override 24 public void setServletResponse(HttpServletResponse response) { 25 this.response = response; 26 } 27 @Override 28 public void setServletContext(ServletContext application) { 29 this.application = application; 30 } 31 32 33 } 34
标签:注意 请求 submit drive log let 泛型 list ognl
原文地址:http://www.cnblogs.com/huguangqin/p/7450194.html