struts2标签库常用标签详解

 

struts2标签库常用标签详解

struts2标签主要分两种:

注意:标签的属性可以被赋值为一个静态的值或一个 OGNL 表达式. 如果在赋值时使用了一个 OGNL 表达式并把它用 %{} 括起来, 这个表达式将会被求值.

1.通用标签库(控制标签、数据标签):

2.表单标签库

<s:property> 将OGNL表达式的内容输出到页面

  • value属性,接受OGNL表达式从值栈取值
  • default属性,显示默认值,如果当OGNL表达式没有获取到值,default设置显示默认值
  • escapeHtml属性,是否对HTML标签转义输出(默认是不转义true,可以关闭)

例如:action代码:

package com.cszy.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TagAction1 extends ActionSupport{
	@Override
	public String execute() throws Exception {
		
		ActionContext.getContext().put("name", "admin");
		ActionContext.getContext().put("table", "<table border='1'><tr><td>html的代码</td></tr></table>");
		return SUCCESS;
	}
}

jsp代码:

    <h3>使用s:property获取值栈内容</h3>
    <s:property value="name"/>
    <h3>s:property default属性</h3>
    <s:property value="#age" default="0"/>
  	<h3>s:property escapeHtml属性,是否对html标签转义,默认不转义(true)</h3>
  	<s:property value="#table" escapeHtml="false"/>

页面效果:

<s:iterator>通过s:iterator遍历集合对象

  • value属性,从值栈中获取对应的集合对象
  • var,设置迭代元素的变量

例如:action代码:

package com.cszy.action;

import java.util.ArrayList;
import java.util.List;

import com.cszy.entity.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TagAction2 extends ActionSupport{
	
	private List<User> userlist;
	
	
	public List<User> getUserlist() {
		return userlist;
	}


	@Override
	public String execute() throws Exception {

		ActionContext.getContext().getValueStack().set("userrole","1");
		
		userlist = new ArrayList<>();
		userlist.add(new User("tom","123"));
		userlist.add(new User("admin","123"));
		userlist.add(new User("domain","123"));
		//把数据放入值栈
		return SUCCESS;
	}
	
	
}

jsp代码:

  	<h3>通过s:iterator遍历集合对象</h3>
  	<h4>s:iterator value属性是用来获取值栈中的集合对象</h4>
  	<s:iterator value="userlist" var="demo">
  		<s:property value="#demo.username"/>
  		<s:property value="#demo.pwd"/> -->root
  		<s:property value="username"/>
  		<s:property value="pwd"/> -->EL
  		${demo.username}==>${demo.pwd}
  		<br />
  	</s:iterator>

页面效果:

<s:if><s:elseif><s:else>控制标签

  • 作用:页面判断,其中的test属性可以接收OGNL表达式。

例如:jsp代码:

  	<h4>s:if 页面判断,其中的test属性可以接收OGNL表达式</h4>
  	<s:if test="userrole == 1">
  		<s:property value="判断成功"/>
  	</s:if>

此处假设在action中设置了userrole的值为1,那么这里就会获取到userrole的值为1,1==1,那么就会显示判断成功

<s:a>超链接标签

<!-- action:请求的 URL
namespace:请求的是哪个 namespace 下面的 action
超链接传值方式一
-->
<s:a action="tag3" namespace="/">
<s:param name="name">tom</s:param>
new 请求 1
</s:a><br>
<!-- 超链接传值方式二
错误的传值写法:<s:param name="name" value="igeek"></s:param>
-->
<s:a action="tag3" namespace="/">
<!-- value:ognl 表达式,访问是对象 -->
<s:param name="name" value="'igeek'"></s:param> new 请求 2
</s:a>

用户界面(UI)标签:用户界面标签主要包括两类:表单类标签和其他类标签

<s:form>

作用:生成 form 标签。

属性:

  • action 属性,对应 struts.xml <action> 元素 name 属性;
  • namespace 属性,对象 struts.xml <package>元素 namespace 属性

<s:textfield>, <s:password>, <s:hidden>, <s:textarea>

作用:

  • <s:textfield> 文本框 ,生成 <input type=”text ” >
  • <s:password> 密码域 ,生成<input type=”password” >
  • <s:hidden> 隐藏域 , 生成 <input type=”hidden” >
  • <s:textarea>文本域,生成<textarea></textarea>

属性:

<s:radio>、<s:checkboxlist>、<s:select>

作用:(#构造 map 集合)

  • <s:radio> 接收 list 或者 map 生成一组单选按钮
  • <s:select> 接收 list 或者 map ,生成一组下拉列表
  • <s:checkboxlist> 接收 list 或者 map ,生成一组复选框

综合代码:

	<h3>struts2 form表单</h3>
	<s:form action="tag5" method="post">
		<h4>文本框:<s:textfield name="username"></s:textfield>  </h4>
		<h4>密码框:<s:password name="password"></s:password>  </h4>
		<h4>隐藏域:<s:hidden name="hide" value="hide"></s:hidden> </h4>
		<h4>文本域:<s:textarea name="content"></s:textarea> </h4>
		
		<hr/>
		<h4>单选按钮<s:radio list="{'男','女'}" name="sex"></s:radio> </h4>
		<h4>复选框<s:checkboxlist list="{'篮球','足球','看书'}" name="hobby"></s:checkboxlist> </h4>
		<h4>下拉框<s:select list="{'上海','北京','东京'}" name="city"></s:select> </h4>
		<s:reset value="重置"></s:reset>
		<s:submit value="提交"></s:submit>
	</s:form>

猜你喜欢

转载自blog.csdn.net/qq_39809458/article/details/81166929