struts1 logic:iterate bean:write标签使用

只是截取项目中部分代码,供参考及日后查阅

用struts1标签html:select 展现select下拉列表
刚开始为如下代码:
<html:select name="ShuiwujiguanForm" property="swjg_dmSelect" >
         <option value=""></option>
         <html:options collection="select_list" property="swjg_dm" labelProperty="swjg_mc"/>
</html:select>


需要改为按树级分层的结构,如下图:


代码修改为:
<html:select name="ShuiwujiguanForm" property="swjg_dmSelect" >
                     <option value=""></option>
                     <logic:iterate id="select_list" name="ShuiwujiguanForm" property="select_list" scope="request">
                                 <option value="<bean:write name="select_list" property="swjg_dm"/>">
                                     <logic:equal name="select_list" property="swjg_level" value="1">nbsp;nbsp;</logic:equal>
                                     <logic:equal name="select_list" property="swjg_level" value="2">nbsp;nbsp;nbsp;nbsp;</logic:equal>
                                     <logic:equal name="select_list" property="swjg_level" value="3">nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;</logic:equal>
                                     <logic:equal name="select_list" property="swjg_level" value="4">nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp; </logic:equal>
                                 <bean:write name="select_list" property="swjg_mc"/>
                                </option>
                     </logic:iterate>
</html:select>

nbsp;替换为&nbsp;,在这里&nbsp;不显示
logic:equal 进行判断, 级别为1的进行空格,为2的再多空格,依次类推。


logic:iterate用法
id 脚本变量的名称,它保存着集合中当前元素的句柄。 
name 代表了你需要叠代的集合(不知道和property有啥区别),来自session或者request的属性。 
type 是其中的集合类元素的类型

1、name和property都存在的时候有值
<logic:iterate id="select_list" name="ShuiwujiguanForm" property="select_list" scope="request">
</logic:iterate>
遍历的是property="select_list"的值

2、有property无name时报错
<logic:iterate id="select_list "  property="select_list" scope="request">
</logic:iterate>

3、有name无property时有值
<logic:iterate id=" select_list " name="select_list"  scope="request">
</logic:iterate>
遍历的是 name  ="select_list"的值

id保存着集合中当前元素,可随意取值 但iterator标签内的标签一些name属性就要引用这个id值
例如id取值为a:
<logic:iterate id="a" name="ShuiwujiguanForm" property="select_list" scope="request">
                        <option value="<bean:write name="a" property="swjg_dm"/>">
                        		<logic:equal name="a" property="swjg_level" value="1">nbsp;nbsp;</logic:equal>
                        		<bean:write name="a" property="swjg_mc"/>
                        </option>
 </logic:iterate>


bean:write用法
bean:write常用的属性有如下几个:
1、name,用来指定属性的名字
2、filter,用来指定是否屏蔽到属性值的HTML格式
3、property,用来指定name所代表的对象的属性名字
4、format,用来指定显示的时间,数字,日期等的格式

只介绍name与property属性
例1
action中设置了request.setAttribute("hello","hello world");
则在jsp页面中,用struts的write标签取出并显示的方式如下:
<bean:write name="hello"/>,则页面上显示出hello world。

例2
假如有User类和Dept类,User类有属性名字userName,年龄age,和所属的Dept,
Dept类有属性组名deptName,并均具有相应的get和set方法。
某处设置了request.setAttribute("user",new User("张三","23","男",new Dept("开发部")));
则在某个jsp页面中,用struts的write标签取出并按指定方式显示结构体的方法如下:
用户名:<input type="text" value="<bean:write name="user" property="userName"/>">
年龄:<input type="text" value="<bean:write name="user" property="age"/>">
性别:<input type="text" value="<bean:write name="user" property="sex"/>">
组名:<input type="text" value="<bean:write name="user" property="dept.deptName"/>">


我就是靠这个文档实现logic:iterate的循环的<br>struts 标签 logic:iterate使用 logic:iterate<br><br>第一页 是struts官方的说明,<br>第二页 是个例子<br>第三页 是我实现的arrayList放入标签的方法。<br>这是页面文件<br><%@ page language="java"<br> import="java.util.*,cn.edu.bit.zgc2d.accountQuery.*" pageEncoding="GBK"%><br><%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"<br> prefix="bean"%><br><%@ taglib uri="http://jakarta.apache.org/struts/tags-html"<br> prefix="html"%><br><%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"<br> prefix="logic"%><br><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><br><html><br> <head><br> </head><br> <body><br> <table><br> <tr><br> <td><%@ include file="../menu.txt"%><br> </td><br> </tr><br> </table><br> <table width=800><br> <tr><br> <td height=100 width=30><br> <img src=../images/small_2.gif><br> </td><br> <td><br> 账务查询<br> <br><br> 基本信息查询。<br> </td><br> </tr><br> </table><br> <table width=800><br> <tr><br> <td height=1 width=700 bgcolor=gray colspan=2></td><br> </tr><br> </table><br> <table width=800><br> <tr><br> <td height=100 width=30><br> <img src=../images/small_2.gif><br> </td><br> <td><br> 基本信息查询。<br> </td><br> </tr><br> </table><br> <table><br> <tr><br> <td WIDTH=100 bgcolor=#bfbfbf><br> 帐号<br> </td><br> <td WIDTH=100 bgcolor=#bfbfbf><br> 帐户别名<br> </td><br> <td WIDTH=100 bgcolor=#bfbfbf><br> 是否为主帐户<br> </td><br> </tr><br> <logic:iterate id="item" name="list" indexId="index"><br> <tr><br> <td WIDTH=100 bgcolor=F2F2F2><br> <a href="www.sina.com.cn?abc=<bean:write name="item" property="account" />"><bean:write name="item" property="account" /><br> </td><br> <td WIDTH=100 bgcolor=F2F2F2><br> <bean:write name="item" property="other_name" /><br> </td><br> <td WIDTH=100 bgcolor=F2F2F2><br> <bean:write name="item" property="is_main_account" /><br> </td><br> </tr><br> </logic:iterate><br> </table><br> </body><br></html><br>这是action<br>public class InformationAction extends Action {<br> public ActionForward execute(ActionMapping mapping, ActionForm form,<br> HttpServletRequest request, HttpServletResponse response) {<br> InformationForm informationForm = (InformationForm) form;// TODO Auto-generated method stub<br> //业务开始<br> Connection conn = null; Statement st = null; ResultSet rs = null;<br> try {<br> db db = new db();<br> // 打开数据库<br> conn = db.conn();<br> // 创建st<br> st = conn.createStatement();<br> // 组织sql并执行<br> HttpSession session_account = request.getSession();<br> String account = (String) session_account.getAttribute("account");<br> String sql = "select * from account where main_account=" + account<br> + "order by is_main_account desc";<br> rs = st.executeQuery(sql);<br> // 组建arrayList列表<br> ArrayList list = new ArrayList();<br> while (rs.next()) {<br> accountBean bean = new accountBean();<br> bean.setArea(rs.getString("area")); bean.setKind(rs.getInt("kind")); bean.setAccount(rs.getString("account")); bean.setMain_account(rs.getString("main_account")); bean.setType(rs.getInt("type")); bean.setOther_name(rs.getString("other_name")); bean.setPassword(rs.getString("password")); bean.setIs_main_account(rs.getInt("is_main_account")); bean.setMoney(rs.getString("money")); bean.setId(rs.getInt("id"));<br> list.add(bean);<br> }<br> HttpSession session = request.getSession();<br> session.setAttribute("list", list);<br> return mapping.findForward("success");<br> } catch (Exception e) { e.printStackTrace(); } <br> //业务结束<br> return null;<br> }<br>}//自己将需要的包导入<br>bean文件<br>package cn.edu.bit.zgc2d.accountQuery;<br><br>public class accountBean {<br> private String area;<br> private int kind;<br> private String account;<br> private String main_account;<br> private int type;<br> private String other_name;<br> private String password;<br> private int is_main_account;<br> private String money;<br> private int id;<br> public String getArea() {<br> return area;<br> }<br> public void setArea(String area) {<br> this.area = area;<br> }<br> public int getKind() {<br> return kind;<br> }<br> public void setKind(int kind) {<br> this.kind = kind;<br> }<br> public String getAccount() {<br> return account;<br> }<br> public void setAccount(String account) {<br> this.account = account;<br> }<br> public String getMain_account() {<br> return main_account;<br> }<br> public void setMain_account(String main_account) {<br> this.main_account = main_account;<br> }<br> public int getType() {<br> return type;<br> }<br> public void setType(int type) {<br> this.type = type;<br> }<br> public String getOther_name() {<br> return other_name;<br> }<br> public void setOther_name(String other_name) {<br> this.other_name = other_name;<br> }<br> public String getPassword() {<br> return password;<br> }<br> public void setPassword(String password) {<br> this.password = password;<br> }<br> public int getIs_main_account() {<br> return is_main_account;<br> }<br> public void setIs_main_account(int is_main_account) {<br> this.is_main_account = is_main_account;<br> }<br> public String getMoney() {<br> return money;<br> }<br> public void setMoney(String money) {<br> this.money = money;<br> }<br> public int getId() {<br> return id;<br> }<br> public void setId(int id) {<br> this.id = id;<br> }<br>}<br>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值