struts标签介绍

struts标签介绍

<1>struts标签:logic标签

1.<logic:equal> and so on比较标签
以下eg都是由一个servlet转发过来的
MyBean mb = new MyBean();
mb.setName("yc");
mb.setCount(200);
request.setAttribute("mybean",mb);
request.getRequestDispatcher("jsp1.jsp").forward(request,response);

eg1:<bean:define id="myint" value="100"/>//当前页面定义一个字符串
<logic:equal name="myint" value="100">
myint=100
</logic:equal>

eg2:<bean:define id="bean3" name="mybean" scope="request"/>//从request上获取bean
<logic:lessThan name="bean3" property="count" value="200.a">//比较bean的count属性和200.a
bean3 LessThan 200.a
</logic:lessThan>

注意:如果两个字符串都能够成功转为数字,则是比较数字的大小,否则像eg2就是比较字符串的大小

eg3:<bean:define id="bean1" name="mybean" property="name" scope="request"/>
<logic:equal name="bean1" value="yc">
name1=yc
</logic:equal>

eg4:<bean:define id="bean2" name="mybean" scope="request"/>
<logic:equal name="bean2" property="name" value="yc">
name2=yc
</logic:equal>

注意:eg3和eg4都是比较bean属性,只是写法不同

eg5:比较请求参数

<logic:equal parameter="id" value="150">
<bean:parameter id="myid" name="id"/>
parameter:<bean:write name="myid"/>=150
</logic:equal>

2.<logic:match>
查看指定字符串是否被包含
<logic:match name="" value="被包含字符串" location="start/end也可以不写,就是任意位置"/>

eg:<bean:define id="bean4" name="mybean" property="name" scope="request"/>
<logic:match name="bean4" value="y" location="start">
yc is start with bean4
</logic:match>

3.< logic:iterator>
a.遍历Vector等集合,value形式的
<logic:iterator id="每个集合的元素" name="集合在当前页面的绑定名" indexId="序号"
offset="起始位置,0开始" length="遍历长度"/>

前提: Vector v = new Vector();
v.add("dog");
v.add("girl");
v.add("bird");
v.add("cat");
request.setAttribute("myvector",v);
request.getRequestDispatcher("jsp1.jsp").forward(request,response);


eg1:<bean:define id="vector1" name="myvector" scope="request"/>
<logic:iterate id="element" name="vector1">
<bean:write name="element"/>
<br />
</logic:iterate>

eg2:<bean:define id="vector2" name="myvector" scope="request"/>
<logic:iterate id="element" name="vector2" indexId="index" offset="1" length="3">
<bean:write name="index"/>.<bean:write name="element"/><br />
</logic:iterate>

b.遍历Map等,key/value形式
只是输出的时候有所不同
<bean:write name="" property="key"/>输出元素的key部分
<bean:write name="" property="value"/>输出元素的value部分

前提: HashMap hm = new HashMap();
hm.put("yc","yuechen");
hm.put("tjs","tianjinsong");
hm.put("xc","xiaocen");
hm.put("xk","xukai");
request.setAttribute("mymap",hm);

eg:<bean:define id="map1" name="mymap" scope="request"/>
<logic:iterate id="element" name="map1" indexId="index">
<bean:write name="index"/>.<bean:write name="element" property="key"/>:<bean:write name="element" property="value"/><br />
</logic:iterate>

c.遍历map中的集合
双层iterator,内层iterator需要加入property="value"


前提: HashMap hm2 = new HashMap();
String[]asia = {"china","japan","korea"};
hm2.put("asia",asia);
String[]erop = {"greman","rasa","england"};
hm2.put("erop",erop);
String[]us = {"usa","canada"};
hm2.put("us",us);
request.setAttribute("country",hm2);

eg:<bean:define id="myc" name="country" scope="request"/>
<logic:iterate id="element" name="country" indexId="index">
<bean:write name="index"/>.<bean:write name="element" property="key"/><br />
<logic:iterate id="inelement" name="element" indexId="inindex" property="value">
-----<bean:write name="inindex"/>.<bean:write name="inelement"/><br />
</logic:iterate>
</logic:iterate>

<2>struts标签:bean标签

1.<bean:page>
当前页面获取jsp页面内置对象
<bean:page id="当前页该内置对象的绑定名" property="jsp内置对象名"/>
jsp内置对象:application
session
request
response
config

eg1:<bean:page id="mySession" property="session"/>
输出session创建时间:<bean:write name="mySession" property="creatTime"/>
输出sessionID:<bean:write name="mySession" property="id"/>

eg2: <bean:page id="myApplication" property="application"/>
服务器信息:<bean:write name="myApplication" property="serverInfo"/>

注意:严格区分内置对象属性的大小写,可以用 <% session.XXX %> 看看session都有什么方法


2.<bean:include>
包含资源,将资源用一个变量代替
<bean:include id="当前页面的绑定名" forward="Global-Forward名" />
<bean:include id="当前页面的绑定名" page="jsp页面相对路径,用/开头" />
<bean:include id="当前页面的绑定名" href="完整url" />

显示资源
<bean:write name="绑定名" filter="是否显示原样html标记,默认true,显示,应该设置为false" />

eg1:<bean:include id="jsp2" page="/jsp2.jsp"/>
<bean:write name="jsp2" filter="true"/>

eg2:<bean:include id="jsp3" forward="myForward"/>
<bean:write name="jsp3" filter="false"/>


3.<bean:define>
在toScope作用域(可以不写,默认page)上定义(绑定)一个变量,代表scope作用域(可以不写,默认从page找到application)上的
字符串,或者javabean,或者javabean的属性
输出字符串:value是字符串的值,只有在此时才能用value属性
eg1:<bean:define id="myString" value="yc"/>
<bean:write name="myString"/>
输出bean属性值:
已知当前jsp页面接受控制器发来的如下:
MyBean mb = new MyBean();
mb.setName("jrunner");
request.setAttribute("mybean",mb);
request.getRequestDispatcher("jsp1.jsp").forward(request,response);

eg1:<bean:define id="beanname1" name="mybean" property="name" scope="request"/>
<bean:write name="beanname1"/>

eg2:<bean:define id="beanname2" name="mybean" scope="request"/>
<bean:write name="beanname2" property="name" />
注意:eg1,eg2相当于:<%=((MyBean)(request.getAttribute("mybean"))).getName() %>
注意scope可以不写,但是建议写,严格和request.getAttribute比较

eg3:<bean:define id="beanname3" name="mybean" type="demo.MyBean" scope="request"/>
<bean:write name="beanname3" property="name" />

eg4:<bean:define id="beanname4" name="beanname3" type="demo.MyBean" scope="page"/>
<bean:write name="beanname4" property="name"/>
注意:eg4 的beanname4是从page上面获得的beanname3,type属性可以不写
name:只能是绑定名

4.<bean:size>
输出集合长度

eg:在request上面绑定了HashMap类型的集合,绑定名是mymap,然后转发jsp页面

<bean:size id="length" name="mymap" scope="request"/>
<bean:write name="length"/>

5.<bean:write>
可以按照指定的格式输出,加format属性

eg1:request.setAttribute("myfloat",Float.valueOf("3.1"));

<bean:define id="myf" name="myfloat" scope="request"/>
<bean:write name="myf" format="#.###########"/>
这个时候会得出3.09999990463


6.<bean:parameter>
获得请求参数
一个参数的情况:
<bean:paramter id="当前页面请求参数的绑定名" name="请求参数名" value="请求参数默认值(可以不写)" />
<bean:write name="当前页面请求参数的绑定名" />
n个参数的情况: