Struts1标签库

1、Bean标签库

Bean标签库中标签可以访问已经存在的JavaBean以及它们的属性,还可以

定义新的Bean,把它存放在用户指定的任何范围内,供网页或者其它元素访问,

Bean标签库分为以下三类:

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

a) <bean:cookie>

       用于检索发送到服务器端的Cookie,包括以下属性:

           id: 定义一个Cookie类型的变量,这个变量又是作为将信息

存放在page范围内的key。

           name: 指定Cookie的名字

   value: 指定Cookie的默认值,如果name指定的Cookie不存在,

   就使用value指定的属性值。

   例如: <bean:cookie id="cookie" name="cookieName" 

value="defaultValue"/>

         先检索名为"cookieName"的Cookie是否存在,如果

 存在,就把它的值赋给cookie变量的value值,如果

 不存在,cookie变量的值就采用默认值"defaultValue"。

 

b) <bean:header> 

用于检索HTTP请求中的Header信息,包括以下属性:

    id: 定义一个存放信息的字符串变量,这个变量又是作为

将信息存放在page范围内的key。

    name: 指定要检索的Header中的字段名字

    例如: <bean:header id="lang" name="Accept-Language"/>

          等于

  String value = 

(HttpServletRequest)request.getHeader("Accept-Language");

  String lang = value;

  pageContext.setAttribute("lang",value);

 

c) <bean: parameter> 

用于检索HTTP的请求参数,包括以下属性:

    id: 定义一个存放信息的字符串变量,这个变量又是作为

将信息存放在page范围内的key。

    name: 指定请求的参数名

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

    例如1: <bean:parameter id="arg1" name="testarg" 

value="noarg"/>

   等于

   String temp = request.getParameter("testarg");

   if(temp != null){

String arg1 = temp;

pageContext.setAttribute("arg1",temp);

   }else{

String arg1 = "noarg";

pageContext.setAttribute("arg1","noarg");

   }

    例如2: <bean:parameter id="arg2" multiple="yes" 

name="testarg" value="noarg"/>

   arg2是一个存放所有request请求中参数值的字符串数组。

   输出所有的参数值:

    <%

 for(int i = 0; i < arg2.length; i++){

     out.write(arg2[i] + "<br>");

 }

    %>

d) <bean:page>

用于检索JSP隐含对象(如request、session、response)包括

以下属性:

     id: 定义一个引用隐含对象的变量,这个变量又是作为

 将信息存放在page范围中的key。

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

               config、request、response和session。

     例如: <bean:page id="this_session" property="session"/>

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

   表示定义了一个"this_session"变量,它引用JSP中

   的session隐含对象,接着输出该变量中属性

   createTime的值(其实是session中属性createTime的值)。

 

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

    a) <bean:message>

用于输出资源配置文件中的一条信息,<bean:message>标签的bundle

属性指定资源配置文件,它和Struts配置文件中的<message-resources>

元素的key属性匹配。如果没有设置bundle属性,就采用默认的资源配置文件。

Struts配置文件中配置了两个资源配置文件:

<message-resources parameter="ApplicationResources"/>

<message-resources parameter="SpecialResources" key="special"/>

第一个资源配置文件没有指定key属性,因此是默认的资源配置文件,

它的资源文件为ApplicationResources.properties。

第二个资源配置文件指定key属性为"special",它的资源文件

为SpecialResources.properties。

内容: hello=Hello,ereryone!

 

<bean:message>标签三种使用方式:

    a1) <bean:message bundle="special" key="hello"/>

    bundle --> <message-resources>中的key   

    key --> SpecialResources.properties中的hello

 

    注意: 如果<bean:message>中设置了属性bundle,那么

    <message-resources>中一定要设置属性key,否则运行

    时会报错的。

    

    a2) <bean:message>标签中的属性name指定一个scope中的key,value为

资源配置文件中的key。

例如: <% request.setAttribute("stringBean","hello") %>

      <bean:message bundle="special" name="stringBean"/>

      hello --> SpecialResources.properties中的hello

 

    a3) 同时指定<bean:message>标签的name和property,name指定

        一个JavaBean,property指定JavaBean中的一个get方法,get方法

的返回值就是资源配置文件中的key。例如:

<%

    SomeBean bean = new SomeBean();

    bean.setName("hello");

    request.setAttribute("someBean",bean);

%>

<bean:message bundle="special" name="someBean" 

property="name"/>

bundle --> <message-resources>中的key 

name --> request中的key(someBean) 

property --> bean的getName()

getName()返回值(hello) --> 资源配置文件中key(hello)

 

    b) <bean:resource>

用于检索Web资源的内容,包括以下属性:

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

    name: 指定Web资源的路径

    input: 如果没有设置input属性,则id属性定义的变量为字符串

           类型;如果给input设置了值(可以是任意字符串),则id

   属性定义的变量为InputStream类型。

    例如: <bean:resource id="resource" name="/testpage1.jsp"/>

resource变量代表"/testpage1.jsp"资源,由于没有设置input

属性,因此resource变量为字符串类型,它的值为testpage1.jsp

的源文件内容。

 

    c) <bean:struts>

    用于检索Struts框架内在的对象,如ActionFormBean、ActionForward和

ActionMapping。包括以下属性:

    id: 定义一个page范围内的变量,用来引用Struts框架内的对象。

    formbean: 指定ActionFormBean对象,和Struts配置文件中的

<form-bean>元素匹配。

    forward: 指定ActionForward对象,和Struts配置文件中的

<global-forwards>元素的<forward>子元素匹配。

    mapping: 指定ActionMapping对象,和Struts配置文件中的

<action>元素匹配。

    注意: 属性id必须和下面三个属性中的一个匹配

    例如: <bean:struts id="forward" forward="BeanResources"/>

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

  以上代码的用属性id定义了一个"forward"变量,它引用

  一个名为"BeanResources"的ActionForward对象,在Struts

  文件中,与之匹配的<forward>元素为:

  <global-forward>

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

...

  </global-forward>

  <bean:write>标签输出ActionForward对象的path属性值

 

    d) <bean:include>

    和标准的JSP标签<jsp:include>很相似,都可以用来包含其他Web资源

的内容,区别在于<bean:include>标签把其它Web资源的内容存放在一

个变量中,而不是直接显示在页面上。包括以下属性:

    id: 定义一个代表其它Web资源的位置

    forward: 指定全局转发的路径,和Struts配置文件中的

     <global-forward>元素中的<forward>子元素匹配。

    page: 指定相对于当前应用的URL,以"/"开头。

    href: 指定完整的URL

    例如: <bean:include id="p1" page="/page.jsp"/>

  <bean:include id="p2" forward="success"/>

  <bean:write name="p1" filter="false"/>

  <bean:write name="p2" filter="false"/>

 

3) 用于定义或者输出JavaBean的Bean标签

    a) <bean:define>

           用于定义一个变量,属性id指定变量存放在scope中的key,toScope属性指定这个

       变量存放的范围,如果没有设置toScope属性,则这个变量存放在page

       范围内。给id属性定义的变量赋值有三种方式:

    a1) 设置value属性 例如:

        <bean:define id="age" value="25"/>

 

    a2) 同时设置name和property属性。name属性指定一个已经存在的

        Bean,property指定已经存在的Bean的某个属性,id作为key或者变量。

例如:

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

<bean:define id="contextBean" name="sessionBean" 

property="servletContext"/>

ServletContext Name:

<bean:write name="contextBean" 

property="servletContextName"/>

contextBean属于ServletContext类型

    a3) 同时设置name和type属性。name属性指定已经存在的JavaBean

        ,type属性指定这个JavaBean的完整的类名。id作为key或者变量。例如:

<bean:define id="loginForm_copy" name="loginForm" 

type="com.briup.web.LoginForm"/>

<bean:write name="loginForm_copy" property="username"/>

该标签会依次在page、request、session和application范围

中根据key(loginForm)来查找LoginForm的对象,也可以通过

属性scope来指定一个查找的范围。

注意: 属性id定义的变量也是LoginForm的对象变量(对象引用)。

 

    b) <bean:write>

用于在页面上输出某个Bean或者其属性的内容。

例如: <bean:write id="length" name="list"/>

      <bean:write name="student" property="xh"/>

      <bean:write format="#.####" name="employee" 

property="salary"/>

      <bean:write format="MM-dd-yyyyy hh:mm:ss" 

name="now" property="time"/>

      注意: 该标签自动会从page、request、session和application

      范围中根据name指定的key去查找对应的对象,也可以用

      scope属性来限定某个范围。

      另外,属性filter: true表示将html中的标记作为普通字符显示

      false表示不会把html中的标记作为普通字符显示,默认为true。

 

    c) <bean:size>

用于获得Map、Collection或者数组的长度。包括以下属性:

    id: 定义一个Integer类型的变量

    name: 指定已经存在的Map、Collection或者数组变量

    例如: <bean:size id="length" name="list"/>

  <bean:write name="length"/>

 

2、HTML标签库

该标签库可以和标准的HTML标记完成相同的功能,主要分为以下几类:

   1) 用于生成基本的HTML的标签

a) <html:html>

用于在页面的开头生成HTML的<html>元素,该标签有一个属性

   lang来显示用户显示的语言。

   例如: <html:html lang="true"/>

         如果用户的浏览器使用中文,那么该代码在运行时被解析为

 <html lang="zh-CN">

b) <html:base>

用于在页面的<head>部分生成<base>元素,用于生成当前页面

   的绝对URL路径。

例如: <html:base="http://localhost:8080/jsp"/>

      那么使用该base的页面已经将URL定位到应用程序jsp

      的根目录下面,要从该页面再去访问其它资源,只要

      使用相对路径即可。

c) <html:link>

用于生成HTML的<a>元素,包括以下的属性:

  forward: 指定全局的转发链接,对应于Struts配置文件中

           的<global-forwards>元素

  href: 指定完整的URL链接

  page: 指定相对于当前应用的URL

  例如:

  c1) <html:link forward="index">

  Link to Global ActionForward

      </html:link>

      index对应于struts-config.xml中:

      全局: <forward name="index" path="index.jsp">

     

     上述标签会自动生成HTML代码:

     <a href="/app/index.jsp">

Link to Global ActionForward

     </a>

 

  c2) <html:link href="http://localhost:8080/app/index.jsp">

  Generate an "href" link

      </html>

      生成的HTML代码如下:

      <a href="http://localhost:8080/app/index.jsp">

 Generate an "href" link

      </a>

 

  c3) <html:link page="/login.do">

   A relative link

      </html:link>

      生成的HTML代码如下:

      <a href="/app/login.do">

 Generate an "href" link

      </a>

  使用该标签的优点:

    b1) 应许再URL中以多种方式包含请求参数

    b2) 在当前浏览器关闭Cookie时,会自动重写URL,把

        jsessionid作为请求参数包含在URL中,用于跟踪

用户的状态(除了href属性)。

d) <html:img>

用于在HTML中嵌入图片

例如: 

  d1) <html:img page="/girl.gif"/>

生成HTML代码如下:

<img src="/app/girl.gif"/>

  d2) <html:img src="/app/girl.gif" 

paramId="orderid" paramName="3"/>

      生成HTML代码如下:

      <img src="/app/girl.gif?orderid=3"/>

 

   2) 用于生成HTML表单的标签

a) <html:form> 

生成HTML<form>元素

例如: <html:form action="/login">

      生成的HTML代码如下:

      <form name="loginForm" method="POST"

action="/app/login.do">

      对应于struts-config.xml中部分代码:

      <action path="/login"

      type="com.briup.web.action.LoginAction"

      name="loginForm"

      scope="request"

      input="/login.jsp"

      validate="true">

      <forward name="success" path="/member_activity.jsp"/> 

      </action>

 

b) <html:text> 

生成HTML<input type="text">元素

例如: <html:text property="age"/>

      property属性指定的字段和ActionForm中的属性匹配

      生成HTML代码如下:

      <input type="text" name="age" 

value="ActionForm中属性age的值"/>

c) <html:hidden> 

生成HTML<input type="hidden">元素

例如: <html:hidden property="age"/>

      生成HTML代码如下:

      <input type="hidden" name="age" value="同上"/>

 

d) <html:submit> 

生成HTML<input type="submit">元素

例如: <html:submit>提交</html:submit>

      生成HTML代码如下:

      <input type="submit" name="submit" value="提交"/>

 

e) <html:cancel> 

在表单上生成取消按钮

例如: <html:cancel>Cancel</html:cancel>

      生成HTML代码如下:

      <input type="submit" 

name="org.apache.struts.taglib.html.CANCEL" 

value="Cancel"/>

      注意: 在Action类中,应该以编程的方式来处理取消事件。

f) <html:reset>

生成HTML<input type="reset">元素

例如: <html:reset>重置</html:reset>

      生成HTML代码如下:

      <input type="reset" name="reset" value="重置"/>

g) <html:checkbox> 生成HTML<input type="checkbox">元素

 

h) <html:multibox> 在表单上生成复选框元素

 

i) <html:radio> 生成HTML<input type="radio">元素

 

j) <html:select> 生成HTML<select>元素

 

k) <html:option> 生成HTML<option>元素

 

l) <html:options> 生成一组HTML<option>元素

 

m) <html:optionsCollection> 生成一组HTML<option>元素

 

n) <html:file> 实现将本地文件上传到服务器端

<html:form action="sendFile.do" method="POST" 

enctype="multipart/form-data">

    <html:file property="file"/>

</html:form>

使用注意: n1) <html:file>必须嵌套在<html:form>中

  n2) <html:form>标签的method属性必须为"POST"

  n3) <html:form>标签的编码类型enctype必须设置

      为"multipart/form-data"

  n4) <html:file>标签必须设置property属性,这个

      属性和ActionForm中FormFile类型的属性对应。

在ActionForm中必须配置:

private FormFile file;

public FormFile getFile(){return file;}

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

在Action中的处理:

FormFile file = registerForm.getFile();

//获得上传文件的名字

String fname = file.getFileName();

//从file中获得输入流

InputStream is = file.getInputStream();

//构造一个输出流,并指定上传文件在服务器端存放路径

OutputStream os = new FileOutputStream(dir + "/" + fname);

        //从is中读取字节流后写入到os中

 

注意: 上述标签要使用在<html:form>标签中,但是在<html:form>标签中也可以使用标准的

html标记(如:<input type="text"/>等)。

 

   3) 显示错误或者正常消息的标签

a) <html:errors>

该标签在request和session范围内查找ActionMessages或者

子类ActionErrors的对象,再从中读取ActionMessage对象,

把ActionMessage对象中封装的的错误消息显示在页面上。

该标签处理类获取ActionMessages对象的代码如下:

ActionMessages errors = 

TagUtils.getInstance().getActionMessages(pageContext,name);

参数name指定ActionMessages对象存放在request和session

范围中的key,默认值为Globals.ERROR_KEY。

    

        在ActionForm和Action中都可以生成ActionMessages对象,在

ActionForm中的validate()验证方法返回ActionErrors对象,Struts的

控制组件RequestProcessor把ActionErrors对象存放在request范围内,

存放时的key为Globals.ERROR_KEY

(如: request.setAttribute(Globals.ERROR_KEY,errors))。

 

<html:errors>标签中包括的属性:

       name: 指定ActionMessages对象存放在request或者session范围

             中的key,默认值为Globals.ERROR_KEY。

       property: 指定显示消息的属性,若没有设置,将显示ActionMessages

                 对象中所有的ActionMessage。

       bundle: 指定资源配置文件,如果没有设置此项,将从Web应用

               默认的资源配置文件中获得消息。

       语法: errors.add(消息属性,消息); 

       如: errors.add(ActionMessages.GLOBAL_MESSAGE,

new ActionMessage("error.error1"));

   errors.add(ActionMessages.GLOBAL_MESSAGE,

new ActionMessage("error.error2"));

   saveErrors(request,errors);

   注意: 同一个消息属性可以对应多条消息

 

使用<html:errors>的三种方法:

   a1) 显示全局消息

全局消息指的是不和特定表单字段关联的消息,消息属性为

    ActionMessages.GLOBAL_MESSAGE。设置方式:

errors.add(ActionMessages.GLOBAL_MESSAGE,

new ActionMessage("error.global"));

ActionMessages.GLOBAL_MESSAGE是一个常量,它的值为

org.apache.struts.action.GLOBAL_MESSAGE,代表全局消息。

在页面中显示该消息的方式:

<html:errors property="org.apache.struts.action.GLOBAL_MESSAGE"/>

 

   2) 显示所有的消息

如果在<html:errors/>标签中没有设置property属性,将显示

     ActionMessages对象中所有的消息。页面中显示消息方式:

<html:errors bundle="special"/>

指定了特定的资源配置文件,在struts-config.xml文件中

配置为:<message-resources parameter="SpecialErrors" 

key="special"/>

 

   3) 显示和特定表单字段关联的消息

errros.add("name",new ActionMessage("error.name"));

页面中显示消息方式:

<html:errors property="name" bundle="special"/>

 

b) <html:messages>

      和<html:errors>相似,也在页面上来显示消息,

      使用例子:

   <html:messages id="message" name="key" message="true">

<bean:write name="message"/>

   </html:messages>

      该标签包括以下属性:

 name: 指定ActionMessages对象存放在request或者session中

       的key。标签处理类将根据这一属性key来检索request

       或者session范围的ActionMessages对象。

 message: 指定消息的来源。如果为true,则从request或者

  session范围内检索出属性key为Globals.MESSAGE_KEY

  的ActionMessages对象,此时属性name无效;如果

  为false,则根据属性name来检索ActionMessages对象,

  如果此时没有设置name属性,将采用默认值Globals.ERROR_KEY。

  message属性的默认值为false。

 id: 用来命名从消息集合中检索出的每个ActionMessage对象,

     它和<bean:write>标签的name属性匹配。在上述例子中,

     <html:messages>标签的处理类每次从消息集合中取出一个

     ActionMessage对象,并通过id把它命名为"message",

     <bean:write>标签接着把这个名为"message"的ActionMessage

     对象的消息输出到页面上。

 

     ActionMessages messages = new ActionMessages();

     messages.add(ActionMessages.GLOBAL_MESSAGE,

new ActionMessage("error.error1"));

     saveMessages(request,messages);

     protected void saveMessages(HttpServletRequest request,

ActionMessages messages){

 ...

 request.setAttribute

(Globals.MESSAGE_KEY,messages);

     }

 

3、Logic标签库

该标签库中的标签可以根据特定的逻辑条件来控制输出页面的内容,或者

   循环遍历集合中所有的元素。Logic标签库中的标签分为以下几类:

1) 进行比较运算的Logic标签

     a) <logic:equal> 比较变量是否等于指定的常量

例如:

a1) <% request.setAttribute("number",new Integer(100));%>

    <logic:equal name="number" value="100">

The value of number is 100

    </logic:equal>

 

a2) <%

Student student = new Student();

stu.setName("jack");

request.setAttribute("student",student);

    %>

    <logic:equal name="student" property="name" 

value="jack">

The name of student is jack

    </logic:equal>

 

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

     c) <logic:greaterEqual> 比较变量是否大于或者等于指定的常量

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

     e) <logic:lessEqual> 比较变量是否小于或者等于指定的常量

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

 

2) 进行字符串匹配的Logic标签

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

 例如: 

<% request.setAttribute("username","jackWang")%>

<logic:match name="username" scope="request" 

value="jack"> 

match is right!

</logic:match>

 

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

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

    a) <logic:empty> 判断指定的变量是否为null或者为""。

例如: <%

request.setAttribute("emptyString","");

或者

request.setAttribute("emptyString",null);

      %>

      <logic:empty name="emptyString">

  The vlaue of emptyString is empty 

      </logic:empty>

 

    b) <logic:notEmpty> 判断指定的变量是否不为null或者不是""。

    c) <logic:present> 判断指定的安全角色、用户、Cookie、HTTP

请求Header或者JavaBean是否存在。

    d) <logic:notPresent> 判断指定的安全角色、用户、Cookie、HTTP

请求Header或者JavaBean是否不存在。

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

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

 

4) 进行循环遍历的Logic标签

      <logic:iterate>是Logic标签库中最复杂的标签,也是用途最广

  的一个标签,它能够在一个循环中遍历数组、Collection、Enumeration

  Iterator或者Map中的所有元素。

a) 遍历数组

    <%

String[] colors = 

new String[]{"red","yellow","green"};

 List list = new ArrayList();

 list.add("a");

 list.add("b")

 Map map = new HashMap();

 map.put("1","a");

 map.put("2","b");

 request.setAttribute("colors",colors);

 request.setAttribute("list",list);

 request.setAttribute("map",map);

    %>

    <logic:iterate id="color" name="colors">

<bean:write name="color"/>

    </logic:iterate>

b) 遍历Collection

 <logic:iterate id="element" name="list" scope="page|request|session|applicatoin">

    <bean:write name="element"/>

 </logic:iterate>

c) 遍历Map

<logic:iterate id="entry" indexId="index" 

name="map" scope="page|request|session|applicatoin">

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

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

</logic:iterate>

  属性scope省去不写,默认从page、request、session和application查找

 

 

5) 进行请求转发或者重定向的Logic标签

    a) <logic:forward> 进行请求转发

例如: <logic:forward name="index"/>

      属性name指定的值为请求转发的全局目标资源,与Struts配置

      文件中的<global-forward>元素中的<forward>子元素匹配。

      <global-forwards>

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

   ...

      </global-forwards>

 

    b) <logic:redirect> 进行请求重定向

    它包括forward、href和page三个属性,这三个属性的使用

方法和<html:link>标签中forward、href和page属性的用法很相似。

例如: <logic:redirect href="http://localhost:8080/app/index.jsp"/>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值