ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 基本类型 包含包装类.将页面名称和struts2中的成员属性名对比,如果一致调用成员属性的set方法将页面的值封装到成员属性中. 条件:需要私有属性,需要set. ~~~ public class HelloAction extends ActionSupport { private String name; private int age; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public String index() { System.out.println("name : " + name + ",age: " + age); return null; } } ~~~ ## 对象类型 需要注意的是:页面的开头得是私有对象属性的属性名. 对象.属性. 这种叫OGNL表达式. 条件:需要对象.需要get/set,页面的写法也不同. domain: ~~~ public class User { private String name; private Integer age; public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } } ~~~ action: ~~~ public class HelloAction extends ActionSupport { private User user; public void setUser(User user) { this.user = user; } public User getUser() { return user; } public String index() { System.out.println(user); return null; } } ~~~ 页面: ![](https://box.kancloud.cn/c214d45826d50804f4eb0da58c29aa2b_1270x250.png) ## 复杂类型 ### list 索引代表第几个对象. action: ~~~ public class HelloAction extends ActionSupport { private List<User> list; public List<User> getList() { return list; } public void setList(List<User> list) { this.list = list; } public String index() { for (User user : list) { System.out.println(user); } return null; } } ~~~ 页面: ![](https://box.kancloud.cn/fe3e7eb8ad045b7dd51de31e0aa88744_1942x338.png) 结果: ``` User{name=jack', age=30} User{name='milan', age=40} ``` ### map action: ~~~ public class HelloAction extends ActionSupport { private Map<String,User> map; public Map<String, User> getMap() { return map; } public void setMap(Map<String, User> map) { this.map = map; } public String index() { for (String key:map.keySet()){ System.out.println(map.get(key)); } return null; } } ~~~ 页面: ![](https://box.kancloud.cn/2023e554860ee2289fad8e57e66aee4b_1824x298.png)