您的位置:首页 > 编程语言 > Java开发

struts2第十七讲学习笔记,Action处理复选框与JSP页面使用ognl处理map

2017-06-05 19:36 519 查看
1.Action处理复选框,可以使用数组和List 集合存放数据。

JSP代码

<body>
<form action="hobbies.action" method="post">
爱好:<input type="checkbox" name="hobbies" value="football" />足球 <input
type="checkbox" name="hobbies" value="basketball">篮球 <input
type="checkbox" name="hobbies" value="pingpangball">乒乓球 <input
type="checkbox" name="hobbies" value="yumaoball">羽毛球 <br>
喜欢:<input type="checkbox" name="hobby" value="football" />足 <input
type="checkbox" name="hobby" value="basketball">篮 <input
type="checkbox" name="hobby" value="pingpangball">乒乓 <input
type="checkbox" name="hobby" value="yumaoball">羽毛 <br>
<input type="submit" value="提交" />
</form>

</body>
Action中代码

public class hobby {
private String[] hobby;
private List<String> hobbies;

public String execute() {
for (String h : hobby) {
System.out.print(" " + h);
}
System.out.println();
System.out.println(hobbies);//在控制台可以看到从前台接受到的数据

return null;
}

public List<String> getHobbies() {
return hobbies;
}

public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}

public String[] getHobby() {
return hobby;
}

public void setHobby(String[] hobby) {
this.hobby = hobby;
}

}
2.ognl表达式在页面中使用map的强大功能,懒得从数据库获取数据了,自己手写了一份。
Action代码

public String list() {
map = getMap();//自己手动写的map
System.out.println(map);
list = service.getList();//从数据库拿到的list
System.out.println("list run:" + list);
return Action.SUCCESS;
}

public Map<Integer, String> getMap() {
Map<Integer,String> map = new HashMap<Integer,String>();
map.put(1, "黄铜会员");
map.put(2, "白银会员");
map.put(3, "黄金会员");
map.put(4, "钻石会员");
return map;

}

<s:iterator value="list" var="bean">
<tr>
<!--第二种写法,加入var=bean,从bean请求数据,带上#即可-->
<td><s:property value="#bean.id"/></td>
<td><s:property value="#bean.name"/></td>
<td><s:property value="#bean.password"/></td>
<td>
<!--每一个会员的等级都会显示出来,ognl表达式能直接拿到map对象而不声明-->
<s:property value="map[#bean.id]"/>
</td>
<td><a href="user/toUpdate.action?user.id=<s:property value="#bean.id"/>">修改</a></td>
</tr>

</s:iterator>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: