表單標籤
概述
使用表單標籤的優點
--表單回顯(頁面返回時,數據會保存在上面,密碼默認不會回顯)
--對頁面進行佈局和排版
標籤的屬性可以被賦值爲一個靜態的值或一個OGNL表達式
如果在賦值時使用了一個OGNL表達式並把它用%{}括起來,這個表達式將會被求值。
默認情況下,form標籤將被呈現爲一個表格形式的HTML表單,嵌套在form標籤裏的輸入字段將被呈現爲一個表格行,
<!--
表單標籤:
1.使用<span style="color:#ff0000;">和HTML的form標籤差不多</span>
2.struts2的<span style="color:#ff0000;">form標籤會生成一個table,已進行自動的排版</span>
3.可以對錶單提交的值進行<span style="color:#ff0000;">自動的回顯</span>:從棧頂對象開始匹配屬性,並把
匹配的屬性值賦給對應的標籤的value中,若棧頂對象沒有對應的屬性,
則依次向下找響應的屬性。
-->
<s:form action="save" >
...
</s:form>
textfield標籤將被呈現爲一個輸入文本字段,password標籤將被呈現爲一個口令字段,hidden標籤將被呈現爲一個不可見字段。
<s:hidden name="userId"></s:hidden>
<s:textfield name="userName" label="UserName"></s:textfield>
<s:password name="userPassword" label="Password"></s:password>
textarea標籤將被呈現爲一個HTML文本域元素
<s:textarea name="userDesc" label="Desc"></s:textarea>
checkbox標籤
checkbox標籤將呈現爲一個HTML複選框元素,該複選框元素通常用於提交一個布爾值
<s:checkbox name="married" label="Married"></s:checkbox>
假設married默認值爲trueHTML的checkbox標籤不提時,就無法改變married的值
checkbox標籤解決了這個侷限性,它採取的方法是爲某個複選框元素創建一個配對的不可見字段
當包含着一個複選框表單被提交時,如果某個複選框被選中了,它的值將爲true,這個複選框在HTTP請求量增加一個請求參數,
<input type="checkbox" name="married" value="true" checked="checked" id="save_married"/>
<input type="hidden" id="__checkbox_save_married" name="__checkbox_married" value="true" />
list,listKey,listValue這3個屬性對radio,select,checklist等標籤非常重要
radio標籤
radio標籤將呈現爲一組單選按鈕,單選按鈕的個數與程序員通過該標籤的list屬性提供的選項的個數相同
一般地,使用radio標籤實現“多選一”,對於“真/假”則該使用checkbox標籤
<s:radio name="gender" list="#{'1':'Male','0':'Female'}" label="Gender"></s:radio>
select標籤將呈現爲一個select元素
<s:select list="{11,12,13,14,15,16,17,18,19,20}"
headrKey=""
headValue="請選擇"
name="age"
label="Age" >
<!--
s:optgroup可以用作s:select的子標籤,用於顯示更多的下拉框
注意:必須指定鍵值對(前面是實際的值,後面是顯示的鍵),而不能使用一個集合,讓其值作爲鍵,又作爲值。
也可以使用listKey和listValue來寫
-->
<s:optgroup label="21-23" list="#{21:21,22:22,23:23}"></s:optgroup>
<s:optgroup label="31-33" list="#{31:31,32:32,33:33}"></s:optgroup>
</s:select>
optiongroup標籤
optiongroup標籤對select元素所提供的選項進行分組,每個選項有它自己的來源。
注意:必須要有鍵和值,且前要加‘#’
<s:optgroup label="21-23" list="#{21:21,22:22,23:23}"></s:optgroup>
checkboxlist標籤將呈現一組多選框
checkbox標籤被映射到一個字符串數組或是一個基本類型的數組。若它提供的多選框一個也沒有被選中,相應的屬性將被賦值爲一個空數組而不是空值。
<%
List<City> cities = new ArrayList<City>();
cities.add(new City(1001,"AA"));
cities.add(new City(1002,"BB"));
cities.add(new City(1003,"CC"));
cities.add(new City(1004,"DD"));
request.setAttribute("cities",cities);
%>
<!--
服務端需要使用集合類型,以保證能夠被正常的回顯
-->
<s:checkboxlist list="#request.cities"
listKey="cityId" listValue="cityName"
label="City" name="city"></s:checkboxlist>
下面給出完整實例
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="wul" namespace="/" extends="struts-default">
<action name="save" class="com.wul.struts2.ValueStack.UserAction" method="save">
<result name="input">/form-tag.jsp</result>
</action>
</package>
</struts>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page import="java.util.*"%>
<%@ page import="com.wul.struts2.ValueStack.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insert title here</title>
</head>
<body>
<s:debug></s:debug>
<%
List<City> cities = new ArrayList<City>();
cities.add(new City(1001,"AA"));
cities.add(new City(1002,"BB"));
cities.add(new City(1003,"CC"));
cities.add(new City(1004,"DD"));
request.setAttribute("cities",cities);
%>
<!--
表單標籤:
1.使用和HTML的form標籤差不多
2.struts2的form標籤會生成一個table,已進行自動的排版
3.可以對錶單提交的值進行自動的回顯:從棧頂對象開始匹配屬性,並把
匹配的屬性值賦給對應的標籤的value中,若棧頂對象沒有對應的屬性,
則依次向下找響應的屬性。
-->
<s:form action="save" >
<s:hidden name="userId"></s:hidden>
<s:textfield name="userName" label="UserName"></s:textfield>
<s:password name="userPassword" label="Password"></s:password>
<s:textarea name="userDesc" label="Desc"></s:textarea>
<s:checkbox name="married" label="Married"></s:checkbox>
<s:radio name="gender" list="#{'1':'Male','0':'Female'}" label="Gender"></s:radio>
<!--
服務端需要使用集合類型,以保證能夠被正常的回顯
-->
<s:checkboxlist list="#request.cities"
listKey="cityId" listValue="cityName"
label="City" name="city"></s:checkboxlist>
<s:select list="{11,12,13,14,15,16,17,18,19,20}"
headrKey=""
headValue="請選擇"
name="age"
label="Age" >
<!--
s:optgroup可以用作s:select的子標籤,用於顯示更多的下拉框
注意:必須指定鍵值對(前面是實際的值,後面是顯示的鍵),而不能使用一個集合,讓其值作爲鍵,又作爲值。
也可以使用listKey和listValue來寫
-->
<s:optgroup label="21-23" list="#{21:21,22:22,23:23}"></s:optgroup>
<s:optgroup label="31-33" list="#{31:31,32:32,33:33}"></s:optgroup>
</s:select>
<s:submit></s:submit>
</s:form>
<form action="save" method="post">
Married:<input type="checkbox" name="married" />
<input type="submit" value="submit">
</form>
</body>
</html>
City.java
package com.wul.struts2.ValueStack;
public class City {
private Integer cityId;
private String cityName;
public Integer getCityId() {
return cityId;
}
public void setCityId(Integer cityId) {
this.cityId = cityId;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public City() {
super();
}
public City(Integer cityId, String cityName) {
super();
this.cityId = cityId;
this.cityName = cityName;
}
}
UserAction.java
package com.wul.struts2.ValueStack;
import java.util.Arrays;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
public class UserAction {
private String userId;
private String userName;
private String userPassword;
private String userDesc;
private boolean married;
private String gender;
<span style="color:#ff0000;">
// 服務端需要使用集合類型,以保證能夠被正常的回顯</span>
private List<String> city;
private String age;
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public List<String> getCity() {
return city;
}
public void setCity(List<String> city) {
this.city = city;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserDesc() {
return userDesc;
}
public void setUserDesc(String userDesc) {
this.userDesc = userDesc;
}
public boolean getMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
}
public String save(){
System.out.println(this);
UserAction ua = new UserAction();
ua.setUserDesc("Oracle");
ua.setUserPassword("112");
ua.setUserId("1001");
ua.setUserName("wul");
// ActionContext.getContext().getValueStack().push(ua);
return "input";
}
@Override
public String toString() {
return "UserAction [userId=" + userId + ", userName=" + userName
+ ", userPassword=" + userPassword + ", userDesc=" + userDesc
+ ", married=" + married + ", gender=" + gender + ", city="
+ city + ", age=" + age + "]";
}
}