您的位置:首页 > 编程语言 > Java开发

Struts标签的简单总结

2007-08-21 13:41 423 查看
Struts HTML标签

基本的HTML元素标签
(1)<html:html>
用法:<html:html lang="true">,lang属性可选
(2)<html:base>
用法:<html:base/>,用于生成当前网页的绝对URL路径,当该网页引用同一应用的其他网页时,只需给出相对于当前网页的相对URL路径
(3)<html:link>
用法:
a.创建全局转发链接
<html:link forward="index">Link to Global ActionForward</html:link>
b.创建具有完整URL的链接
<html:link href="http://www.ustb.edu.cn">ustb</html:link>
c.创建相对当前应用URL的链接
<html:link page="/HtmlBasic.do">A relative link</html:link>
在URL或URI中包含请求参数
a.把请求参数加在URL或URI的末尾
b.请求参数存在于当前网页可访问的一个变量中
<html:link page="/HtmlBasic.do" paramId="urlParamName" paramName="variable"></html:link>
参数名:urlParamName 参数值:变量variable的值
<html:link page="/HtmlBasic.do" paramId="urlParamName" paramName="Bean" paraProperty="propertyName"></html:link>
参数名:urlParamName 参数值:Bean.propertyName
<html:link page="/HtmlBasic.do" name="HashMap"></html:link>
参数名:HashMap.Key 参数值:HashMap.Value
(4)<html:rewrite>
用法:用于输出超链接中的URI部分(完整URL中协议、主机和端口以后的内容),但不生成HTML<a>元素
<html:rewrite page="/HtmlBasic.do?prop1=abc&prop2=123"/>
(5)<html:img>
用法:
a.<html:img page="/img.gif"/>page属性指定相对当前应用的URI
b.<html:img src="/htmltaglibs/img.gif"/>src属性指定完整的URI

基本的表单标签
(1)<html:form>
<html:form action="FormBasic.do"></html:form>
(2)<html:text>
<html:text property="ActionFormBean.propertyName" [disabled="true"]/>
(3)<html:hidden>
<html:hidden property="ActionFormBean.propertyName" [write="true"]/>
可选属性write="true",将隐藏字段值输出到网页
(4)<html:checkbox>
<html:checkbox property="FormBean.propertyName" [value="true|false"]/>
a.FormBean.propertyName类型必须为boolean
b.可选属性value,设置选中检查框时的值,默认为true
(5)<html:multibox>
1{<html:multibox property="FormBean.PropertyArray" value="value"/>}n
1{<html:multibox property="FormBean.PropertyArray">value</html:multibox>}n
a.property的类型可以不是boolean类型
b.仅选中的复选框的值被存放到对应Bean的相应数组中
(6)<html:radio>
1{<html:radio property="FormBean.propertyName" value="value"/>}n
如果一个按钮都没选中,则FormBean.propertyName="";
(7)<html:select>
<html:select property="FormBean.propertyName" [multiple="true"] size="5">
1{<html:option>,<html:options>,<html:optionsCollections>}n
</html:select>
a.size属性:指定网页上显示的可选项数目
b.multiple属性:指定是否支持多选。默认为false
c.property属性:当multiple=ture时,为数组类型
(7.1)<html:option>
<html:option value="htmlselect.purple">Purple</html:option>
<html:option value="htmlselect.orange" bundle="htmlselect.Colors" key="htmlselect.red"/>
选项的实际值:htmlselect.purple和htmlselect.orange
选项的标签值:Purple和资源文件[HtmlSelect.Colors].properties中htmlselect.red对应键值。其中[]中部分与<message-resources>元素中parameter值对应
(7.2)<html:options>
<html:options collection="colorCollection" property="value" labelProperty="label"/>
colorCollection是存放选项(LabelValueBean对象)的集合
(7.3)<html:optionsCollection>
<html:optionsCollection name="formBeanName" property="formBean.propertyName" label="property1" value="property2"/>
a.如果没有name属性,则name为与表单关联的FormBean
b.formBean.propertyName为一个对象数组
c.label=formBean.propertyName.property1的值
d.value=formBean.propertyName.property2的值
(8)<html:submit>
<html:submit>Submit</html:submit>
(9)<html:cancel>
<html:cancel>Cancel</html:cancel>
(10)<html:reset>
<html:reset>Reset</html:reset>

文件上传标签
<html:form action="UploadFile.do" method="POST" enctype="multipart/form-data">
...
<html:file property="file"/>
...
<html:submit/>
</html:form>
a.<html:form>的mothod属性必须设为"POST"
b.<html:form>的enctype属性必须设为"multipart/form-data"
c.<html:file>必须设置property属性,该属性与ActionForm Bean中FormFile类型的属性对应

<html:errors>标签
<html:errors>标签可以放在网页的任何地方,它有以下重要属性:
a.name:指定ActionMessages对象存放在request或session内的key,默认为Globals.ERROR_KEY
b.property:指定消息属性。如果此项没有设置,将显示ActionMessages对象中所有的ActionMessage
c.bundle:指定ResourceBundle,与<message-resources>元素的key值相对应。如果此项没有设置,将从应用默认的Resource Bundle中获取消息文本
ActionMessages的add(String property,ActionMessage message)方法的propery参数用于设置消息属性。

<html:message>标签
重要属性:
a.name:指定ActionMessages对象存放在request或session范围内的属性key
b.message:指定消息来源。如果为true,则从request或session范围内检索出属性key为Globals.MESSAGE_KEY的ActionMessages对象,此时name属性无效;如果为false,则根据name属性来检索ActionMessages对象,如果此时没有设置name属性,则采用默认值Globals.MESSAGE_KEY
c.id:用来命名从消息集合中检索出的每个ActionMessage对象,它和<bean:write>标签的name属性匹配。

Struts Bean标签库
(1)<bean:header>
用于检索HTTP请求中的Header信息,它具有以下属性:
a.id:定义一个java.lang.String类型变量,存放在page范围
b.name:指定需要检索的Header信息
如<bean:header id="lang" name="Accept-Language"/>
(2)<bean:parameter>
用于检索HTTP请求参数,它有以下属性:
a.id:定义一个java.lang.String类型变量,存放在page范围
b.name:请求参数名
c.value:指定参数的默认值
如果希望检索出所有和参数名匹配的请求参数,应设属性multiple="yes",此时id是字符串数组,存放所有和参数名匹配的请求参数值
(3)<bean:cookie>
用于检索HTTP请求参数,它有以下属性:
a.id:定义一个javax.servlet.http.Cookie类型变量,存放在page范围
b.name:指定Cookie的名字
c.value:指定Cookie的默认值
如果希望检索出所有和Cookie名字匹配的Cookie,应设属性multiple="yes",此时id是Cookie数组,存放所有和参数名匹配的Cookie
(4)<bean:page>
用于检索JSP隐含对象,如session,request和response等。它具有以下属性:
a.id:定义一个引用隐含对象,存放在page范围内
b.property:指定隐含对象的名字,可选值包括application,config,request,response和session
(5)<bean:message>
用于输出Resource Bundle中的一条消息。<bean:message>标签的bundle属性指定ResourceBundle
在<bean:message>标签中指定消息key有三种方式:
a.<bean:message>标签的key属性直接指定消息key,如<bean:message bundle="special" key="hello"/>
b.通过name属性和property属性指定,key=name.property,如
<%SomeBean bean=new SomeBean();bean.setName("hello");request.setAttribute("someBean",bean);%>
<bean:message bundle="special" name="someBean" property="name"/>
c.通过name属性指定一个可以转化为字符串的变量,这个变量的字符串值为消息key,如
<%request.setAttribute("stringBean","hello");%>
<bean:message bundle="special" name="stringBean"/>
对带参的复合消息(如hello=Hello,{0}),可以使用属性arg0,arg1,arg2,arg3和arg4属性来设置参数值,arg0代表第一个参数,依次类推。如:<bean:message key="hello" arg0="Linda"/>
(6)<bean:resource>
用于检索Web资源的内容,它具有以下属性:
a.id:定义代表Web资源的变量
b.name:指定Web资源的路径
c.input:如果没设,则id属性为字符串类型;如果给input属性设置了值(任意字符串),则id属性为java.io.InputStream类型
(7)<bean:struts>
用于检索Struts框架内在的对象,如ActionFormBean,ActionForward或ActionMapping。<bean:struts>标签的id属性定义一个page范围的变量,用来引用Struts框架的内在对象。必须设置formbean,forward或mapping属性中的一个属性,来指定被引用的Struts内在对象。
(8)<bean:include>
用来包含其他Web资源的内容,<bean:include id="variable" page=""|forward=""|href=""/>
(9)<bean:define>
用于定义一个变量。id属性指定变量名,toScope属性指定变量的存放范围,默认值为page。给id属性定义的变量赋值有三种方式:
a.设置value属性,此时id属性为字符串类型,value属性代表这个变量的字符串值
b.同时设置name和property属性。name指定一个已经存在的Bean,property属性指定已经存在的Bean的某个属性,id属性的类型由Bean.property的类型决定。
c.同时设置name属性和type属性。name指定一个已经存在的Bean,type属性指定这个JavaBean的完整类名。id属性引用这个已经存在的JavaBean。
(10)<bean:size>
用于获得Map、Collection或数组的长度。<bean:size>的id属性定义一个Integer类型的变量,name属性指定已经存在的Map、Collection或数组变量。
(11)<bean:write>
用于在网页上输出某个对象或它的属性的内容,它有以下属性:
a.name:指定要输出的变量。如果只有该属性,则调用name指定变量的toString方法获得字符串输出
b.property:可选属性,指定要输出的对象属性
c.format:用于设置输出数据的格式,如format="#.####"或"MM-dd-yyyy hh:mm:ss"
d:filter:默认为true。如果filter属性为true,将把输出内容中的特殊HTML符号作为普通字符串来显示

Struts Logic标签库

进行比较运算的Logic标签:
(1)<logic:equal>
(2)<logic:notEqual>
(3)<logic:greaterEqual>
(4)<logic:greaterThan>
(5)<logic:lessEqual>
(6)<logic:lessThan>
进行字符串匹配的Logic标签
(1)<logic:match>
(2)<logic:notMatch>
字符串匹配标签的location属性指定子字符串的位置,可选值包括
a.start:子字符串位于母字符串的初始位置
b.end:子字符串位于母字符串的结尾
判断指定内容是否存在的Logic标签
(1)<logic:empty>:判断指定变量是否为null或""
(2)<logic:notEmpty>
(3)<logic:present>
(4)<logic:notPresent>
(5)<logic:messagePresent>
(6)<logic:messageNotPresent>
进行循环遍历的Logic标签
<logic:iterate>在一个循环中遍历数组、Collection、Enumeration、Iterator或Map中的所有元素。如
<logic:iterate id="element" indexId="index" name="Animals" offset="1" length="2">
<bean:write name="index"/>.<bean:write name="element"/>
</logic:iterate>
length属性指定需要遍历的元素的数目,如果没有设置,就遍历集合中所有元素。offset属性指定开始遍历的起始位置,默认值为”0“,表示从集合的第一个元素开始遍历。indexId属性定义一个代表当前被遍历元素序号的变量,这个变量存放在page范围内。
设置被遍历变量的方法:
a.设置name属性,name属性指定需要遍历的集合或Map
b.设置name和property属性,name属性指定一个JavaBean,property属性指定JavaBean的一个属性,这个属性为需要遍历的集合或Map
c.设置collection属性,collection属性指定一个运行时表达式,表达式的运算结果为需要遍历的集合或Map,如<logic:iterate id="header" collection="<%=request.getHeaderNames()%>">
请求转发或重定向的Logic标签
(1)<logic:forward>
<logic:forward name="global-forward-name"/>
(2)<logic:redirect>
<logic:redirect forward|href|page=""/>与<html:link>相似
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: