1,TagForm.java
複製代碼 代碼如下:package com.tarena.struts.tag.form;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.*;
public class TagForm extends ActionForm
{
private int id;
private String userName;
private String password;
private int sex;
private String[] hobbies;
private String from;
private String introduction;
private Collection provinces;
private boolean checkbox1;
private boolean checkbox2;
public TagForm()
{
sex = 1;
provinces = new ArrayList();
provinces.add(new OptionBean("beijing", "100000"));
provinces.add(new OptionBean("hebei", "110000"));
provinces.add(new OptionBean("tianjin", "120000"));
}
public String getFrom()
{
return from;
}
public void setFrom(String from)
{
this.from = from;
}
public String[] getHobbies()
{
return hobbies;
}
public void setHobbies(String[] hobbies)
{
this.hobbies = hobbies;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getIntroduction()
{
return introduction;
}
public void setIntroduction(String introduction)
{
this.introduction = introduction;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public int getSex()
{
return sex;
}
public void setSex(int sex)
{
this.sex = sex;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public Collection getProvinces()
{
return provinces;
}
public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request)
{
//checkbox1 = false;
//hobbies = null;
}
public boolean isCheckbox1() {
return checkbox1;
}
public void setCheckbox1(boolean checkbox1) {
this.checkbox1 = checkbox1;
}
public boolean isCheckbox2() {
return checkbox2;
}
public void setCheckbox2(boolean checkbox2) {
this.checkbox2 = checkbox2;
}
public void setProvinces(Collection provinces) {
this.provinces = provinces;
}
}
2,input_struts.jsp 複製代碼 代碼如下:<%@ page language="java" contentType="text/html;charset=utf-8"%>
<%@ taglib uri="/WEB-INF/resource/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/resource/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/resource/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/resource/struts-tiles.tld" prefix="tiles"%>
<%@ taglib uri="/WEB-INF/resource/struts-nested.tld" prefix="nested"%>
<%@ taglib uri="/WEB-INF/resource/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/resource/app.tld" prefix="app"%>
<%@ taglib uri="/WEB-INF/resource/fmt.tld" prefix="fmt"%>
<!-- 需要在struts-config.xml中action的path為"/tag"的name屬性中指定一個form bean -->
<html:form action="/tag">
<html:hidden property="id" />
userName:<html:text property="userName" />
password:<html:password property="password" /><br>
<!--
在checkbox後面添加一個和checkbox的屬性同名並且value為“false”的隱藏輸入框,強迫Struts去重新設定checkbox的屬性值
一般情況下,在ActionForm中都使用布爾型變數來表示一個checkbox,這是因為它要麼沒被選中,要麼就是被選中。
multibox標籤產生網頁上的複選框,其實它的功能和checkbox一樣。
所不同的是,在此複選框所在的ActionForm中使用一個數組來表示該複選框。
所以,一般情況下,推薦使用multibox。
private boolean checkbox1;
private String[] hobbies;
-->
checkbox1:<html:checkbox property="checkbox1" />
<input type="hidden" name="checkbox1" value="false">
checkbox2:<html:checkbox property="checkbox2" />
<input type="hidden" name="checkbox2" value="false"><br>
hobbies:swim<html:multibox property="hobbies" value="1" />
reading<html:multibox property="hobbies" value="2" />
walking<html:multibox property="hobbies" value="3" /><br>
<!-- 從form傳來的radio若已經setter賦值了,則在頁面上value值與之相等的就會被勾選 -->
gender:male<html:radio property="sex" value="1" />
female<html:radio property="sex" value="2" /><br>
<!-- provinces在form bean中定義及賦值如下:
private Collection provinces;
provinces = new ArrayList();
provinces.add(new OptionBean("beijing", "100000"));
provinces.add(new OptionBean("hebei", "110000"));
provinces.add(new OptionBean("tianjin", "120000"));
-->
<html:select property="from">
<html:optionsCollection property="provinces"/>
</html:select>
<!--
size為1,則只同時顯示一個選項。
還有一個multiple屬性,當其為true時,該挑選清單就允許多選。使用者可以通過滑鼠的拖動,或是按住Ctrl鍵進行多選。
當multiple屬性為true時,在ActionForm中對應的屬性應是一個數群組類型以便同時向其賦上使用者選中的多個值。
-->
<html:select property="from" size="3" multiple="true">
<html:option value="value1">Show Value1</html:option>
<html:option value="value2">Show Value2</html:option>
<html:option value="value3">Show Value3</html:option>
</html:select><br>
<html:textarea rows="5" cols="30" property="introduction"></html:textarea><br>
<input type="submit" value="register" >
<br><br>
</html:form>