---
title: "struts tags小结"
author: "netxdiy"
date: "2011-03-20T10:47:37Z"
tags: ["Bean", "HTML", "JSP", "Struts", "XML"]
categories: ["企业架构"]
slug: "struts-tags"
draft: false
lastmod: "2011-03-21T16:15:17Z"
id: 968112
url: "https://netxdiy.iteye.com/blog/968112"
---

struts.xml /tags.jsp TagsAction.java package com.bjsxt.struts2.tags; import com.opensymphony.xwork2.ActionSupport; public class TagsAction extends ActionSupport { private String password; private String username; public TagsAction() { } public String execute() { this.addFieldError("fielderror.test", "wrong!"); 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; } } User.java 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; } } Dog.java 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; } } S.java package com.bjsxt.struts2.tags; public class S { public static String STR = "STATIC STRING"; public static String s() { return "static method"; } } index.jsp &lt;%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%&gt; &lt;% String contextPath = request.getContextPath(); %&gt; Insert title here 访问属性 [tags](%3C%=contextPath%20%%3E/tags.action?username=u&password=p) tags.jsp &lt;%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%&gt; &lt;%@ taglib uri="/struts-tags" prefix="s" %&gt; Struts-Tags学习

01. property:
02. property 取值为字符串:
03. property 设定默认值:
04. property 设定HTML:
    
    * * *
05. set 设定adminName值（默认为request 和 ActionContext）:
06. set 从request取值:
07. set 从ActionContext取值: &lt;%--
08. set 设定范围:
09. set 从相应范围取值: &lt;%=pageContext.getAttribute("adminPassword") %&gt; --%&gt;
10. set 设定var，范围为ActionContext:
11. set 使用#取值:
12. set 从相应范围取值:
    
    * * *
    
    &lt;%--
13. push:
14. push:
15. push: --%&gt;
    
    * * *
16. bean 定义bean,并使用param来设定新的属性值:
17. bean 查看debug情况: 拿出值：
    
    * * *
18. include \_include1.html 包含静态英文文件
19. include \_include2.html 包含静态中文文件
20. include \_include1.html 包含静态英文文件，说明%用法，%{}将里面的内容强制转换成0ngl表达式
    
    * * *
21. if elseif else: age =  
    wrong age! too young! yeah!  
    null
    
    * * *
22. 遍历集合：  
    |
23. 自定义变量：  
    |
24. 使用status:  
    | 遍历过的元素总数： | 遍历过的元素索引： | 当前是偶数？： | 当前是奇数？： | 是第一个元素吗？： | 是最后一个元素吗？：
25. |
26. |

\_include.jsp &lt;%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%&gt; &lt;%@ taglib uri="/struts-tags" prefix="s" %&gt; \_include params :  
使用property取参数：p1=  
使用property取参数：p2=  
使用property取参数：p3=  
\_include1.html \_include1.html This is my \_include1 page.  
\_include2.html \_include1.html 中文