struts2(4)----------struts2的标签

xiaoxiao2021-02-28  40

1、作用:用来jsp页面中显示数据(学习时,类比jstl标签)

2、使用:引入标签库<%@taglib prifix="s" uri="/stuts-tags"%>

如:studentAction.java

package com.bz.sqc.action; import com.opensymphony.xwork2.Action; import com.pengsai.entity.Student; import com.pengsai.service.StudentService; import com.pengsai.service.impl.StudentServiceImpl; public class StudentAction { StudentService stus = new StudentServiceImpl(); //查看 public String selectAll(){ try{ list = stus.selectAll(); return Action.SUCCESS; }catch(Exception e){ return Action.ERROR; } } private List<Student> list ; public List<Student> getList() { return list; } public void setList(List<Student> list) { this.list = list; } }

        2.1)展示单个数据<s:property value="OGNL表达式">

<table border="1px solid black" cellpadding="0px" cellspacing="0px"> <tr><th>id</th><th>姓名</th><th>性别</th><th>生日</th><th>星座</th><th>属相</th><th>更新</th><th>删除</th></tr> <s:iterator value="list"> <tr> <td><s:property value="id" /></td> <td><s:property value="name" /></td> <td><s:property value="sex" /></td> <td><s:date name="birthday" format="yyyy-MM-dd"/></td> <td><s:property value="xingzuo" /></td> <td><s:property value="shuxiang" /></td> <td><a href="<s:url value="/student/selectById" />?id=<s:property value="id" />">更新</a></td> <td><a href="<s:url value="/student/deleteById" />?id=<s:property value="id" />">删除</a></td> </tr> </s:iterator> </table>

        2.2)有条件展示数据<s:if  test="OGNL表达式" ></s:if>   <s:elseif test=""><s:elstif>  <s:else></else>

        2.3)展示多个数据<s:iterator value="OGNL表达式"><s:iterator>

                注意:1)使用struts的迭代标签,每次迭代1次,取出1个集合元素,放入root中

                          2)如果集合元素是String、基本类型或包装类型,则输出集合元素内容,使用<s:property/>

                          3)遍历map集合时,使用<s:property value="key" />输出键名,<s:property vakue="key.value">输出值

                补充:begin--开始索引(从1开始)

                          end--结束索引,step--步长

                          status--迭代状态,j结果放在contextMap中,在ONGL使用时必须

                          <s:property value="#s.index"> 循环下标(从0开始)

                          <s:property value="#s.count"> 循环次数(从1开始)

                          <s:property value="#s.even"> 是否为奇数(是为true)

                          <s:property value="#s.odd"> 是否为偶数(是为true)

<s:iterator begin="1" end="10" status="i">         <s:property value="#i.count"/> //通常做分页处理 </s:iterator>

        2.4)格式化日期标签<s:date name="OGNL表达式"  format="yyyy-MM-dd">

        2.5)动态获取资源标签<s:url value="/子目录/xx.jsp">

        2.6)通过jsp页面加载发送请求标签<s:action name=""  namespace="" executeResult="" var=""/>

<s:action>常用于做回显数据(下拉菜单的回显非常方便):

<s:action name="selectByUserId" namespace="/address" executeResult="false" var="addresses"/>

name:action中方法的名字,即xml中配置的method

namespace:xml中配置的工作空间namespace配置

executeResult:,指定是否将Action的处理结果包含到本页面中.默认值为false,不包含.

var:取出的数据放在ContextMap中,使用时#address.[action中定义的传值变量].属性值

例如:电商网站地址回显 <s:action name="selectByUserId" namespace="/address" executeResult="false" var="addresses"/> <ul class="shdz_con"> <p>▪ 收货地址</p> <input type="hidden" name="addreddid" value="<s:property value="#addresses.adds.id"/>" /> <li><label><strong>*</strong>收  货  人:</label><input type="text" name="ship_man" value="<s:property value="#addresses.adds.receiveperson"/>" /><span id="spn_ship_man" class="hint new_tip" style="display: block;">请填写收货人姓名</span></li> <li><label><strong>*</strong>详细地址:</label><input type="text" name="ship_man" value="<s:property value="#addresses.adds.detailaddress"/>"/></li> <li><label><strong>*</strong>邮政编码:</label><input type="text" name="ship_man" value="<s:property value="#addresses.adds.zipcode" />" /></li> <li><label><strong>*</strong>手      机:</label><input type="text" name="ship_man" value="<s:property value="#addresses.adds.phone"/>" /><label></li> </ul>       2.7)<s:debug>标签,帮助查看root和contextMap区中的数据

转载请注明原文地址: https://www.6miu.com/read-2620549.html

最新回复(0)