当前位置: 移动技术网 > IT编程>开发语言>Java > jsp struts1 标签实例详解第1/2页

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

2018年05月29日  | 移动技术网IT编程  | 我要评论

尚辉陶瓷,开心成人社区,网络兼职

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>

1

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网