struts-tags的使用

struts2的常用tags:proterty,set,debug,iterator,bean

背景准备:

index.jsp跳转

<a href="<%=basePath%>yanlu/yan!add?name=1&age=18"> 接受参数的测试,欢迎拍砖</a> <br>

struts.xml中action对应的class为com.yanlu.User

package com.yanlu;

import com.opensymphony.xwork2.ActionSupport;

public class User extends ActionSupport {
	private String name;
	private String age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String add() throws Exception {
		// TODO Auto-generated method stub
		//System.out.print(user.getName());
		//System.out.print(user.getAge());
		//this.addFieldError("error", "error is too long");
		//this.addFieldError("error","error  is  too slow");
		//this.addFieldError("test","what");
		
		return "success";
	}

}

常用标签正式开八。。。

1.s:property的使用

</pre><pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html"><li>user.name:<s:property value="name"></s:property></li>        
        <!--prints the result of user's getName() method. -->  
<li>user.age:<s:property value="#parameters.age"></s:property></li> 
<li>设定默认值:<s:property value="user" default="yanlu"></s:property></li>
        <!-- 在com.yanlu.User中没有找到getUser,使用默认值,且不放入value stack中 -->
<li><s:property value="'</hr>'" escape="false"></s:property></li>  
        <!-- escape的默认值为false escape="false" -->

</pre><pre>
 

user.name在value stack中取到,#parameters.user.name在actionContext中取不到值

name  在value stack 中取到,#parameters.name在action Context中取到

详细查看debug信息

2.s:set的使用

 <li>set的用法<s:set var="myAge" value="age" scope="action"></s:set></li>
  	        <s:property value="myAge" ></s:property>
 <li>set的用法<s:set var="myName" value="name" scope="request"></s:set></li>
  	        <s:property value="#request.myName" ></s:property>
  	                                                                  

       

var声明变量放在stack context中 value为变量的值;

value的值,在com.yanlu.User中getAge()取到;

scope为var的存放位置,default为action ,也可设置为request,application,session

 set设定的值,必 是在action类中定义,有get() 方法才能获取


3.s:debug的使用

<s:debug></s:debug>

4.s:iterator的使用

	<li>遍历集合:<br />
		<s:iterator value="{'aaa', 'bbb', 'ccc'}" >
			<s:property/> |
		</s:iterator></li>
	<li>自定义变量:<br />
		<s:iterator value="{'aaa', 'bbb', 'ccc'}" var="x">
			<s:property value="#x.toUpperCase()"/> |
		</s:iterator></li> 
	<li>使用status:<br />
		<s:iterator value="{'aaa', 'bbb', 'ccc'}" status="status">
			<s:property/> | 
			遍历过的元素总数:<s:property value="#status.count"/> |
			遍历过的元素索引:<s:property value="#status.index"/> |
			当前是偶数?:<s:property value="#status.even"/> |
			当前是奇数?:<s:property value="#status.odd"/> |
			是第一个元素吗?:<s:property value="#status.first"/> |
			是最后一个元素吗?:<s:property value="#status.last"/>
			<br />
		</s:iterator><span style="font-family: Arial, Helvetica, sans-serif;"></li></span>

5.s:bean的使用

<s:bean name="com.yanlu.Dog" var="myDog">          <!-- 声明一个类,并初始化 -->
    <s:param name="sex" value="'M'"></s:param>     <!-- 设置类属性的值为M -->
    <s:property value="sex"></s:property>          <!-- value stack 取属性的值 -->
    <s:property value="#myDog.sex"></s:property>   <!-- action context取属性的值 -->
</s:bean><!-- 结束标签的位置需要注意 -->

value stack 的Object 中创建类 com.yanlu.Dog 

var="myDog" 放在 action context中

name="sex" value="'M'"  放在 value stack 中


注意:  使用标签的时候,查看stuts2的标签文档,注意查看那属性的使用方式,以及是否过时

              属性type 类型为 object,在jsp中被翻译为ognl      <s:property value="name" default="yanlu">

             属性type类型为String,在jsp中注意加上单引号     <s:param name="sex" value="'M'"></s:param>

    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值