1 / 25

第四章

第四章. Struts 标签库. 回顾. 下面这个异常是怎么回事?该怎么解决?. 预习检查. <input type="text" /> 对应的 Struts 标签是什么? 循环处理集合数据的 Struts 标签是什么? <bean:size> 标签的用途是什么?. 本章任务. 升级房屋出租系统: 实现房屋信息查询功能. 1 、普通查询: 点查询后,仍返回当前页面,查询条件保留。 2 、高级查询: 使用下拉框选择区县和户型查询条件。查询后返回查询页面,保留查询条件。 3 、用户登录后才可使用高级查询。

sibyl
Download Presentation

第四章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第四章 Struts标签库

  2. 回顾 • 下面这个异常是怎么回事?该怎么解决?

  3. 预习检查 • <input type="text" /> 对应的Struts标签是什么? • 循环处理集合数据的Struts标签是什么? • <bean:size>标签的用途是什么?

  4. 本章任务 • 升级房屋出租系统: • 实现房屋信息查询功能 1、普通查询: 点查询后,仍返回当前页面,查询条件保留。 2、高级查询: 使用下拉框选择区县和户型查询条件。查询后返回查询页面,保留查询条件。 3、用户登录后才可使用高级查询。 4、在用户自己发布的租房信息旁显示“编辑”和“删除”链接。

  5. 本章目标 • 掌握html标签 • 掌握bean标签 • 掌握logic标签

  6. 使用Html标签实现简单查询 • 实现简单查询需求 1、查询条件为空,则查询全部租房信息 2、点查询后,显示查询结果,查询条件保留。 输入查询条件,点查询 转到查询结果页面,查询条件仍然保留

  7. “常规”实现方法 • 如何实现上述功能? DAO public List getFwxxList(String title){ String sql = "select … from fwxx where 1=1 "; if (null!= title && ! title.trim().equals("") ){ sql += "and title like '%" + title + "%' "; } sql += " order by date desc " ; //执行Sql 返回结果 } FwxxListForm myForm = (FwxxListForm) form; String title = myForm.getTitle(); List list = this.fwxxBiz.getFwxxList(title); request.setAttribute("list", list); request.setAttribute("title", title); return mapping.findForward("list"); 查询条件不输,则查询全部 保存查询条件 Action 显示上次输入的查询条件 <input type="text" name="title" value="${title}" /> <c:forEach items="${list}" var="fwxx"> <tr> <td><a href='detail.do?fwid=${fwxx.fwid}'>${fwxx.title}</a></td> <td align='center' style='height:30px;'>${fwxx.zj}元</td> <td align='center'><c:out value="${fwxx.date}"/></td> </tr> </c:forEach> 使用Struts标签,可以省掉这些代码 页面:list.jsp

  8. 如何使用Html:text标签 引入Struts标签 list.jsp • 使用标签简化Struts开发 • <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> • <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> • <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> • … • <html:form action="list" method="post"> • <html:text property="title" /> • … • </html:form> title:FwxxListForm的属性 public class FwxxListForm extends ActionForm { private String title; //getter and setter … } 演示示例:使用html:text标签 FwxxListForm.java

  9. Html:select标签 public class FwxxListForm extends ActionForm { private FWXX condition = new FWXX(); //getter and setter … } • 如何实现如图所示的高级查询? • DAO层接口怎么设计? getFwxxList(String title,int qxid,int jdid,int zj1,int zj2,…) • 如此多查询条件,ActionForm怎么设计? • 对下拉框(如区县),如 何实现“保存查询条件”功能? 推荐做法:使用实体对象传参: getFwxxList(FWXX condition) ? <% for (int i=0;i<options.length;++i){ if (jdid==options[i]){ out.println("<option selected='selected' >" + options[i]+"</option>"); }else{ out.println("<option>"+options[i]+"</option>"); } } ? 使用html:select将自动绑定下拉框的值

  10. 实现x室x厅查询 public class FwxxListForm extends ActionForm { private FWXX condition = new FWXX(); //getter and setter … … } 使用html:select标签实现 <html:select property="condition.shi"> <html:option value="0">不限-- </html:option> <html:option value="1">1</html:option> <html:option value="2">2</html:option> <html:option value="3">3</html:option> </html:select>室 <html:select property="condition.ting"> <html:option value="0">不限-- </html:option> <html:option value="1">1</html:option> <html:option value="2">2</html:option> </html:select> 厅 绑定到Form属性, 类似EL表达式的语法 FwxxListForm.java 使用html:option实现选项 list.jsp 演示示例:使用html:select标签

  11. html:optionsCollection标签 • 区县下拉框中的选项是从数据库中读取出来的,怎么实现呢? 1、Form public class FwxxListForm extends ActionForm { ... private List qxList = new ArrayList(); //getter and setter … } 2、Action List qxList = this.qxBiz.getQXList(); myForm.setQxList(qxList); 3、list.jsp <html:select property="condition.qxid"> <html:optionsCollection property="qxList" label="qx" value="qxid" /> </html:select> 演示示例:使用html:optionsCollection标签

  12. 小结 实体类 public class User{ private String name; private String from;//籍贯 //getter and setter ... } • 要实现如下的用户信息编辑功能,请写出ActionForm代码,并用Struts标签写出页面代码。 • 实体类:User • Action:UserAction • Form: UserForm

  13. Logic标签 • 如何增加如下功能: • 用户登录后才提供高级搜索功能,否则点“高级搜索”弹出对话框提示“请先登录”。 在页面上嵌入java脚本实现: <%if (session.getAttribute("user") != null) {%> <!-- 登录后显示的页面代码 --> ... <%} else {%> <!-- 未登录显示的页面代码 --> ... <%}%> 使用logic:empty标签可以让代码更简洁

  14. Logic:empty标签 scope属性指定查找bean的范围 • 采用标签的方式实现: <logic:empty name="user" scope="session"> <span onclick="alert('请先登录') ">高级搜索</span> </logic:empty> <logic:notEmpty name="user" scope="session"> <span onclick="switch('advSearch')">高级搜索</span> </logic:notEmpty> <logic:notEmpty>: 判断指定的变量不为null,并且不为空字符串 <logic:empty>: 判断指定的变量是否为null,或者为空字符串 通过name属性和可选的property属性查找Bean。 如果没有指定scope属性,则按照page、request、session 、application的顺序查找。

  15. logic:equal标签 • 在用户自己发布的租房信息的标题后显示“编辑”和“删除”的链接。 <logic:equal>标签: 根据name和property指定的Bean的值和value属性做比较,相等则显示标签内的内容 • <c:forEach items="${list}" var="fwxx"> • ... • <a href='detail.do?fwid=${fwxx.fwid}'>${fwxx.title}</a> • <logic:equal name="fwxx" property="uid" value="${sessionScope.user.uid}"> • [<a href="...">编辑</a>] [<a href="...">删除</a>] • </logic:equal> • ... • </c:forEach> 使用logic:equal标签 对应Bean的名字 对应Bean的属性 相比较的值,可以是常量(如:"521"),JSP表达式(如:<%=expr %>)或EL表达式。

  16. logic:iterate标签 • 仅仅为了使用<c:forEach>就要引入相应的tld和jar文件。Struts标签中提供类似功能的标签吗? <c:forEach items="${list}" var="fwxx"> ${fwxx.zj}元 <c:out value="${fwxx.date}"/> </c:forEach> <logic:iterate id="fwxx" name="list"> ${fwxx.zj}元 <c:out value="${fwxx.date}"/> </logic:iterate> id属性和c:forEach的var属性类似,会在pageScope内声明同名的Bean

  17. Struts标签通用属性 • 总结:Struts标签的通用属性

  18. 小结 • 编写页面代码实现: 1、显示注册用户列表(使用logic:iterate标签) 2、status值为1为有效用户,值为0为无效用户。现要求列表中只显示有效用户 对应Action有如下代码: List userList = userBiz.getUserList(); request.setAttribute("data",userList); 实体类: public class User{ private String name; private String from;//籍贯 private int status; //getter and setter }

  19. bean标签 • 如何按照下图的要求实现数字和日期的格式化?

  20. bean:write标签 • bean:write标签用于输出文本到页面 <logic:iterate id="fwxx" name="list"> ${fwxx.zj}元 <c:out value="${fwxx.date}"/> </logic:iterate> 格式化字符串 <logic:iterate id="fwxx" name="list"> <bean:write name="fwxx" property="zj" format="#,###" />元 <bean:write name="fwxx" property="date" format="yyyy年MM月dd日" /> </logic:iterate>

  21. bean:define标签 • 怎样实现:“共找到 x条记录”? ? 共找到 <%=list.size() %>条记录 list是保存在request的attribute中Bean的名字,不能直接在JSP脚本中做变量名引用 这样是可以的,不过太繁琐了。 有简单的办法么? 共找到 <%=((java.util.List)request.getAttribute("list")).size()%>条记录

  22. bean:define标签 • bean:define定义后,就可以在JSP脚本中引用了 id:变量名或pageScope属性名 type:类型 <bean:define id="dataList" name="list" type="java.util.List" scope="request" /> 共找到 <%=dataList.size()%>条记录 name、property、scope属性含义与其他Struts标签相同 bean:define标签没有输出,根据其name和property属性取得Bean并重新定义,以其id属性为名,定义变量供JSP脚本使用,并定义pageScope的Bean供其他标签使用

  23. bean:size标签 • 使用bean:size标签,还可以更简单: 与bean:define标签类似,bean:size标签没有输出 <bean:size id="count" name="list" /> 共找到 ${count }条记录 bean:size标签以其id属性为名,重新定义变量,变量的值为通过name和property取得的集合元素的元素个数

  24. 总结 • 简述下列标签的用法和用途 • html:text • html:select • html:option • html:optionsCollection • logic:empty • logic:equal • logic:iterate • bean:write • bean:define • bean:size • 指出下列标签属性的含义 • name • property • scope • id • type • format

More Related