JavaWeb -- Struts2 构建视图:标签和结果, UI组件标签
1. 示例
action 注入数据 和 处理action
/** * OgnlAction */ public class UiAction extends ActionSupport { private static final long serialVersionUID = -6933309304624396640L; /* 婚否 */ private boolean married = true; /* javabean集合 */ private List userList; /*省集合*/ private List<Area> provinces ; /* 选中的id */ private Integer selectedId = 10; public List getUserList() { return userList; } public void setUserList(List userList) { this.userList = userList; } public String reg() { popUserList(); return "success"; } public String toRegView() { System.out.println("toRegView"); return "loginView"; } /** * 保存数据, */ public String saveData(){ popUserList(); popProvinces(); return "showView" ; } /** * 组装用户集合 */ private void popUserList(){ userList = new ArrayList<User>(); User u = null ; for(int i = 0 ; i < 10 ; i ++){ u = new User(); u.setId(1 + i); u.setName("tom" + i); u.setAge(20 + i); userList.add(u); } } /** * 组装province集合数组 */ private void popProvinces(){ provinces = new ArrayList<Area>(); Area p1 = new Area(1,"1号省"); p1.getCities().add(new Area(11,"1.1城市")); p1.getCities().add(new Area(12,"1.2城市")); p1.getCities().add(new Area(13,"1.3城市")); Area p2 = new Area(2,"2号省"); p2.getCities().add(new Area(21,"2.1城市")); p2.getCities().add(new Area(22,"2.2城市")); p2.getCities().add(new Area(23,"2.3城市")); provinces.add(p1); provinces.add(p2); } public boolean isMarried() { return married; } public void setMarried(boolean married) { this.married = married; } public Integer getSelectedId() { return selectedId; } public void setSelectedId(Integer selectedId) { this.selectedId = selectedId; } public List<Area> getProvinces() { return provinces; } public void setProvinces(List<Area> provinces) { this.provinces = provinces; } }
showview.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>showView.jsp</title> <style type="text/css"> .even{ background-color: blue; } .odd{ background-color: yellow; } </style> </head> <body> <br> ------------------------------------------------------------------------<br> request.username = <s:property value="#request.username" /><br> session['username'] = <s:property value="#session.username" /><br> application.username = <s:property value="#application.username" /><br> attr.username = <s:property value="#attr.username" /><br> parameters.username = <s:property value="#parameters.username" /><br> vs.username = <s:property value="username" /><br> vs.username = ${request['struts.valueStack']}<br> <!-- 强制ognl表达式解析 --> <s:textfield name="#session.username" label="%{#request.username}"/><br> ------------------------------------------------------------------------<br> s:property<br> <s:property value="'<hr>tom'" escape="false"/> <br> ------------------------------------------------------------------------<br> s:set:如果没有指定范围,则存放ActioinContext自身的map中,<br> <s:set var="applicationmyname" value="#session['username']" /> <s:property value="applicationmyname" /> <br>------------------------------------------------------------------------<br> s:push,将对象放到栈顶,标签结束后从栈顶删除<br> <s:push value="#application.username"> <s:property/> </s:push> <br>------------------------------------------------------------------------<br> s:bean,将创建新的javabean对象,到栈顶也可指定actionContextmap自身中<br> <s:bean name="cn.itcast.struts2.model.User" var="mybean"> <s:param name="name" value="#request.username" /> <s:property value="name"/> </s:bean> <s:property value="#mybean.name"/> <br>------------------------------------------------------------------------<br> s:action,在jsp直接调用某个action.<br> <s:action name="RegAction_toRegView" namespace="/regns" executeResult="true"/> <br>------------------------------------------------------------------------<br> s:iterator,迭代集合属性的<br> <table border="1"> <tr> <td>count</td><td>index</td><td>name</td><td>age</td><td>状态</td> <td>编辑</td><td>查看</td> </tr> <s:iterator value="userList" var="u" status="st"> <tr class='<s:property value="#st.even?'even':'odd'" />'> <td><s:property value="#st.count" /></td> <td><s:property value="#st.index" /></td> <td><s:property value="#u.name" /></td> <td><s:property value="#u.age" /></td> <td> <s:if test="#u.age < 23">少年</s:if> <s:elseif test="#u.age > 27">老年</s:elseif> <s:else>中年</s:else> </td> <td> <s:url action="UserAction_edit" namespace="/user" var="editUrl" escapeAmp="false"> <s:param name="id" value="#u.id" /> <s:param name="name" value="#u.name" /> </s:url> <a href='<s:property value="#editUrl"/>'>编辑</a> </td> <td><a href='<s:url action="UserAction_view" namespace="/user"><s:param name="id" value="#u.id" /></s:url>'>查看</a></td> </tr> </s:iterator> </table> <br>------------------------------------------------------------------------<br> s:url,访问url资源<br> <s:url includeParams="all" var="myurl"/> <s:property value="myurl"/> <br>------------------------------------------------------------------------<br> s:i18n,s:text通常结合在一起使用,访问指定资源文件中串<br> <s:i18n name="cn.itcast.struts2.action.RegAction_zh"> <s:text name="label.age" var="mytext"/> </s:i18n> <s:property value="#mytext"/> <br>------------------------------------------------------------------------<br> 访问集合的大小以及是否为空<br> userList.size = <s:property value="userList.size"/><br> userList.isEmpty = <s:property value="userList.isEmpty"/> <br>------------------------------------------------------------------------<br> ognl表示定义集合<br> <s:iterator value="{'tom1','hjerry3','tomca5','kking7','jerrli9'}"> <s:property /><br> </s:iterator> <br>------------------------------------------------------------------------<br> ognl定义map集合<br> <s:iterator value="#{100:'tom',200:'jerry',300:'kingkong'}"> key = <s:property value="key" /> : value = <s:property value="value" /><br> </s:iterator> <br>------------------------------------------------------------------------<br> 集合的过滤<br> <s:iterator value="userList.{?#this.age > 25}"> <s:property/><br> </s:iterator> 集合的投影<br> <s:iterator value="userList.{name}"> <s:property/><br> </s:iterator> <br>------------------------------------------------------------------------<br> ognl表达式调用方法<br> <s:property value="sayHello(#session.username)"/> <br>------------------------------------------------------------------------<br> ognl调用任意类的静态方法<br> <s:property value="@cn.itcast.struts2.action.HelloWorldAction@sayStaticHello(#session.username)"/> <br>------------------------------------------------------------------------<br> 主题:<br> <s:form theme="xhtml"> <s:textfield name="username" label="UserName" /> </s:form> <br>------------------------------------------------------------------------<br> s:textfield标签<br> <s:textfield name="username" key="label.name.global" theme="xhtml" value='%{username}'/> <br>------------------------------------------------------------------------<br> s:checkbox<br> <s:checkbox name="married" label="婚否" /> </body> </html>
reg.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>reg.jsp</title> </head> <body> <s:debug /> <s:form namespace="/ui" action="UiAction_reg" method="post" theme="xhtml"> <s:checkbox name="married" label="婚否" /> <s:select list="#{101:'tom',202:'jerry',303:'kingkong'}" label="username" listKey="key" listValue="value"/> <s:select name="selectedId" list="userList" listKey="id" listValue="name" label="UserName"/> <s:radio name="sex" list="#{0:'男',1:'女'}" label="性别" /> <s:checkboxlist name="hobby" list="#{0:'足球',1:'篮球',2:'乒乓球'}" label="爱好"/> <s:doubleselect name="selectProvinceId" list="provinces" listKey="id" listValue="areaName" doubleName="selectedCityId" doubleList="cities" doubleListKey="id" doubleListValue="areaName" label="区域"/> <s:submit /> </s:form> </body> </html>
关联选择 area 区域类
package cn.itcast.struts2.model; import java.util.ArrayList; import java.util.List; /** * 区域类 */ public class Area { private Integer id ; private String areaName ; private String areaNo ; private int type ; /* 省所属城市集合 */ private List<Area> cities = new ArrayList<Area>(); public Area(Integer id, String areaName) { this.id = id; this.areaName = areaName; } public Area(Integer id, String areaName, String areaNo) { this.id = id; this.areaName = areaName; this.areaNo = areaNo; } public Area(Integer id, String areaName, String areaNo, int type) { this.id = id; this.areaName = areaName; this.areaNo = areaNo; this.type = type; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAreaName() { return areaName; } public void setAreaName(String areaName) { this.areaName = areaName; } public String getAreaNo() { return areaNo; } public void setAreaNo(String areaNo) { this.areaNo = areaNo; } public int getType() { return type; } public void setType(int type) { this.type = type; } public List<Area> getCities() { return cities; } public void setCities(List<Area> cities) { this.cities = cities; } }
<s:doubleselect /> 产生的级联菜单是换行的, 可通过如下方式解决 s:doubleselect 换行的问题。
<style type="text/css">
.nobr br{display:none;}
</style>
在有s:doubleselect 的 s:form 表单上套上div
<div class="nobr">
<s:form> ...... </s:form>
</div>
http://blog.csdn.net/shangpusp/article/details/4956011