jsp struts1 tag example details

  • 2020-05-10 18:37:25
  • OfStack

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"%> 

<!--  Need to be in struts-config.xml In the action the path for "/tag" the name property 1 a form bean --> 
<html:form action="/tag"> 
<html:hidden property="id" /> 
userName:<html:text property="userName" /> 
password:<html:password property="password" /><br> 
<!-- 
 in checkbox The back to add 1 A and checkbox Property of the same name and value As a" false "Of the hidden input box, forced Struts To reset checkbox The attribute value  
1 Under normal circumstances, in ActionForm Boolean variables are used in 1 a checkbox That's because it's either not selected or it's selected.  

multibox Tags generate checkboxes on web pages that actually function and checkbox1 The sample.  
 The difference is where this check box is located ActionForm The use of 1 An array to represent the check box.  
 So, 1 In general, it is recommended 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> 

<!--  from form From the radio If you have setter Once it's assigned, it's on the page value Anything with the same value will be checked  --> 
gender:male<html:radio property="sex" value="1" /> 
female<html:radio property="sex" value="2" /><br> 

<!-- provinces in form bean Are defined and assigned as follows : 
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 for 1 , will only be displayed simultaneously 1 An option.  
 There are 1 a multiple Property, when it is true , the selection list allows multiple selections. The user can drag the mouse, or hold it down Ctrl Key for multiple selection.  
 when multiple Properties for true When the ActionForm The corresponding property should be 1 Array types to assign multiple values selected by the user at the same time.  
--> 
<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> This is a next page </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 The premise to be checked is: when form bean The properties of the hobbies The value in the array is current multibox the value Properties are checked when they have the same value.  
 In a real project, because the checkbox names here are all 1 The sample to action When passing a value, you need to add it 1 Some other values to distinguish between,  
 For instance, value Can be used to connect different values with delimiters: id:checkboxValue 
 here value A value of from bean Output from the label name The value of the property.  
--> 
<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 The label is not from pageContext Extract the object from, only those associated with the form ActionForm Gets an array of the same name.  
 Among them, the final page submitted after the transfer action The value of value Instead of label The value of the.  
--> 
<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 --> 
<!--  Determines whether the specified object exists.  
property Properties: name Properties are used together, when name The variable specified by the property is 1 a JavaBean The judge property Property whether the object property specified by the.  
--> 
<%pageContext.setAttribute("ExistingString","teststring");%> 
<logic:present name="ExistingString"> 
ExistingString The value of <bean:write name="ExistingString"/> 
</logic:present> 
<logic:notPresent name="ExistingString"> 
ExistingString The value of <bean:write name="ExistingString"/> 
</logic:notPresent><p> 

<!-- logic:empty --> 
<%pageContext.setAttribute("test1","");%> 
<logic:empty name="test1"> 
test1 The variable is empty!  
</logic:empty><p> 

<!-- logic:equal --> 
<!--  When both strings can be converted to Numbers, they are compared by the size of the Numbers, and when they cannot be converted to Numbers, they are compared by the strings.  --> 
<%pageContext.setAttribute("test1",new Integer(10000));%> 
<logic:equal name="test1" value="10000"> 
 variable test1 Is equal to the  ${test1 } 
</logic:equal> <p> 

<!-- html:link --> 
<!-- name attribute :  Its value is 1 a  java.util.HashMap The object name of the type, each of which 1 a " key / value " The means 1 For the " Parameter names / The parameter value " --> 
<html:link href="http://www.sina.com.cn" href="http://www.sina.com.cn"> 
 sina  
</html:link> 
<%String test1 = "testABC"; 
request.setAttribute("stringtest",test1);%> 
<html:link page="/tag.do" paramId="testString" paramName="stringtest"> 
 The test page 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"> The test page 2</html:link> <p> 

<!-- logic:match --> 
<!--  Determines whether the variable contains the specified constant string.  
location attribute :  There are only two values you can take, 1 One is "start" And the other 1 One is "end" .  
start Said to value The beginning of the specified string, end Said the 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 --><!-- --> 
<!--  Traversal of an array  --> 
<%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 Property specifies the number of output elements, offset Property specifies which element to start the output from, which is 2 Is from the first 3 The output of the element begins  
indexId Property, which specifies 1 Three variables hold the sequence number of the element being accessed in the current collection  
--> 
<logic:iterate id="array1" name="test1" length="3" offset="2" indexId="i"> 
 The first <bean:write name="i"/> Characters for ${array1 } ;  
</logic:iterate><br> 

<!--  right map The traversal  --> 
<%java.util.HashMap countries = new java.util.HashMap(); 
countries.put("country1"," China "); 
countries.put("country2"," The United States "); 
countries.put("country3"," The British "); 
countries.put("country4"," The French "); 
countries.put("country5"," Germany "); 
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> 

<!--  right list The traversal  --> 
<%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> 

<!--  Nested traversal   this ArrayList Each within the object 1 Student: the elements again 1 a String Type of the array  --> 
<%String [] colors = {"red","green","blue"}; 
String [] countries1 = {" China "," The United States "," The French "}; 
String [] persons = {" Jordan "," bush "," Clinton "}; 
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 --> 
<!--  The mark is to determine whether it is in request Memory exists specifically  ActionMessages or ActionErrors Object.  
name Property: specified ActionMessages in request Stored inside an object key Value.  
message Properties: message There are two values for a property.  
 When it is true , is used Globals.MESSAGE_KEY As from request Get in object ActionMessages the key  Value, whatever it is name Nothing specified is valid;  
 When it is false , is the basis of need name The value specified by the property is used as a slave request Get in object ActionMessages the key  Value,  
 If not set at this time name For the value of the property, use the default Globals.ERROR_KEY .  
property Property: specify ActionMessages Of a particular message in an object key Value.  

--> 
<%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"> 
 To find the ActionMessage There is.  
</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>

Related articles: