Struts常用标签的使用

1.        Struts HTML标签库

Struts HTML标签大致分为以下几类:

◆用于生成基本的HTML元素的标签

◆用于生成HTML表单的标签

◆显示错误或正常消息的标签

 

1.1     用于生成基本的HTML元素的标签

<html:html>: 生成HTML<html>元素

<html:base>: 生成HTML<base>元素

<html:link>: 生成HTML Anchor<a>元素

<html:rewrite>: 生成用户请求的URI

<html:img>: 生成HTML<img>元素

 

1.        <html:html>标签

<html:html>标签用于在网页的开头生成HTML<html>元素。<html:html>标签有一个lang属性,用于显示用户使用的语言

<html:html lang=”true”>

如果客户浏览器使用中文语言,那么以上代码在运行时将被解析为普通的HTML代码:

<html lang=”zh-CN”>

lang属性为”true”时,<html:html>标签将先根据存储在当前HttpSession中的Locale对象来输出网页使用的语言,如果不存在HttpSession,或者HttpSession中没有Locale对象,就根据客户浏览器提交的HTTP请求头中的Accept-Language属性来输出语言,如果HTTP请求头中没有Accept-Language属性,就根据默认的Locale来输出语言。

2.        <html:base>标签

<html:base>标签在网页的<head>部分生成HTML<base>元素。HTML<base>元素用于生成当前网页的绝对URL路径。

<head>

<title>struts tag lib</title>

<html:base />     -----------> <base href=”http://localhost:8080/strutsTagLibs/xxx.jsp” >

</head>

如果在网页中使用了<html:base>标签,当该网页引用同一个应用的其他网页时,只需给出相对于当前网页 的相对URL路径即可。

3.        <html:link><html:rewrite>标签

<html:link>标签用于生成HTML<a>元素。<html:link>在创建超链接时,有两个优点:

◆允许在URL中伊多种方式包含请求参数

◆当用户浏览器关闭Cookie时,会自动重写URL,把SessionID作为请求参数包含在URL中,用于跟踪用户的Session状态

<html:link>标签有以下重要属性:

forward: 指定全局转发链接

href: 指定完整的URL链接

page: 指定相对于当前网页的URL

<html:rewrite>用于输出超链接的URI部分,但它并不生成HTML<a>元素。URI指的是URL中协议、主机和端口以后的内容。URI用于指定具体的请求资源。

示例1:创建全局转发链接

Struts配置文件的<global-forwards>元素中定义一个<forward>元素

<global-forward>

<forward name=”index” path=”/index.jsp” />

</global-forward>

JSP文件中创建<html:link>标签

<html:link formward=”index”>index</html:link>

<html:link>标签的forward属性和<global-forwards>元素中的<forward>子元素匹配,生成如下HTML内容:

<a href=”/strutsTagLibs/index.jsp” >index</a>

注意:<html:link>forward属性只能引用Struts配置文件中<global-forwards>内的<forward>子元素,如果引用<action>内的<forward>子元素,在运行时会抛出异常

示例2:创建具有完整URL的链接

<html:link href=”http://www.google.cn”>Google</html:link>

生成的HTML内容:

<a  href=”http://www.google.cn”>Google</a>

注意:如果指定了<html:link>标签的href属性,即使用户浏览器的Cookie关闭,<html:link>标签也不会吧用户SessionID作为请求参数加入到URL

示例3:从当前网页中创建相对URL

<html:link>标签的page属性用于指定相对于当前应用的URI

<html:link page=”/index.do”>index</html:link>

生成的HTML内容:

<a  href=”/strutsTagLibs/index.do”>index</a>

示例4:在URLURI中包含请求参数

<html:link page=”/login.do?username=lidonghai&amp;password=123”>login</a>

或:<html:rewrite page=”/login.do?uername=lidonghai&amp;password=123” />

提示:在HTML编码中,”&amp;”代表特殊字符”&”

示例5:在URLURI中包含单个请求变量

<%

String str=”hello”

pageContext.setAttribute(“str”,str);

%>

<jsp:userBean id=”customer” scope=”page” class=”com.isoftstone.Customer” />

<jsp:setProperty name=”customer” property=”name” value=”lidonghai” />

 

<html:link page=”/user.do” paramID=”say” paramName=”str”>use</html:link>

<html:link page=”/user.do” pramID=”username” paramName=”customer” paramProperty=”name”>user</html:link>

 

<html:rewrite page=”/user.do” paramID=”say” paramName=”str” />

<html: rewrite page=”/user.do” pramID=”username” paramName=”customer” paramProperty=”name” />

<html:link>标签的paramId属性指定请求参数名,paramName属性指定变量的名字。如果变量为JavaBean,paramProperty属性指定JavaBean的属性。

<a href=”/strutsTabLibs/user.do?say=hello”>user</a>

<a href=”/ strutsTabLibs/user.do?username=lidonghai”>user</a>

示例6:在URLURI中包含多个请求变量

如果在URLURI中包含多个请求参数,而这些参数的值来自于多个变量,需要先定义一个Map类型的Java类,如java.util.HashMap,用它来存放请求变量。

<%

java.util.HashMap map = new java.util.HashMap();

map.put(“str”, new String(“hello”));

map.put(“strArray”, new String[]{“I”,”love”,”you”});

pageContext.setAttribute(“map”,map);

%>

<html:link page=”/user.do” name=”map”>user</html:link>

rewrite: <html:rewrite page=”/user.do” name=”map” />

<html:link>标签的name属性指定包含请求变量的HashMap对象。HashMap对象中的每一对“key/value”代表一对或多对“请求参数名/请求参数值”。

<a href=”/strutsTagLibs/user.do?str=hello&amp;strArray=I&amp;strArray=love&amp;strArray=you”>user</a>

4.        <html:img>标签

<html:img>标签用于在HTML页中嵌入图片。此外,它还允许包含请求变量,以便动态控制图片的输出

生成基本的HTML<img>元素

<html:img page=”/upload/move.gif  />

对应的HTML内容:<img src=”/strutsTagLibs/upload/move.gif>

生成包含单个请求变量的HTML<img>元素

<html:img src=”/strutsTagLibs/upload/move.gif” paramId=”headPhoto” paramName=”str” />

对应的HTML内容:<img src=”/strutsTagLibs/upload/move.gif?headPhoto=hello”>

生成包含多个请求变量的HTML<img>元素

<html:img page=”/upload/move.gif” name=”map”/>

对应的HTML内容:

<img src=”/strutsTagLibs/upload/move.gif?str=hello&strArray=I&strArray=love&strArray=you”>

1.2 基本的表单标签

Struts HTML标签库提供了一组生成HTML表单的标签:

<html:form>: 生成HTML<form>元素

<html:text>: 生成HTML<input type=text>元素

<html:hidden>: 生成HTML<input type=hidden>元素

<html:submit>.: 生成HTML<input type=submit>元素

<html:cancel>: 在表单上生成取消按钮

<html:reset>: 生成HTML<input type=reset>元素

 

1.        <html:form>标签

<html:form>标签的action属性用来指定当用户提交表单后,处理用户请求的组件。Struts框架将参照Struts配置文件来查找相应的Action组件

<html:form action=”user.do”>

对应的HTML内容:

<form name=”userForm” method=”post” action=”/strutsTagLibs/user.do”>

2.        <html:text>标签

<html:text property=”username” />

<html:text>标签的property属性指定字段的名字,它和ActionForm Bean中的一个属性匹配,当用户提交表单时,Struts框架会把username字段的内容赋值给UserForm Beanusername属性。

3.        <html:cancel>标签

当用户按下取消按钮时,将产生一个取消事件,这个事件有Action类来捕获,可以在Action类的execute()方法来处理。

<html:cancel>Cancel</html:cancel>

生成的HTML内容:

<input  type=”submit” name=”org.apache.struts.taglib.html.CANCEL” value=”Cancel”>

Action类中,以编程的方式处理取消事件

public class UserAction extends Action {

public ActionForward execute(ActionMappign mapping, ActionForm form,

                              HttpServletRequest request, HttpServletResponse response)

                                  throws Exception {

    UserForm user = (UserForm) form;

    if(isCancelled(request)) {

        user.setStatus(“Cancel was pressed!”);

        return (mapping.findForward(“success”));

    }else {

        user.setStatus(“Submit was pressed!”);

        return (mapping.findForward(“success”));

    }

}

}

Action类的isCancelled(request)方法用来判断取消事件有没有发生。如果这个方法返回true,就表示取消事件发生了,可以在程序中进行相关的操作

4.        <html:reset>标签

<html:reset>标签生成表单的复位按钮

<html:reset>Reset</html:reset>

生成的HTML内容:

<input  type=”reset” value=”Reset” />

5.        <html:submit>标签

<html:submit>标签生成表单的提交按钮

<html:submit>Submit</html:submit>

生成的HTML内容:

<input  type=”submit” name=”submit” value=”Submit”>

6.        <html:hidden>标签

隐藏字段用于在表单上存放不希望让用户看到或不允许修改的信息

<html:hidden property=”createDate” />

对应的HTML内容:

<input type=”hidden” name=”createValue” value=”proValue”>

如果要把隐藏字段的值显示在网页上,则可以采用如下方式:

<html:hidden property=”createDate” write=”true”/>

对应的HTML内容:

<input type=”hidden” name=”createValue” value=”proValue”>propValue

当初次访问该页面时,以上两个隐藏字段的value值都为空,只有当用户在属性为“createDate”的文本框中输入“propValue”并提交表单时,这两个隐藏字段的value值才会变为”propValue”.

1.3 检查框和单选按钮标签

1.        <html:checkbox>

<html:checkbox>标签在表单上生成标准的HTML检查框,如果ActionForm里的某个属性只有两种可选值(truefalse),就可以在表单中使用该标签。

<html:checkbox property=”status” />

生成的HTML内容:

<input type=”checkbox” name=”status” value=”true” />

在相应的ActionForm中,status属性必须定义成boolean类型

<html:checkbox>有一个value属性,用来设置用户选中检查框时的值value的默认值为true.可以采用以下方式显示指定value属性:

<html:checkbox property=”status” value=”true” />

如果把value的属性设为false,如果用户选中这个检查框,则对应的ActionForm里的status属性为false.

为使检查框能正常工作,必须在ActionForm Beanreset()方法中对其复位。当<html:checkbox>value属性为true(false)时,必须在reset()方法中把对应的属性设置为false(true).

this.setStatus(false);

2.        <html:multibox>标签

ActionForm Bean中定义一个数值,来存放所有CheckBox的值

private String[] love = new String[0];

public String[] getLove() { return this.love; }

public void setLove(String love[]) {this.love = love; }

在表单中加入<html:multibox>元素,并对每个元素设置初始值

<html:multibox property=”love” value=”apple” />

<html:multibox property=”love” > banala</html:multibox>

3.        <html:radio>标签

在同一时刻,只允许用户选择一个按钮,

<html:radio property=”sex” value=”boy” />

<html:redio property=”sex” value=”girl” />

如果两个按钮都没被选中,相应的ActionForm中的sex属性被设为空字符串””.

1.4 下拉列表和多选列表标签

1.        <html:select>标签可以在表单上创建下拉列表或多选列表。

<html:select property=”color” multiple=”true” size=”6”>

<html:option>…

<html:options>…

<html:optionCollections>…

</html:select>

size: 指定每次在网页上显示的可选项的数目

multiple: 指定是否支持多项选择,默认值为false

property: 在单项选择的情况下,ActionForm中的属性应定义为简单类型;在多项选择下,ActionForm中的属性应定义为数值。

2<html:option>标签

<html:option>标签被嵌套在<html:select>标签中,代表列表的一个可选项

<html:select property=”colors” size=”6” multiple=”true” >

<html:option value=”htmlselect.orange”>Orange</html:option>

<html:option value=”htmlselect.purple”>Purple</html:option>

 

<html:option value=”htmlselect.red” bundle=”htmlselect.Colors” key=”htmlselect.red”/>

<html:option value=”htmlselect.blue” bundle=”htmlselect.Colors” key=”htmlselect.blue”/>

</html:select>

RedBlue选项的显示值来源于ResourceBundle. <html:option>bundle属性指定Resource Bundle,它和Struts配置文件中<message-resources>元素的ket属性匹配。

<message-resources parameter=”HtmlSelectColors” key=”htmlselect.Colors” />

以上<message-resources>元素配置的Resource Bundle的资源文件为HtmlSelectColors.properties.内容如下:

htmlselect.red=Red

htmlselect.blue=Blue

<html:option>元素的key属性和以上资源文件中的消息key匹配

3.  <html:options>标签

<html:option>标签提供了一组HTML<option>元素,在<html:select>元素中可以包含多个<html:options>元素

<html:select property=”colors” size=”6” multiple=”true” >

<html:option collection=”colorCollection” property=”value” labelProperty=”label” />

</html:select>

collection属性指定存放可选项的集合,这个集合应该存放在page范围内。

<%

Vector colorCollection = new Vector();

colorCollection.add(new org.apache.struts.util.LabelValueBean(“Pink”,”htmlselect.pink”));

colorCollection.add(new org.apache.struts.util.LabelValueBean(“Brown”,”htmlselect.brown”));

pageContext.setAttribute(“colorCollection”,colorCollection);

%>

每个LableValueBean实例代表一个可选项,它有两个属性:labelvalue, 分别代表可选项的显示值和实际值。<html:options>元素的property属性指定集合中可选项的实际值,labelProperty属性指定集合中可选项的显示值。

4.        <html:optionsCollection>标签

<html:optionsCollection>标签提供了一组HTML<option>元素。在<html:select>元素中可以包含多个<html:optionsCollection>元素。

<html:select property=”cusId”>

<html:optionsCollection property=”customers” label=”name” value=”cusId” />

</html:select>

name属性指定包含可选集合的JavaBean的名字,若未指定,将使用与表单关联的ActionForm Bean.

property属性指定可选项集合

以下为对应的ActionForm Bean中定义customers属性的代码:

private Customer customers[];

public Customer[] getCustomers() { return this.customers; }

public void setCustomers(Customer[] customers) { this.customers = customers; }

customers集合中包含一组Customer。每个Customer实例代表一个可选项,它有两个属性:namecusId,分别代表可选项的显示值和实际值

1.5 在表单中上传文件标签

1. <html:file>标签可以方便地实现文件上传的功能

<html:form action=”user.do” method=”post” enctype=”nultipart/form-data”>

<html:file property=”file” />

<html:submit/>

</html:form>

注意:<html:file>必须嵌套在<html:form>标签中,且method属性必须为“post”,编码类型enctype属性必须设为“multipart/form-data”。且必须设置property属性,这个属性和ActionForm中的FormFile类型的属性对应。

private FormFile file;

public FormFile getFile() { return this.file; }

public void setFile(FormFile file) { this.file = file; }

2.        Action类中处理文件上传

String path = servlet.getServletContext().getRealPath(“/upload”);

UserForm userForm = (UserForm) form;

 

FormFile file = userForm.getFile();

if(file == null) {

return mapping.findForward(“success”);

}

 

String fileName = file.getFileName();

String fileSize = Integer.toString(file.getFileSize()) + “bytes”;

 

InputStream in = file.getInputStream();

OutputStream out = new FileOutputStream(dir + “/” + filename);

 

int bytesRead=0;

byte[] buffer = new byte[8192];

while((bytesRead=in.read(buffer,0,8192))!=-1) {

out.write(buffer.0.bytesRead);

}

 

in.close();

out.close();

1.6  <html:errors>标签

1. 错误消息的来源

<html:errors>标签在requestsession范围内寻找ActionMessages(或其子类ActionErrors)集合对象,再从ActionMessages集合对象中读取ActionMessage对象,把ActionMessage对象包含的消息文本显示到网页上

ActionForm中的Validate()方法中生成ActionMessages对象:

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

ActionErrors errors = new ActionErrors();

if(this.getCheckbox1()) {

    errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(“error.global.fromform”));

    errors.add(“checkbox1”, new ActionMessage(“error.checkbox”));

}

return errors;

}

Action中的execute()方法中生成ActionMessages对象

public ActionForward execute(ActionMapping mapping, ActionForm form,

                              HttpServletRequest request,HttpServletResponse response)

                               throws Exception {

ActionMessages errors = new ActionMessages();

String username = (String)((UserForm)form).getUserName();

if(username.equalsIgnoreCase(“lidongai”)) {

    errors.add(“username”,new ActionMessage(“hello, don’t talk to lidonghai”,”lidonghai”));

    saveErrors(request,errors);

    return(new ActionFormward(mapping.getInput()));

}

….

}

2. 格式化地显示错误消息

<html:errors>标签能够格式化地显示ActionMessage对象包含的消息文本,而这些消息文本预先存放在Resource Bundle中。

error.global.fromform=<li>This is a global error</li>

error.checkbox=<li>this is an error that is realted to checkBox1</li>

errors.footer=<hr>

errors.header=<h3><font color=”red”>Validation Error</font></h3>

<html:errors>标签能够识别错误消息文本中的HTML元素

3. <html:errors>标签的用法

<html:errors>标签可以放在网页的任何地方,即可以位于HTML表单内,也可以在表单外。

name:指定ActionMessages对象存放在requestsession范围内的属性key.

property: 指定消息属性。如果没有此项设置,将显示ActionMessages对象中所有的ActionMessage

bundle: 指定Resource Bundle。如果此项没有设置,将从应用默认的Resource Bundle中获取消息文本

1.7  <html:messages>标签

name:指定ActionMessages对象存放在requestsession范围内的属性key.

property: 指定消息来源。如果为true,则从requestsession范围内的检索出属性keyGlobals.MESSAGE_KEYActionMessages对象,此时name属性无效;如果为false,则根据name属性来检索ActionMessages对象,如果此时没有设置name属性,将采用默认值Globals.ERROR_KEYmessage属性默认值为false.

id: 用来命名从消息集合中检索出的每个ActionMessage对象,它和<bean:write>标签的name属性匹配

<html:messages id=”messagemessage=”true”>

<td><bean:write name=”message” /></td>

</html:messages>

 

2.        Struts Bean标签库

Struts Bean标签库中的标签可以访问已经存在的JavaBean以及它们的属性,还可以定义新的Bean,把它放在page范围内或者用户指定的范围内,供网页内其他元素访

Bean标签库大致分为以下三类:

   用于访问HTTP请求信息或JSP隐含对象的Bean标签

   用于访问Web应用资源的Bean标签

   用于定义或输出JavaBeanBean标签

2.1 访问HTTP请求信息或JSP隐含对象

1. <bean:header>标签用于检索HTTP请求中的Header信息

id: 定义一个java.lang.String类型的变量,这个变量存放在page范围内

name: 指定需要检索的Header信息

<bean:header id=”lang” name=”Accept-Language” />

<bean:write name=”lang” /> //输出lang变量的值

2. <bean:parameter>标签用于检索HTTP请求参数

id: 定义一个java.lang.String类型的变量,这个变量存放在page范围内

name: 指定请求参数名

value: 指定请求参数的默认值

<bean:parameter id=”arg1” name=”testarg” value=”noarg”/>

<bean:write name=”arg1”/>

以上代码定义了一个名为”arg1”的字符串类型的变量,默认值为“noarg”。如果访问的该页面的URL不包含”testarg”请求参数,那么arg1变量的值为”noarg”;如果访问该页面的URL包含”testarg”请求参数,那么arg1变量的值为第一个名为”testarg”请求参数的值。

如果希望检索出所有和参数名匹配的请求参数,应设置标签里的multiple属性(可以设置为任意字符串),此时的id属性定义的变量时一个字符串数组

<bean:parameter id=”arg2” multiple=”yes” name=”testarg” value=”noarg” />

3.<bean:cookie>标签可以检索保持在浏览器中的Cookie

id: 定义一个javax.servlet.http.Cookie类型的变量,这个变量被存放在page范围内

name: 指定Cookie的名字

value: 指定Cookie的默认值。如果有name属性指定的Cookie不存在,就使用指定的默认值

<bean:cookie id=”cookie” name=”username” value=”lidonghai” />

如果设置multiple属性(可以设为任意字符串),可以检索出所有和Cookie名字匹配的Cookie

4. <bean:page>标签用于检索JSP隐含对象

id: 定义一个隐含对象的变量,这个变量存放在page范围内。

property: 指定隐含对象的名字,可选值包括applicationconfigrequestresponsesession.

<bean:page id=”this_session” property=”session” />

<bean:write name=”this_session” property=”creationTime” />

2.2 访问Web应用资源

1. <bean:message>标签用于输出Resource Bundle中的一条消息

bundle: 指定Resource Bundle, 它和Struts配置文件的<message-resources>元素的key属性匹配。如果没有设置该属性,就采用默认的Resource Bundle

<message-resources parameter=”ApplicationResources” />

message-resouces parameter=”SpecialResources” key=”special” />

2. <bean:resource>标签

<bean:resource>标签用于检索Web资源的内容,它具有以下属性:

id: 定义一个代表Web资源的变量

name: 如果没有设置input属性,则id属性定义的变量为字符串类型,如果给input属性设置了值(可为任意字符串),则id属性定义的变量为java.io.InputStream类型

<bean:resource id=”resource” name=”/testpage1/jsp”/>

<bean:write name=”resource” />

3. <bean:struts>标签

<bean:struts>标签用于检索Struts框架内在对象。

id: 定义一个page范围的变量,用来引用Struts框架的内在对象。必须设置为formbeanforwardmapping

formean属性:指定ActionFormBean对象,和Struts配置文件的<form-bean>元素匹配

forward属性:指定ActionForward对象,和Struts配置文件的<global-forwards>元素的<forward>子元素匹配

mapping属性:指定ActionMapping对象,和Struts配置文件的<action>元素匹配。

<bean:struts id=”forward” forward=”BeanResources”/>

<bean:write name=”forward” property=”path”/>

Struts配置文件中与之匹配的<forward>元素:

<global-forwards>

<forward name=”BeanReources” path=”/BeanReources.jsp”/>

</global-forwards>

4. <bean:include>标签

<bean:include>标签把其他Web资源的内容存放在一个变量中,而不是直接显示到网页上。

<bean:include>标签的id属性定义一个代表其他Web资源的变量。

可以通过<bean:include>标签的forwardpagehref属性来指定其他Web资源

forward: 指定全局转发路径,和Struts配置文件的<global-forwards>元素中的<forward>子元素匹配。

page: 指定相对于当前应用的URI,以”/”开头

href: 指定完整的URL

<bean:include id=”register” page=”/register.jsp”/>

<bean:write name=”register” filter=”false”/>

2.3 定义或输出JavaBean

1. <bean:define>标签

<bean:define>标签用于定义一个变量。id属性指定变量的名字,toScope属性指定这个变量存放范围,如果没有设置toScope属性,则这个变量存放在page范围内.

设置value属性,此时id属性定义的变量为字符串类型,value属性代表这个变量的字符串值

<bean:define id=”usrname” value=”lidonghai”/> //定义一个字符串变量usrename,它的值为lidonghai

同时设置nameproperty属性。name属性指定一个已经存在的Beanproperty属性指定已经存在的Bean的某个属性。id属性定义的变量的值由property属性决定

<% request.setAttribute(“sessionBean”,session); %>

<bean:define id=”contextBean” name=”sessionBean” property=”servletContext” />

<bean:write name=”contextBean” property=”servletContextName”/>

同时设置name属性和type属性。name属性指定已经存在的JavaBean, type属性指定这个JavaBean的完整的类名,id属性定义的变量引用这个已经存在的JavaBean.

<bean:define id=”student1” name=”student” type=”com.isoftstone.vo.Student” />

对于有name属性指定的已经存在的JavaBean, 在默认情况下,<bean:define>标签会依次在pagerequestsessionapplication范围内寻找这个JavaBean. 另外也可以通过scope属性指定要寻找的范围。

2. <bean:size>标签

<beran:size>标签用于获得MapCollection或数组的长度。

<bean:size>标签的id属性定义一个Integer类型的变量,name属性指定已经存在的MapCollection或数组变量。id属性定义的变量的变量值为MapCollection或数组的长度

<bean:size id=”length” name=”map”/> //mapMap类型的一个集合

3. <bean:write>标签

<bean:write>标签用于在网页上输出某个Bean或它的属性内容。name属性指定已经存在的变量

<bean:write name=”length” />

<bean:write>标签还有一个filter属性,默认值为true. 如果filter属性为true, 将把输出内容的特殊HTML符号作为普通字符串来显示;如果filter属性为false, 则不会把输出内容中的特殊HTML符号转化为普通字符串。

3.        Struts Logic标签库

3.1 进行比较运算的Logic标签

1. <logic:equal>: 比较变量是否等于指定的常量。

2. <logic:notEqual>: 比较变量是否不等于指定的常量

3. <logic:greaterEqual>: 比较变量是否大于或等于指定的常量

4.<logic:greaterThan>: 比较变量是否大于指定的常量

5.<logic:lessEqual>: 比较变量是否小于或等于指定的常量

6.<logic:lessThan>: 比较变量是否小于指定的常量

所有的运算标签都比较一个变量和指定常量的大小,其中value的属性指定常量的值,可以通过以下方式来设置变量:

<logic:equal cookie="username" value="tuozixuan">

UserName in Cookie is tuozixuan

</logic:equal>

设置header属性,此时变量为header属性的HTTP请求中的Header信息

<logic:equal header="Accept-Language" value="zh-cn">

Language is: zh-cn

</logic:equal>

设置parameter属性,此时变量为parameter属性指定的请求参数值

<logic:greaterThan parameter="arg1" value="100">

Language is: zh-cn

</logic: greaterThan>

设置name属性指定被比较的变量,比较运算符调用变量的toString()方法

<logic:equal name="age" value="80">

Language is: zh-cn

</logic:equal>

在默认情况下,将依次在pagerequestsession、和application范围内寻找name属性指定的变量。此外还可通过scope属性来指定变量的存在范围

同时设置nameproperty属性,此时name属性指定已经存在的JavaBeanProperty属性指定JavaBean的属性,被比较的变量为这个属性的值

<logic:notEqual name="student" property="name" value="tuozixuan">

    His name is not "tuozixuan"

</logic:notEqual>

如果两个字符串都可以成功地转化为数字,就比较数字的大小,否则就进行字符串比较

3.2 进行字符串匹配的Logic标签

<logic:match>: 判断变量中是否包含指定的常量字符串

<logic:notMatch>: 判断变量中是否不包含指定的常量字符串

字符串匹配标签的value属性指定常量值,可以通过cookie/header/parameter/nameproperty属性来设置变量,用法与比较运算标签类似

字符串匹配标签的location属性指定子字符串的位置,可选择包括start/end; 如果没有指定location属性,子字符串可以位于母字符串的任何位置。

start: 子字符串位于母字符串的起始位置

end: 子字符串位于母字符串的结尾

<logic:notMatch name="authorName" scope="request" value="tuozixuan" location="start">

    <bean:write name="authorName" />doesn’t start with the string ‘tuozixuan’.

</logic:notMatch>

 

3.3 判断指定内容是否存在的Logic标签

<logic:empty>: 判断指定的变量是否为null,或者为空字符串""

<logic:notEmpty>: 判断指定的变量不为null,并且不是空字符串""

可以设置<logic:empty><logic:notEmpty>标签的name属性,或者同时设置name属性和property属性来指定变量

<logic:present>: 判断指定的安全角色、用户、CookieHTTP请求HeaderJavaBean是否存在

<logic:notPresent>: 判断指定的安全角色、用户、CookieHTTP请求HeaderJavaBean是否不存在

<logic:present><logic:notPresent>标签判断指定的对象是否存在

cookie属性:判断指定的cookie是否存在

header属性:判断指定的HTTP请求Header是否存在

role属性:判断当前通过权限验证的用户是否具有指定的安全角色。多个安全角色之间以逗号隔开

<logic:present role="role1,role2,role3">code…</logic:present>

user属性:判断当前通过权限验证的用户是否拥有指定的用户名

parameter属性:判断指定的请求参数是否存在

name属性:判断指定的JavaBean是否存在

同时设置nameproperty属性:name属性指定JavaBeanproperty属性指定JavaBean的某个属性,判断这个属性是否存在并且是否为null.

<logic:messagesPresent>: 判断指定的消息是否存在

<logic:messagesNotPresent>: 判断指定的消息是否不存在

3.4 进行循环遍历的Logic标签

<logic:iterate>能够在一个循环中遍历数组CollectionEnumerationIteratorMap中所有的元素

1.遍历集合

name属性:指定需要遍历的集合对象,它每次从集合中检核出一个元素,然后把它存放在page范围内

id属性: 指定遍历集合对象所检核出的元素

length属性: 指定需要遍历的元素的数目,如果未指定,则遍历集合中的所有元素

offset:指定开始遍历的起始位置,默认值为"0",表示从集合的第一元素开始遍历

indexed: 定义一个代表当前被遍历元素序号的变量,这个变量被存放在page范围内,可以被标签主体的<bean:write>标签访问

<logic:iterate id="student" indexed="index" name="student" offset="1" length="2">

<bean:write name="index"/> <bean:write name="student" property="name" /><br/>

</logic:iterate>

2.        遍历Map

<logic:iterate>标签遍历Map中的每一个元素,每一个元素都包含一对key/value。可通过<bean:write>标签输出

<logic:iterate id="element" indexed="ind" name="months">

<bean:write name="ind" />

<bean:write name="element" property="key" />

<bean:write name="element" property="value" />

</logic:iterate>

如果HashMap中的每个元素的value是集合对象,则可以采用嵌套的<logic:iterate>遍历集合中所有对象

<logic:iterate id="element" indexed="ind" name="catalog">

<bean:write name="ind" />

<bean:write name="element" property="key" />

<logic:iterate id="elementValue" name="element" property="key"/><br/>

------<bean:write name="elementValue"/>

</logic:iterate>

</logic:iterate>

3.设置被遍历的变量

设置name属性,name属性指定需要遍历的集合或Map

设置name属性和property属性指定一个JavaBeanproperty属性指定JavaBean的一个属性,这个属性为需要遍历的集合或Map

设置collection属性,collection属性指定一个运行时表达式,表达式的运算结果为需要遍历的集合或Map

<logic:iterate id="header" collection="<%=request.getHeaderNames()%>" >

<bean:write name="header" /><br/>

</logic:iterate>

 

3.5 进行请求转发或重定向的Logic标签

1. <logic:forward>标签用于请求转发,它的name属性指定转发目标,与Struts配置文件中的<global-forwards>元素的<forward>子元素匹配

//struts-config

<global-forwards>

<forward name="index" path="/index.jsp"/>

</global-forwards>

 

<logic:forward name="index" />

2. <logic:redirect>标签用于请求重定向,它的forwardhrefpage属性指定重定向目标,这几个属性的用法和<html:link>标签相似

猜你喜欢

转载自tuozixuan.iteye.com/blog/1273096
今日推荐