jsp struts1 标签实例详解第1/2页

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>

jsp技术jsp struts1 标签实例详解第1/2页,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。