struts2是可以注入一个对象的,那么一定需要继承ModelDriven的泛型接口。
package com.test.action; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.test.model.Contact; import com.test.model.User; public class ValideAction extends ActionSupport implements ModelDriven<User>{ /** * */ private static final long serialVersionUID = 1L; private User user; private String username; private Contact contact; public Contact getContact() { return contact; } public void setContact(Contact contact) { this.contact = contact; } 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; } private String password; public String execute() { System.out.println(user.getContact().getPhone()); return SUCCESS; } @Override public User getModel() { // TODO Auto-generated method stub if (null == user) { return user = new User(); } return user; } }如上面的代码所示,就是会将User注入到类action中。而User也是一个嵌套类。
User
package com.test.model; public class User { private String username; private Contact contact; public Contact getContact() { return contact; } public void setContact(Contact contact) { this.contact = contact; } 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; } private String password; public String toString() { return username+" "+password; } }
package com.test.model; public class Contact { private String phone; private String address; private String email; public Contact() { } public Contact(String phone,String address,String email) { this.phone=phone; this.address=address; this.email=email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 这个地方是用来显示错误信息 --> <s:fielderror/> <form action="data.action" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> -------------------联系方式----------------<br> 手机:<input type="text" name="contact.phone"><br> 地址:<input type="text" name="contact.address"><br> 邮箱:<input type="text" name="contact.email"><br> <input type="submit" name="ok"><br> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 测试结果页面<br> 用户名:<s:property value="username"/><br> 密码:<s:property value="password"/><br> --------------------联系方式-------------<br> 手机:<s:property value="contact.phone"/><br> 地址:<s:property value="contact.address"/><br> 邮箱:<s:property value="contact.email"/><br> <s:debug></s:debug> </body> </html>
从这个例子中,学会了跳跃性的解决问题的思路,另外就是对依赖注入还不是非常了解,这个需要重点去学习和理解一下,特别是常用的几种注入方式。