欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jsp struts1 標(biāo)簽實例詳解

 更新時間:2009年06月18日 21:02:28   作者:  
struts1 標(biāo)簽實例,應(yīng)用。

3,next.jsp

復(fù)制代碼 代碼如下:

<%@ 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被勾選的前提是:當(dāng)form bean的屬性hobbies數(shù)組中的值與當(dāng)前multibox的value屬性的值相等時就會勾選。
在實際項目中,因為這里復(fù)選框的名字都是一樣的,向action傳值的時候,需要添加一些別的值來加以區(qū)別,
比如,value的值可以用分隔符來連接不同的值:id:checkboxValue
此處value的值為從bean標(biāo)簽中輸出的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標(biāo)簽不從pageContext中取出對象,只從與表單相關(guān)聯(lián)的ActionForm中取出同名的數(shù)組。
其中,最終在頁面提交后傳遞給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屬性同時使用,當(dāng)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 -->
<!-- 當(dāng)相比較的兩個字符串都可以轉(zhuǎn)化為數(shù)字時,則按數(shù)字的大小來比較,當(dāng)無法轉(zhuǎn)成數(shù)字時,才按字符串進(jìn)行比較。 -->
<%pageContext.setAttribute("test1",new Integer(10000));%>
<logic:equal name="test1" value="10000">
變量test1等于 ${test1 }
</logic:equal> <p>

<!-- html:link -->
<!-- name屬性: 它的值是一個 java.util.HashMap類型的對象名稱,它的每一個"鍵/值"對就代表一對的"參數(shù)名/參數(shù)值" -->
<html:link >
新浪網(wǎng)
</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表示結(jié)尾
-->
<%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 --><!-- -->
<!-- 對數(shù)組的遍歷 -->
<%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屬性指定了輸出元素的個數(shù),offset屬性指定了從第幾個元素開始輸出,如此處為2則表示從第三個元素開始輸出
indexId屬性,它指定一個變量存放當(dāng)前集合中正被訪問的元素的序號
-->
<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對象內(nèi)的每一個元素又是一個String型的數(shù)組 -->
<%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 -->
<!-- 標(biāo)記是來判斷是否在request內(nèi)存在特定的 ActionMessages或ActionErrors對象。
name屬性:指定了ActionMessages在request對象內(nèi)存儲時的key值。
message屬性:message屬性有兩種取值。
當(dāng)其為true時,表示使用Globals.MESSAGE_KEY做為從request對象中獲取ActionMessages的key 值,此時無論name指定什么都無效;
當(dāng)其為false時,則表示需要根據(jù)name屬性所指定的值做為從request對象中獲取ActionMessages的key 值,
倘若此時未設(shè)置name屬性的值,則使用默認(rèn)的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
復(fù)制代碼 代碼如下:

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
復(fù)制代碼 代碼如下:

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

相關(guān)文章

最新評論