Struts2_表單標籤

表單標籤

概述

表單標籤將在HTML文檔裏被呈現爲一個表單元素

使用表單標籤的優點
--表單回顯(頁面返回時,數據會保存在上面,密碼默認不會回顯)
--對頁面進行佈局和排版

標籤的屬性可以被賦值爲一個靜態的值或一個OGNL表達式
如果在賦值時使用了一個OGNL表達式並把它用%{}括起來,這個表達式將會被求值。

如何實現回顯:Strust2將值壓到棧頂,再取回
利用此原理可以做修改時原信息的回顯

form標籤
form標籤用來呈現HTML語言中的表單元素
默認情況下,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標籤
textfield標籤將被呈現爲一個輸入文本字段,password標籤將被呈現爲一個口令字段,hidden標籤將被呈現爲一個不可見字段。
password標籤拓展自textfield標籤,多了一個showPassword屬性,該屬性是布爾類型,默認值爲false,它決定着表單回顯時是否輸入密碼。
		<s:hidden name="userId"></s:hidden>
		<s:textfield name="userName" label="UserName"></s:textfield>
		<s:password name="userPassword" label="Password"></s:password>

textarea標籤
textarea標籤將被呈現爲一個HTML文本域元素
		<s:textarea name="userDesc" label="Desc"></s:textarea>

checkbox標籤
checkbox標籤將呈現爲一個HTML複選框元素,該複選框元素通常用於提交一個布爾值
		<s:checkbox name="married" label="Married"></s:checkbox>
假設married默認值爲true
HTML的checkbox標籤不提時,就無法改變married的值
checkbox標籤解決了這個侷限性,它採取的方法是爲某個複選框元素創建一個配對的不可見字段
當包含着一個複選框表單被提交時,如果某個複選框被選中了,它的值將爲true,這個複選框在HTTP請求量增加一個請求參數,
但如果該複選框未被選中,在請求中就不會增加一個請求參數。
strus2轉成網頁後會自動生成這兩句:
<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屬性
list,listKey,listValue這3個屬性對radio,select,checklist等標籤非常重要
可以把一個String,一個數組,一個Enumeration,Iterator,Map或Colollection賦給list屬性



radio標籤
radio標籤將呈現爲一組單選按鈕,單選按鈕的個數與程序員通過該標籤的list屬性提供的選項的個數相同
一般地,使用radio標籤實現“多選一”,對於“真/假”則該使用checkbox標籤
		<s:radio name="gender" list="#{'1':'Male','0':'Female'}" label="Gender"></s:radio>

select標籤
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標籤
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>

下面給出完整實例



struts.xml
<?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>

form-tag.jsp
<%@ 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 + "]";
	}
	
	
	
	
	
	
	
	
}





發佈了1 篇原創文章 · 獲贊 7 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章