---
title: "struts2 UI标签"
author: "lydia_fly"
date: "2014-02-17T22:36:29Z"
tags: []
categories: ["企业架构"]
slug: "struts2-ui"
draft: false
lastmod: "2014-02-17T22:36:29Z"
id: 2018382
url: "https://lydia-fly.iteye.com/blog/2018382"
---

Struts2的UI标签

 1) 用途： 

 --生成框体（文本框、选择框）

 --自动回显数据（文本框显示值，选择框做默认勾选）

 --常用于修改功能中默认数据的显示

 2) 涉及的标签

 a,表单标签 b,文本框标签 c,密码框标签 d,单个值的复选框标签 e,提交按钮标签

 f,单选框标签 g,复选框标签 h,下拉选择标签

 

案例如下：

    **ToUpdateAction.java** 

```
public class ToUpdateAction {
	//input，由于是模拟的代码，不给输入了
	//output
	private Customer customer;
	private List<City> cities;
	
	public ToUpdateCustomerAction() {
		System.out.println("实例化Action...");
	}
	public String execute() {
		System.out.println("Action业务方法...");
		CustomerDAO dao = new CustomerDAO();
		customer = dao.findById();
		cities = dao.findAllCities();
		return "success";
	}
      // get/set方法省略
```

 

**Customer.java**

```
public class Customer {
	private String name; // 姓名
	private String password; // 密码
	private String desc; // 简介
	private String sex; // 性别
	private boolean marry; // 是否已婚
	private List<String> travelCities; // 去过的城市
	private String home; // 家乡
	
	private String loveCity; // 最喜欢的城市
       // .....
}
```

 **City.java**

```
public class City {
	private String code;
	private String name;
   //....
}
```

 **update.jsp**

```
<%@page pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head></head>
<body>
	<h1>演示Struts2的UI标签示例</h1>
	<!-- 1.生成表单标签 -->
	<s:form action="#" method="post">
		<!-- 
			2.文本框标签:
				a.生成文本框
				b.通过name中的OGNL表达式访问来源Action，
					并根据返回结果给该文本框赋值
		-->
		<s:textfield name="customer.name" label="姓名"/>
		
		<!-- 
			3.密码框标签	
				可以理解为特殊的文本框，用法同2
		 -->
		 <s:password name="customer.password" label="密码" showPassword="true"/>
		
		<!-- 
			4. 单个值的复选框标签
				a.生成一个checkbox
				b.根据name中的OGNL表达式，向来源
					Action取值，并根据取值的结果设置
					是否勾选此checkbox
				c.OGNL表达式访问的属性，应该是布尔类型
		-->
		<s:checkbox name="customer.marry" label="是否已婚" labelposition="left"/>
		
		<!-- 
			6.单选框标签
				a.通过list生成一组radio及对应的label，
					list可以指定一个Map，那么Map的key
					用于生成radio的value属性值，Map的
					value用于生成label显示值
				b.通过name的OGNL表达式，访问来源Action，
					将返回的值与radio的key匹配，哪个
					radio的key与返回值一致，则该radio
					会设置为选中的。
		 -->
		 <s:radio name="customer.sex" list="#{'M':'男','F':'女'}" label="性别"/>
		
		<!-- 
			7.复选框标签
				a.用法等同于6
				b.由于可以多选，那么name中OGNL表达式
					访问的属性应该是集合
		 -->
		<s:checkboxlist name="customer.travelCities" list="#{'beijing':'北京','shanghai':'上海','guangzhou':'广州','dongguan':'东莞','shenzhen':'深圳','diaoyudao':'钓鱼岛'}" label="去过的城市"/>
		<!-- 
			7.1复选框标签-动态初始化范围
				a.通过list的OGNL表达式，访问Action中的
					集合属性，返回的集合用于生成多个选项。
					listKey要指定集合对象中用于生成value
					值的属性名。
					listValue要指定集合对象中用于生成label
					显示值的属性名。
				b.通过name来默认勾选，用法同7
		 -->
		<s:checkboxlist name="customer.travelCities" list="cities" listKey="code" listValue="name" label="去过的城市2"/>
		
		<!-- 
			8.下拉选标签
				a.通过list生成选项option，其中Map的key
					用于生成option的value，Map的value
					用于生成option的显示值
				b.通过name中的OGNL表达式，访问来源
					Action，并将取到的是结果与option匹配，
					哪个option的value值与返回值一致，则将
					此option选中
		 -->
		 <s:select name="customer.home" list="#{'beijing':'北京','shanghai':'上海','guangzhou':'广州','dongguan':'东莞','shenzhen':'深圳','diaoyudao':'钓鱼岛'}" label="家乡" headerKey="-1" headerValue="请选择"/>
		 <!-- 
		 	8.1下拉选标签-动态初始化选项
		 		同7.1
		  -->
		 <s:select name="customer.home" list="cities" listKey="code" listValue="name" label="家乡2" headerKey="-1" headerValue="请选择"/>
		
		<!-- 5.提交按钮标签 -->
		<s:submit value="保存"/>
	</s:form>
</body>
</html>
```

 **结果如下：**

**![](http://dl2.iteye.com/upload/attachment/0093/9348/6fe1fce5-3213-379f-a493-49e108013cf9.jpg)**