1,TagForm.java
详细出处参考:http://www.jb51.net/article/18639.htm
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>
3,next.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"%> <%@ page import="java.util.*"%> <font color=red>这是next页面</font> <html:form action="/tag"> <bean:define id="theForm" name="tagForm"/> <html:hidden property="id" /> userName:<html:text property="userName" /> password:<html:password property="password" /><br> 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> gender:male<html:radio property="sex" value="1" /> female<html:radio property="sex" value="2" /><br> <!-- multibox被勾选的前提是:当form bean的属性hobbies数组中的值与当前multibox的value属性的值相等时就会勾选。 在实际项目中,因为这里复选框的名字都是一样的,向action传值的时候,需要添加一些别的值来加以区别, 比如,value的值可以用分隔符来连接不同的值:id:checkboxValue 此处value的值为从bean标签中输出的name属性的值。 --> <logic:notEmpty name="theForm" property="hobbies"> <logic:iterate id="iter" name="theForm" property="hobbies" indexId="i"> box:<html:multibox property="hobbies" > <bean:write name='i'/> </html:multibox> </logic:iterate> </logic:notEmpty><br> <!-- optionsCollection标签不从pageContext中取出对象,只从与表单相关联的ActionForm中取出同名的数组。 其中,最终在页面提交后传递给action的值为value的值,而不是label的值。 --> <html:select property="from"> <html:optionsCollection property="provinces" label="label" value="value"/> </html:select> <!-- html:options --> <%java.util.ArrayList list = new java.util.ArrayList(); list.add(new org.apache.struts.util.LabelValueBean("Show value1","value1")); list.add(new org.apache.struts.util.LabelValueBean("Show value2","value2")); list.add(new org.apache.struts.util.LabelValueBean("Show value3","value3")); list.add(new org.apache.struts.util.LabelValueBean("Show value4","value4")); pageContext.setAttribute("valuelist",list);%> <html:select property="from"> <html:options collection="valuelist" property="value" labelProperty="label"/> </html:select><p> <!-- html:textarea --> <html:textarea rows="5" cols="30" property="introduction"></html:textarea><br> <input type="submit" value="register" ><br> <!-- logic:present --> <!-- 判断所指定的对象是否存在。 property属性:和name属性同时使用,当name属性所指定的变量是一个JavaBean时,判断property属性所指定的对象属性是否存在。 --> <%pageContext.setAttribute("ExistingString","teststring");%> <logic:present name="ExistingString"> ExistingString的值为<bean:write name="ExistingString"/> </logic:present> <logic:notPresent name="ExistingString"> ExistingString的值为<bean:write name="ExistingString"/> </logic:notPresent><p> <!-- logic:empty --> <%pageContext.setAttribute("test1","");%> <logic:empty name="test1"> test1变量为空! </logic:empty><p> <!-- logic:equal --> <!-- 当相比较的两个字符串都可以转化为数字时,则按数字的大小来比较,当无法转成数字时,才按字符串进行比较。 --> <%pageContext.setAttribute("test1",new Integer(10000));%> <logic:equal name="test1" value="10000"> 变量test1等于 ${test1 } </logic:equal> <p> <!-- html:link --> <!-- name属性: 它的值是一个 java.util.HashMap类型的对象名称,它的每一个"键/值"对就代表一对的"参数名/参数值" --> <html:link href="http://www.sina.com.cn" href="http://www.sina.com.cn"> 新浪网 </html:link> <%String test1 = "testABC"; request.setAttribute("stringtest",test1);%> <html:link page="/tag.do" paramId="testString" paramName="stringtest"> 测试页面1 </html:link> <%HashMap para_map = new HashMap(); para_map.put("testString","testABC"); para_map.put("testInt",new Integer(10000)); request.setAttribute("map1",para_map);%> <html:link page="/test.do" name="map1">测试页面2</html:link> <p> <!-- logic:match --> <!-- 判断变量中是否包含指定的常量字符串。 location属性: 所能取的值只有两个,一个是"start",另一个是"end"。 start表示以value所指定的字符串开头,end表示结尾 --> <%pageContext.setAttribute("test","Hello,World");%> <logic:match name="test" value="Hello"> <bean:write name="test"/> </logic:match> <logic:match name="test" value="Hello" location="end"> <bean:write name="test"/> </logic:match><p> <!-- logic:iterate --><!-- --> <!-- 对数组的遍历 --> <%String [] testArray1 = {"str0","str1","str2","str3","str4","str5","str6"}; pageContext.setAttribute("test1",testArray1);%> <logic:iterate id="array1" name="test1"> <bean:write name="array1"/> </logic:iterate><br> <!--length属性指定了输出元素的个数,offset属性指定了从第几个元素开始输出,如此处为2则表示从第三个元素开始输出 indexId属性,它指定一个变量存放当前集合中正被访问的元素的序号 --> <logic:iterate id="array1" name="test1" length="3" offset="2" indexId="i"> 第<bean:write name="i"/>个字符为${array1 }; </logic:iterate><br> <!-- 对map的遍历 --> <%java.util.HashMap countries = new java.util.HashMap(); countries.put("country1","中国"); countries.put("country2","美国"); countries.put("country3","英国"); countries.put("country4","法国"); countries.put("country5","德国"); pageContext.setAttribute("countries",countries);%> <logic:iterate id="country" name="countries"> <bean:write name="country" property="key"/>: <bean:write name="country" property="value"/> </logic:iterate> <br> <logic:iterate id="country" name="countries"> ${country.key }:${country.value } </logic:iterate><br> <!-- 对list的遍历 --> <%java.util.ArrayList list1 = new java.util.ArrayList(); list1.add("str1"); list1.add("str2"); list1.add("str3"); list1.add("str4"); list1.add("str5"); pageContext.setAttribute("testlist",list1); %> <logic:iterate id="showlist" name="testlist" indexId="index"> <bean:write name="index"/>: <bean:write name="showlist"/> </logic:iterate><br> <!-- 嵌套遍历 这个ArrayList对象内的每一个元素又是一个String型的数组 --> <%String [] colors = {"red","green","blue"}; String [] countries1 = {"中国","美国","法国"}; String [] persons = {"乔丹","布什","克林顿"}; java.util.ArrayList list2 = new java.util.ArrayList(); list2.add(colors); list2.add(countries1); list2.add(persons); pageContext.setAttribute("list2",list2);%> <logic:iterate id="list2Id" name="list2" indexId="j"> <bean:write name="j"/>: <logic:iterate id="subId" name="list2Id" indexId="k"> <bean:write name="subId"/> </logic:iterate> </logic:iterate><p> <!-- logic:messagesPresent --> <!-- 标记是来判断是否在request内存在特定的 ActionMessages或ActionErrors对象。 name属性:指定了ActionMessages在request对象内存储时的key值。 message属性:message属性有两种取值。 当其为true时,表示使用Globals.MESSAGE_KEY做为从request对象中获取ActionMessages的key 值,此时无论name指定什么都无效; 当其为false时,则表示需要根据name属性所指定的值做为从request对象中获取ActionMessages的key 值, 倘若此时未设置name属性的值,则使用默认的Globals.ERROR_KEY。 property属性:指定ActionMessages对象中某条特定消息的key值。 --> <%org.apache.struts.action.ActionMessages messages = new org.apache.struts.action.ActionMessages(); messages.add("message1",new org.apache.struts.action.ActionMessage("html.errors.error1")); request.setAttribute(org.apache.struts.Globals.MESSAGE_KEY,messages); %> <logic:messagesPresent message="true" property="message1"> 所查找的ActionMessage存在。 </logic:messagesPresent> </html:form>
4,TagAction.java
package com.tarena.struts.tag.action; import org.apache.struts.action.Action; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import com.tarena.struts.tag.form.TagForm; import javax.servlet.http.*; public class TagAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { TagForm f = (TagForm)form; String[] hbs = f.getHobbies(); if(hbs != null){ for(int i = 0 ; i<hbs.length ; i++) System.out.println("hobbies" + i +"=="+ hbs[i]); } hbs = new String[4]; hbs[0]= "1"; hbs[1]= "2"; f.setHobbies(hbs); //int i = f.getHobbies().length; //System.out.println(i); return mapping.findForward("next"); } }
5,struts-config.xml
<form-bean name="tagForm" type="com.tarena.struts.tag.form.TagForm" /> <action path="/tag" type="com.tarena.struts.tag.action.TagAction" name="tagForm"> <forward name="next" path="/tag/next.jsp" /> </action>