﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-程序人生-文章分类-java</title><link>http://www.cppblog.com/pxxfts/category/17802.html</link><description>程序人生</description><language>zh-cn</language><lastBuildDate>Mon, 03 Oct 2011 19:29:14 GMT</lastBuildDate><pubDate>Mon, 03 Oct 2011 19:29:14 GMT</pubDate><ttl>60</ttl><item><title>Struts2标签(转)  </title><link>http://www.cppblog.com/pxxfts/articles/156612.html</link><dc:creator>程序狂人</dc:creator><author>程序狂人</author><pubDate>Fri, 23 Sep 2011 05:00:00 GMT</pubDate><guid>http://www.cppblog.com/pxxfts/articles/156612.html</guid><wfw:comment>http://www.cppblog.com/pxxfts/comments/156612.html</wfw:comment><comments>http://www.cppblog.com/pxxfts/articles/156612.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/pxxfts/comments/commentRss/156612.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/pxxfts/services/trackbacks/156612.html</trackback:ping><description><![CDATA[<h3 class="title pre fs1"><span class="tcnt"><font size="5" face="微软雅黑">Struts2标签(转)</font></span>&nbsp;&nbsp;<span class="bgc0 fc07 fw0 fs0"></span></h3>
<p style="line-height: 20px" class="tdep clearfix nbw-act fc06"><span class="pleft"><span class="blogsep">2009-12-14 21:53:17</span><span class="blogsep">|&nbsp;&nbsp;分类：</span> <a class="fc03 m2a" title="Struts2" href="http://hbluojiahui.blog.163.com/blog/#m=0&amp;t=1&amp;c=fks_087069080084081065082094084095087087081069080081080068"><font color="#58992a">Struts2</font></a> <span id="$_blogTagTitle" class="blogsep phide">|&nbsp;&nbsp;标签：</span><span id="$_blogTagInfo" class="fc03 phide"></span> </span><span class="pright fc07 ztag"><span class="blogsep">|</span><span id="$_fontswitch" class="zihao fc03" __1316753990421__="ev_8147571842"><font color="#58992a">字号</font><span id="$_fontsTypes" class="zihaoshow  phide"><span class="zihaoc bdc0"><span class="stag" __1316753990421__="ev_1421531220" __tabkey__="true" index="0"></span><span class="fc04 stag" __1316753990421__="ev_1221799625" __tabkey__="true" index="1">大</span><span class="fc04 stag selected js-fcurrent fc05" __1316753990421__="ev_7253150936" __tabkey__="true" index="2">中</span><span class="fc04 stag" __1316753990421__="ev_7246987817" __tabkey__="true" index="3">小</span></span></span></span></span><span id="$_blog_subscribe" class="pright pnt fc03" __1316753990421__="ev_2226564460"><span class="iblock icn0 icn0-919">&nbsp;</span><a class="m2a">订阅</a></span> </p>
<div></div>
<div class="nbw-blog-start"></div>
<div class="bct fc05 fc11 nbw-blog ztag js-fs2" __1316753990421__="ev_2130854688">
<p style="text-indent: 2em">1：输出act<wbr>ion中属性值，在jsp页面中显示用：&lt;s:property value="属性名" /&gt; </p>
<p style="text-indent: 2em">2：在页面中实现自动增加的序号用iterator的statuts的index属性 eg： </p>
<p style="text-indent: 2em">&lt;s:iterator value="#request.inOutAccountList" id="da<wbr>ta" status="listStat"&gt; </p>
<p style="text-indent: 2em">&lt;s:property value="#listStat.index+1"/&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;输出当前迭代元素的索引&#224; </p>
<p style="text-indent: 2em">&lt;s:property value="#listStat.index"/&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;输出当前迭代了几个元素&#224; </p>
<p style="text-indent: 2em">&lt;s:property value="#listStat.count"/&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;返回当前迭代元素的索引是否为奇数&#224; </p>
<p style="text-indent: 2em">&lt;s:property value="#listStat.odd"/&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;返回当前迭代元素的索引是否为偶数&#224; </p>
<p style="text-indent: 2em">&lt;s:property value="#listStat.event"/&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;返回当前元素是否为第一个元素&#224; </p>
<p style="text-indent: 2em">&lt;s:property value="#listStat.first"/&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;返回当前元素是否为最后一个元素&#224; </p>
<p style="text-indent: 2em">&lt;s:property value="#listStat.last"/&gt; </p>
<p style="text-indent: 2em">&lt;/s:iterator&gt; </p>
<p style="text-indent: 2em">3:在act<wbr>ion类中取得request和session对象的方法 </p>
<p style="text-indent: 2em">Map session = ActionContext.getContext().getSession(); </p>
<p style="text-indent: 2em">HttpServletRequest request = ServletActionContext.getRequest (); </p>
<p style="text-indent: 2em">设置它们的值的方法: </p>
<p style="text-indent: 2em">session.put("operation", "add"); </p>
<p style="text-indent: 2em">request.setAttribute("name", name); 页面中取得它们的值: </p>
<p style="text-indent: 2em">&lt;s:property value="#session.operation"/&gt; </p>
<p style="text-indent: 2em">&lt;s:property value="#session[&#8216;operation&#8217;]"/&gt; </p>
<p style="text-indent: 2em">${sessionScope.TEST} == ${session.TEST} </p>
<p style="text-indent: 2em">&lt;s:if test="#session.operation =='add'"&gt; </p>
<p style="text-indent: 2em">&lt;s:if test="#session[&#8216;operation&#8217;]=='add'"&gt; </p>
<p style="text-indent: 2em">&lt;s:if test="#session[&#8216;operation&#8217;]==null"&gt; </p>
<p style="text-indent: 2em">&lt;s:property value="#request.name"/&gt; </p>
<p style="text-indent: 2em">${requestScope.name} == ${request.name} </p>
<p style="text-indent: 2em">4:页面中奇偶行样式不一样的控制方法： </p>
<p style="text-indent: 2em">&lt;tr class="&lt;s:if test='#listStat.odd==true'&gt;tableStyle-tr1&lt;/s:if&gt;&lt;s:else&gt;tableStyle-tr2&lt;/s:else&gt;" &gt; </p>
<p style="text-indent: 2em">5:单选框和复选框的使用方法 </p>
<p style="text-indent: 2em">1):可以设置默认选中值，注意list的值的设置，通过这种方式使key和value不一样，这种方法比较常用(checkboxlist or radio) </p>
<p style="text-indent: 2em">&lt;s:radio name="uncarInsPolicy.policyStateCode" </p>
<p style="text-indent: 2em">list="#{'5':'通过' , '2':'不通过'}" listKey="key" listValue="value" value='5' /&gt; </p>
<p style="text-indent: 2em">2):这里的key和value的值是一样的(checkboxlist or radio) </p>
<p style="text-indent: 2em">&lt;s:checkboxlist list="{'Red', 'Blue', 'Green'}" name="favoriteColor"/&gt; </p>
<p style="text-indent: 2em">6:struts2 中的标签会生成类似由&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;构成的字串（具体什么标签生成什么，可以查 看生成后的页面的源代码）如果不限制这些多余代码的生成，页面将变得无法控制，所以一般我们是不希望它生成多余的代码的，具体的设置方法如果，在 struts.xml中统一配置 </p>
<p style="text-indent: 2em">&lt;constant name="struts.ui.theme" value="simple"/&gt;加上该句即可 </p>
<p style="text-indent: 2em">也可以通过在页面中将tag的theme属性设为"simple"取消其默认的表格布局 </p>
<p style="text-indent: 2em">不过最好是：自定义一个theme，并将其设为默认应用到整个站点，如此一来就可以得到统一的站点风格 </p>
<p style="text-indent: 2em">7：jsp页面中格式化日期的方法 &lt;s:date name="unCarInsModificationInfo.createTime" format="yyyy-MM-dd" nice="false"/&gt;这样就可以将日期格式化为yyyy－MM－dd的形式 </p>
<p style="text-indent: 2em">8：默认情况下，当请求act<wbr>ion发生时，Struts运行时（Runtime）根据struts.xml里的Act<wbr>ion映射集(Mapping)，实例化act<wbr>ion对应的类，并调用其execute方法。当然，我们可以通过以下两种方法改变这种默认调用 1）在classes/sturts.xml中新建Act<wbr>ion，并指明其调用的方法 </p>
<p style="text-indent: 2em">比如想调用act<wbr>ion类中的 </p>
<p style="text-indent: 2em">public String aliasAction() { </p>
<p style="text-indent: 2em">message ="自定义Act<wbr>ion调用方法"; </p>
<p style="text-indent: 2em">return SUCCESS; </p>
<p style="text-indent: 2em">} </p>
<p style="text-indent: 2em">则在classes/sturts.xml中加入下面代码： </p>
<p style="text-indent: 2em">&lt;act<wbr>ion name="AliasHelloWorld" class="tutorial.HelloWorld"method="aliasAction"&gt; </p>
<p style="text-indent: 2em">&lt;result&gt;/HelloWorld.jsp&lt;/result&gt; </p>
<p style="text-indent: 2em">&lt;/act<wbr>ion&gt; </p>
<p style="text-indent: 2em">既可用act<wbr>ion名调用该方法了 </p>
<p style="text-indent: 2em">2）（比较常用） </p>
<p style="text-indent: 2em">访问Act<wbr>ion时，在Act<wbr>ion名后加上&#8220;!xxx&#8221;（xxx为方法名）。 </p>
<p style="text-indent: 2em">9：Struts 2.0有两个配置文件，struts.xml和struts.properties都是放在WEB-INF/classes/下。 </p>
<p style="text-indent: 2em">struts.xml用于应用程序相关的配置 </p>
<p style="text-indent: 2em">struts.properties用于Struts 2.0的运行时（Runtime）的配置 </p>
<p style="text-indent: 2em">10:在act<wbr>ion类中取得web下某一文件夹物理路径（绝对路径）的方法 </p>
<p style="text-indent: 2em">filePath = ServletActionContext.getServletContext().getRealPath("/upLoadFiles") </p>
<p style="text-indent: 2em">11：要想返回的页面不是一个直接JSP页面而是要先通过返回act<wbr>ion中的方法读取相应的数据再返回到jsp页面，有两种方法 </p>
<p style="text-indent: 2em">1）在struts.xml中这么设置 </p>
<p style="text-indent: 2em">&lt;result name="list" type="redirect-act<wbr>ion"&gt;sysmanage/UserBaseInfoAction!findUserBaseInfo.act<wbr>ion&lt;/result&gt; </p>
<p style="text-indent: 2em">2）在act<wbr>ion中返回时直接调用这个方法即可 </p>
<p style="text-indent: 2em">return findList（）； </p>
<p style="text-indent: 2em">12：设置checkboxlist中默认值的方法 </p>
<p style="text-indent: 2em">&lt;s:checkboxlist name="skills1"="Skills 1" list="{ 'Java', '.Net', 'RoR', 'PHP' }" value="{ 'Java', '.Net' }" /&gt; </p>
<p style="text-indent: 2em">&lt;s:checkboxlist name="skills2" </p>
<p style="text-indent: 2em">label="Skills 2" list="#{ 1:'Java', 2: '.Net', 3: 'RoR', 4: 'PHP' }" listKey="key" listValue="value" value="{ 1, 2, 3 }"/&gt; </p>
<p style="text-indent: 2em">13：二级级连下拉框 &lt;s:set name="foobar" </p>
<p style="text-indent: 2em">value="#{'Java': {'Spring', 'Hibernate', 'Struts 2'}, '.Net': {'Linq', ' ASP.NET 2.0'}, 'Database': {'Oracle', 'SQL Server', 'DB2', 'MySQL'}}" /&gt; </p>
<p style="text-indent: 2em">&lt;s:doubleselect list="#foobar.keySet()" doubleName="technology" doubleList="#foobar[top]" label="Technology" /&gt; </p>
<p style="text-indent: 2em">14:在页面中取出list对象中的属性值 &lt;s:if test="list!=null&amp;&amp;list.size()&gt;0"&gt; </p>
<p style="text-indent: 2em">&lt;s:iterator value="list" status=&#8221;stat&#8221;&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;如果list中存储形式为select id,name,title-- &gt; </p>
<p style="text-indent: 2em">&lt;s:property value="list[#stat.index][0]" /&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;如果list中存储多对象如left join Role left join Group-- &gt; </p>
<p style="text-indent: 2em">&lt;s:property value="list[#stat.index][0].title" /&gt; </p>
<p style="text-indent: 2em">&lt;s:property value="list[#stat.index][1].title" /&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;如果为Map形式存储-- &gt; </p>
<p style="text-indent: 2em">&lt;s:iterator value="mapUsers.keySet()" id="id"&gt; </p>
<p style="text-indent: 2em">&lt;s:property value="#id"/&gt; </p>
<p style="text-indent: 2em">&lt;s:iterator value="mapUsers.get(#id)"&gt; </p>
<p style="text-indent: 2em">&lt;s:property value="nickname"/&gt;||&lt;s:property value="UName"/&gt; </p>
<p style="text-indent: 2em">&lt;/s:iterator&gt; </p>
<p style="text-indent: 2em">&lt;/s:iterator&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;判断是否为奇数-- &gt; </p>
<p style="text-indent: 2em">&lt;s:if test="stat.odd == true"&gt;&lt;/s:if&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;判断是否为第一条数据-- &gt; </p>
<p style="text-indent: 2em">&lt;s:if test="#stat.index==0"&gt; </p>
<p style="text-indent: 2em">&lt;s:if test="#stat.first"&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;判断属性是否为null-- &gt; </p>
<p style="text-indent: 2em">&lt;s:if test="list[#stat.index][0].title ==null&#8221;&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;将属性值声明为变量-- &gt; </p>
<p style="text-indent: 2em">&lt;s:set name="title" value="list[#stat.index][0].title " &gt;&lt;/s:set&gt; </p>
<p style="text-indent: 2em">&lt;!&#8212;判断属性值-- &gt; </p>
<p style="text-indent: 2em">&lt;s:if test="list[#stat.index][0].title ==4"&gt; </p>
<p style="text-indent: 2em">&lt;s:if test="list[#stat.index][0].title ==&#8217;admin&#8217;"&gt; </p>
<p style="text-indent: 2em">&lt;s:if test="#title ==4"&gt; </p>
<p style="text-indent: 2em">&lt;s:if test="list[#stat.index][0].title.length()&gt;15"&gt; </p>
<p style="text-indent: 2em">&lt;s:property value="list[#stat.index][0].title.substring(0,15)" /&gt;... </p>
<p style="text-indent: 2em">&lt;/s:if&gt; </p>
<p style="text-indent: 2em">14:获取select值 </p>
<p style="text-indent: 2em">&lt;s:select list="listDept" name="selectDept" listKey="val" listValue="prntext" disabled = "true" value="deptSelected" /&gt; </p>
<p style="text-indent: 2em">&lt;s:hidden name="selectDept" value="%{deptSelected}"/&gt; </p>
<p style="text-indent: 2em">15:循环指定int型数据 </p>
<p style="text-indent: 2em">其中first属性指定循环起始值，last指定循环终止值，其它相关属性可以查看org.apache.struts2.util.Counter类源码。在下面迭代器中输入循环的当前值，即：current </p>
<p style="text-indent: 2em">&lt;s:bean name="org.apache.struts2.util.Counter"&gt; </p>
<p style="text-indent: 2em">&lt;s:param name="first" value="1" /&gt; </p>
<p style="text-indent: 2em">&lt;s:param name="last" value="pages" /&gt;&lt;!&#8212;pages page为act<wbr>ion中属性&#224; </p>
<p style="text-indent: 2em">&lt;s:iterator&gt; </p>
<p style="text-indent: 2em">&lt;s:if test="current-1==page"&gt; </p>
<p style="text-indent: 2em">&lt;font color="red"&gt;&lt;s:property /&gt;&lt;/font&gt; </p>
<p style="text-indent: 2em">&lt;/s:if&gt; </p>
<p style="text-indent: 2em">&lt;s:else&gt; </p>
<p style="text-indent: 2em">[&lt;s:property /&gt;] </p>
<p style="text-indent: 2em">&lt;/s:else&gt; </p>
<p style="text-indent: 2em">&lt;/s:iterator&gt; </p>
<p style="text-indent: 2em">&lt;/s:bean&gt; </p>
<p style="text-indent: 2em">16:输出国际化文件中的值 </p>
<p style="text-indent: 2em">&lt;s:text name=&#8221;key&#8221;&gt; </p>
<p style="text-indent: 2em">${getText[&#8220;key&#8221;]} </p>
<p style="text-indent: 2em">17:输出Act<wbr>ion中的消息 </p>
<p style="text-indent: 2em">&lt;s:actionmessage /&gt; </p>
<p style="text-indent: 2em">18:输出Act<wbr>ion中的错误 </p>
<p style="text-indent: 2em">&lt;s:actionerror /&gt; </p></div><img src ="http://www.cppblog.com/pxxfts/aggbug/156612.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/pxxfts/" target="_blank">程序狂人</a> 2011-09-23 13:00 <a href="http://www.cppblog.com/pxxfts/articles/156612.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>