当前位置: 首页 > 图文教程 > 网络编程 > JSP > jsp struts1 标签实例详解

JSP
GET 方式提交的含有特殊字符的参数
java big5到gb2312的编码转换
java Lucene 中自定义排序的实现
hibernate中的增删改查实现代码
jsp 定制标签(Custom Tag)
jsp基础速成精华讲解
IE cache缓存 所带来的问题收藏
关于JSP的一点疑问小结
JSP 多条SQL语句同时执行的方法
jsp include文件时的一个乱码解决方法
在JSTL EL中处理java.util.Map,及嵌套List的情况
jsp 页面显示的一些用法
根据Hibernte的cfg文件生成sql文件
五种 JSP页面跳转方法详解
JSP 防范SQL注入攻击分析
JSP 连接MySQL配置与使用
java eclipse 启动参数
jsp 页面上图片分行输出小技巧
解决jsp开发中不支持EL问题
JSP 页面中使用FCKeditor控件(js用法)

JSP 中的 jsp struts1 标签实例详解


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 262 ::
收藏到网摘: n/a

struts1 标签实例,应用。 1,TagForm.java
复制代码 代码如下:
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>