利用struts2框架进行将页面请求的参数封装有三种方法
第一种(不推荐)
就是动作类和bean中结合在一起,这样方法比较简单,但是很混乱。
代码:
package com.example.action; import com.opensymphony.xwork2.ActionSupport; public class Person extends ActionSupport{ private String username; private String password; private String nickname; public String getUsername() { return username; } //动作的方法 public String execute() throws Exception { System.out.println(username+","+password+","+nickname); return NONE; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } }
<action name="act1" class="com.example.action.Person"> <!-- 静态注入 相当于调用action动作类的get 和set 方法--> <param name="nickname">游客</param> </action>
第二种将动作类与bean中分离
package com.example.domain; /** * 这是模型 * @author sheldonLee * */ public class Student { private String username; private String password; private String nickname; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } @Override public String toString() { return "Student [username=" + username + ", password=" + password + ", nickname=" + nickname + "]"; } }
package com.example.action; import com.example.domain.Student; /** * 这是动作 * @author sheldonLee * */ public class StudentAction { private Student student; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public String save(){ //将表单的数据封装到模型对象中 //调用service 将student中的数据保存到数据库中 System.out.println(student.toString()); return "none"; } }
<body> <form action="${pageContext.request.contextPath }/act2" method="post"> 用户名:<input type="text" name="student.username"/><br/> 密 码:<input type="password" name="student.password"/><br/> 昵 称: <input type="text" name="student.nickname"/><br/> <input type="submit" value="注册"/><br/> </form>
第三种是驱动模型(动作和bean也是分开的)ModelDriven
package com.example.domain; /** * 这是Customer模型 * @author sheldonLee * */ public class Customer { private String username; private String password; private String nickname; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } @Override public String toString() { return "Customer [username=" + username + ", password=" + password + ", nickname=" + nickname + "]"; } }
package com.example.action; import com.example.domain.Customer; import com.opensymphony.xwork2.ModelDriven; public class CustomerAction implements ModelDriven<Customer>{ //动作类关联一个Customer 对象 //为了getModel() 这个不会返回为空 所以要是实例化一个对象 private Customer customer= new Customer(); public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public String save(){ System.out.println(customer); return "none"; } //调用动作方法前,框架会先调用这个方法 //这里不会实例化对象 public Customer getModel() { // TODO Auto-generated method stub return customer; } }
<form action="${pageContext.request.contextPath }/act2" method="post"> <!--参数中省掉了类名 --> 用户名:<input type="text" name="username"/><br/> 密 码:<input type="password" name="password"/><br/> 昵 称: <input type="text" name="nickname"/><br/> <input type="submit" value="注册"/><br/> </form>
主要注意两点 动作类要实现ModelDriver的接口,动作类中要关联对象要注意要实例化
时间: 2024-10-11 07:30:05