struts标签

//index.jsp
访问属性
<a href="tags?username=u&password=p">tags</a>


//tags.jsp
<ol><!-- 通用标签property,set,bean,include, -->
			<li><!-- property 标签 -->
				property:
				<s:property value="username" /><!-- username是OGNL表达式 -->
			</li>
			<li>
				property 取值为字符串:
				<s:property value="'username'" /> <!-- 'username' 是 字符串 -->
			</li>
			<li>
				property 设定默认值:
				<s:property value="admin" default="管理员" /> <!-- 取不到值则取默认值 -->
			</li>
			<li>
				property 设定HTML:
				<s:property value="'<hr/>'" escape="false" /><!-- escape默认为true,表示要解析为字符串 -->
			</li>
			<hr />
			<li><!-- set 标签,可以用于变量换名  -->
				set 设定adminName值(默认为request 和 ActionContext):
				<s:set var="adminName" value="username" />
			</li><!-- 没有设置scope时,将默认会把变量放到request和ActionContext中 -->

			<li>
				set 从request取值:
				<s:property value="#request.adminName" />
			</li>
			<li>
				set 从ActionContext取值:
				<s:property value="#adminName" />
			</li>

			<%--<li>set 设定范围: 
			        <s:set name="adminPassword" value="password" scope="page"/>
			</li>                   <!-- scope = "page" 表示只在当前页面有效 -->
		        <li>set 从相应范围取值: <!-- 从页面中取值 -->
		        <%=pageContext.getAttribute("adminPassword") %></li>
		--%>
			<li>
				set 设定var,范围为ActionContext:
				<s:set var="adminPassword" value="password" scope="session" />
			</li><!-- scope="session" 表示将变量放入session,不会放入ActionContext,所以用#拿不到值 -->
			<li>
				set 使用#取值:<!-- 用#取ActionContext中的值 -->
				<s:property value="#adminPassword" />
			</li>
			<li>
				set 从相应范围取值: <!-- 从session中取值 -->
				<s:property value="#session.adminPassword" />
			</li>
			<hr />
			<%--<li>push: <!-- push标签 -->
				<s:set name="myDog" value="new com.bjsxt.struts2.ognl.Dog('oudy')">
				</s:set> 
			</li>
	     	<li>
				   push:<s:push value="#myDog">
					<s:property value="name"/>
				</s:push>
			</li>
				<li>push: <s:property value="name"/></li>
		--%>
			<hr />
			<li><!-- bean 标签 -->
				bean 定义bean,并使用param来设定新的属性值:
				<s:bean name="com.bjsxt.struts2.tags.Dog">
					<s:param name="name" value="'pp'"></s:param>
					<s:property value="name" /> <!-- s:property表示拿值栈的内容 -->
				</s:bean>
			</li>
			<li>
				bean 查看debug情况:<!-- 使用var定义变量,会将变量放到ActionContext,在其他地方可以用#访问 -->
				<s:bean name="com.bjsxt.struts2.tags.Dog" var="myDog">
					<s:param name="name" value="'oudy'"></s:param>
				</s:bean>  <!-- "oudy"表示OGNL表达式 ,"'oudy'"表示字符串-->
				拿出值:
				<s:property value="#myDog.name" /><!-- 用#取上面定义的变量myDog -->
			</li>
			<hr />
			<li><!-- s:include标签处理中文时特别麻烦,有bug,得靠运气 -->
				include _include1.html 包含静态英文文件
				<s:include value="/_include1.html"></s:include>
			</li>
			<li>
				include _include2.html 包含静态中文文件
				<s:include value="/_include2.html"></s:include>
			</li>
			<li>
				include _include1.html 包含静态英文文件,说明%用法
				<s:set var="incPage" value="%{'/_include1.html'}" />
				<s:include value="%{#incPage}"></s:include>
			</li><!-- "%{#incPage}"(%{})表示强制将#incPage当成OGNL表达式 -->
			<hr />
			<li><!-- 控制标签 -->
				if elseif else: age =
				<s:property value="#parameters.age[0]" /> <!-- 取第一个age -->
				<br />
				<s:set var="age" value="#parameters.age[0]" /><!-- set标签定义变量 -->
				<s:if test="#age < 0">wrong age!</s:if> <!-- 小于0 -->
				<s:elseif test="#parameters.age[0] < 20">too young!</s:elseif><!-- 小于20 -->
				<s:else>yeah!</s:else>
				<br />
				<s:if test="#parameters.aaa == null">null</s:if><!-- 判断参数aaa,等于null不存在  -->
			</li>
			<hr />
			<li>
				遍历集合:<!-- s:iterator -->
				<br />
				<s:iterator value="{1, 2, 3}">
					<s:property /> |  <!-- s:property 逐个遍历 -->
				</s:iterator>
			</li>
			<li>
				自定义变量:
				<br />  <!-- var="x" 定义临时遍量 -->
				<s:iterator value="{'aaa', 'bbb', 'ccc'}" var="x">
					<s:property value="#x.toUpperCase()" /> |<!-- 逐个遍历拿给变量x,放到ActionContext,用#访问 -->
				</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>
			</li>
			<li>               <!-- key:value 用:隔开 -->
				<s:iterator value="#{1:'a', 2:'b', 3:'c'}"><!-- 定义map,注意加# -->
					<s:property value="key" /> | <s:property value="value" />
					<br />
				</s:iterator>
			</li>
			<li>
				<s:iterator value="#{1:'a', 2:'b', 3:'c'}" var="x"><!-- 定义变量遍历 -->
					<s:property value="#x.key" /> | <s:property value="#x.value" />
					<br />
				</s:iterator>
			</li>
			<li>
				<s:fielderror fieldName="fielderror.test" theme="simple"></s:fielderror>
			</li><!-- s:fielderror把action中加入的对应fielderror名为fielderror.test取出 -->
			<li><s:subset>截取子集合</s:subset></li>
		</ol>
//struts.xml
<constant name="struts.i18n.encoding" value="GBK" /> <!-- internationalization -->
    <constant name="struts.ui.theme" value="simple" />
    <package name="tags" extends="struts-default" namespace="/">
        <action name="tags" class="com.bjsxt.struts2.tags.TagsAction">
            <result>/tags.jsp</result>
        </action>
    </package>
//action
package com.bjsxt.struts2.tags;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class TagsAction extends ActionSupport {

	private String password;

	private String username;

	public TagsAction() {
	}

	public String execute() {//添加对属性校验的错误信息, 
		this.addFieldError("fielderror.test", "wrong!");//表示属性名为fielderror.test的错误信息
		return SUCCESS;
	}
	
	public String getPassword() {
		return password;
	}

	public String getUsername() {
		return username;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
}




package com.bjsxt.struts2.tags;

public class User {
	private int age = 8;
	
	public User() {
		
	}
	
	public User(int age) {
		super();
		this.age = age;
	}


	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "user" + age;
	}
}




package com.bjsxt.struts2.tags;

public class Dog {
	
	private String name;
	
	public Dog() {
		
	}

	public Dog(String name) {
		super();
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "dog: " + name;
	}
}




package com.bjsxt.struts2.tags;

public class S {
	public static String STR = "STATIC STRING";
	
	public static String s() {
		return "static method";
	}
}

你可能感兴趣的:(html,bean,jsp,xml,struts)