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

SSH:Struts1框架(标签的使用)

2015-11-19 12:43 567 查看
Struts提供了五个标签库,即:HTML、Bean、Logic、Template和Nested。

HTML标签 : 用来创建能够和Struts 框架和其他相应的HTML 标签交互的HTML 输入表单

Bean标签:  在访问JavaBeans 及其属性,以及定义一个新的bean 时使用

Logic标签: 管理条件产生的输出和对象集产生的循环

Template标签:随着Tiles框架包的出现,此标记已开始减少使用

Nested标签:  增强对其他的Struts 标签的嵌套使用的能力

 


标签的公共特征

>>styleId:命名自定义标签创建时的脚本变量名。

>>name :指出关键字值,在该关键字下可以找到一个存在的bean 。如果给出了scope属性,则仅仅在scope中查找。否则,根据标准的顺序在各种scope中查找。标准顺序为 (page,request, session, or application)。 

>> property :指出bean 中的某个属性,可以在其中检索值。如果没有标明,则使用对象本身的值。

>><text>标签、<hidden>标签、<textarea>标签、<radio>标签、<checkbox>标签、<submit>标签、<reset>标签都有一个property属性,最后会被转换成HTML中的name属性

>> scope :定义了Bean在哪个范围(page, request, session, or application)中被查找。如果没有标明按顺序查找。脚本变量(见styleId)将在相同的范围中创建。

 

说明:

Struts标签也支持嵌套引用。

eg:

property="foo.bar.baz"

这相当于进行下面的调用:

getFoo().getBar().getBaz();

或者做为setter:

getFoo().getBar().setBaz(value);

 

虽然Struts 标签的设计原意是为了避免使用scriptlet,scriptlet的表达式还能够提供给所有的Struts 标签使用。但请确保使用完整的表达式。

错误:

<html:linkhref="'<%= "/" + name %>/index.jsp>'>

正确:

<html:linkhref="'<%= "/" + name + "/index.jsp"%>'>    // 表达式必须提供整个属性值

Html标签库


struts标签使用举例-HTML


html:base

表示所包含页面的绝对位置。这个标签只有内嵌在head标签中才有效。

<html:base/>

此行代码解析后:

<basehref=\"http://www.mymain.com/myStrutsApp/testing.jsp\">

同html的base元素。Html的base的作用:


2、 html:cancel

2、html:cancle

该标签生成一个取消按钮。当点击该按钮后action servlet会绕过相应的form bean的validate()方法,同时将控制权交给相应的action。在action中可使用Action.isCancelled(HttpServletRequest)方法判断是否被取消了。如果返回true表示这个action被取消了,否则表示这个action没有被取消。
eg.  <html:cancel>取消</html:cancel>


3、html:form

1)  标签中必须包含一个action属性,它是这个标签中唯一必需的属性。如果不具备该属性则JSP页面会抛出一个异常。之后你必须给这个action属性指定一个有效值。一个有效值是指应用程序的Struts配置文件中元素里的任何一个子元素的访问路径。而且相应的元素中必须有一个name属性,它的值是form bean的名称。

<html:form action="/login.do" >

如果你有上述一个标签 ,那么你的Struts配置文件的元素中必须有一个如下显示为粗体的元素: 
<action-mappings> 

     <action path="/login" 

      type="com.javapro.struts.LoginAction" 

      name="loginForm"

      scope="request"

      input="/login.jsp">

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

    </action>

</action-mappings>

// 这就是说一个form标签是和form bean相关联的

2) 任何包含在<form>中用来接收用户输入的标签(<text>、<password>、<hidden>、<textarea>、<radio>、<checkbox>、<select>)必须在相关的form bean中有一个指定的属性值。比如,如果你有一个属性值被指定为“username”的<text>标签,那么相关的form bean中也必须有一个名为“username”的属性。输入<text>标签中的值会被用于生成form bean的userName属性。

3)可以用focus属性来生成JavaScript,它会“定焦”(focus)到该form所包含的一个元素上。使用focus属性时你需要给它指定元素的名称。

<body>

<html:form action="/login" focus="password">

User Name: <html:text property="userName"/><br>

Password: <html:text property="password"/><br>

<html:submit/>

</html:form>

</body>

代码解析后:

<body>

<form name="loginForm"method="post"  action="/myStrutsApp/login.do">

User Name: <input type="text" name="userName"  value=""><br>

Password: <input type="text" name="password" value=""> <br>

<input type="submit"  value="Submit">

</form>

<script  type="text/javascript">

<!--

if (document.forms["loginForm"].elements["password"].type!= "hidden")

document.forms["loginForm"].elements["password"].focus()

// -->

</script>

</body>


4、html:select标签

该标签生成一个select元素。multiple属性决定是否为多选。如果指定了multiple="true"则为多选,此时对应的属性应该是一个数组。否则,此时对应的属性应该是标量。

注意:为了正确的处理未作选择的情况,在ActionForm中的reset()方法中必须将标量属性设置为默认值而将数组的长度置为0。

另外的一个重要问题就是struts如何生成option元素了,这个任务struts交给了html:option、html:options和html:optionsCollection三个标签。


html:option

该标签生成一个HTML的option元素。该标签必须嵌在html:select标签中。它的显示文本来自其标签体,也可以来自于资源文件。

eg.

<html:optionvalue="red">red</html:option>

<html:optionvalue="blue">blue</html:option>

 

来自于资源文件:

<html:optionvalue="color1" bundle="htmlselect.Colors" key="htmlselect.red"/>

它和配置文件中的<message-resources>元素的key属性匹配 --><message-resource parmeter="HtmlSelectColors"key="htmlselect.Colors"/>

<message-resource>中配置的资源文件为HtmlSelectColors.properties,相关内容为 htmlselect.red=RED


html:options

该标签生成多个HTML的option元素。该标签必须嵌在html:select标签中。

指定collection属性的方式举例如下:

<html:selectname="selectForm" property="orgId" size="1">

<html:options

collection="orgCollection"(coll)

property="orgId"(value)

labelProperty="orgName"/>(label)

       </html:select>

注意:orgCollection是在某个引藏对象范围(pageContext,rqeust,…)内存在的

未指定collection属性方式的举例如下:

<html:selectname="selectForm" property="orgId"size="1">     

     <html:options  property="orgIds"labelProperty="orgNames"/>  

  </html:select>


html:optionsCollection标签

该标签生成多个HTML的option元素。其功能和html:options标签的相同。

<html:selectname="selectForm" property="orgIds"size="1">      

            <html:optionsCollection

name="selectForm"

property="orgs"

label="orgName"

value="orgId"/>   

     </html:select>

注意:orgs一定是在ActionForm里面出现的一个Collection类型的Bean


5、html:checkbox标签

该标签生成checkbox元素

eg

<html:checkboxproperty="loves" value="bb"name="studentForm"/>bb

<html:checkboxproperty="loves" value="cc"name="studentForm"/>cc

<html:checkboxproperty="loves" value="dd"name="studentForm"/>dd

在名为studentForm中有一个名为loves的String类型的数组来与此标签对应,这样才能在studentForm中接收到该标签的多选值


6、html:multibox标签

该标签用法同html:checkbox


8、html:radio标签

该标签生成radio元素

eg

<html:radioproperty="sex" name="studentForm" value="男"/>男

在名为studentForm中有一个名为sex的String类型的属性来与此标签对应,这样才能在studentForm中接收到该标签的单选值


9、html:img标签

最重要的属性page:图象文件的路径,前面必须带有一个斜线。其它属性:heignt、width、alt。

      <html:img

page="/logo.gif"height="50"  width="200" alt="Web Logo"/>


10、html:link标签

<html:linkpage="/index.html">Click demo</html:link>

此行代码解析后:

<ahref="/index.html">Click demo</a>


11、html:html标签

它有两个属性:locale和xhtml,两者都不是必需的。

<html:htmllocale=\"true\">

此行代码解析后:

<htmllang=\"en\">

说明:

生成的结果取决于Struts应用程序所位于的服务器的locale。如果你将应用程序部署到一个不同locale的服务器,你不需要改变代码,Locale会自动调整。

 


12、html:errors(不常用)

通过一个简单的<html:errors/>标签,你就可以在一个JSP页面上显示完全自定义的错误信息。功能超强大!!

说明:

这个标签在Request对象的属性集合中查找reserved key。如果它找到一个reserved key,它就假设这个key是一个String、或是一个String数组  (它包含在模块的MessageResources中查找的message keys)、或是类型为org.apache.struts.action.ActionErrors的一个对象。

如果在应用程序资源中存在相应的信息,那么就可以用下面这些可选的message keys:

errors.header  or errors.prefix:相应的信息在错误信息的单独列表前显示。

errors.footer or  errors.suffix:相应的信息在错误信息的单独列表后显示。


13、html:password

eg:

<html:passwordproperty="password" redisplay="false"/>

该标签中的一个很重要的属性是"redisplay",它用于重新显示以前输入到这个区域中的值。该属性的缺省值为true。然而,为了使password不能被重新显示,你或许希望将该属性的值设为false。


struts标签使用举例-logic


logic:empty

该标签是用来判断是否为空的。如果为空,该标签体中嵌入的内容就会被处理。该标签用于以下情况:

1)当Java对象为null时;

2)当String对象为""时; 

3)当java.util.Collection对象中的isEmpty()返回true时;

4)当java.util.Map对象中的isEmpty()返回true时。

eg. 

< logic:empty   name="userList">   

             ...   

 < /logic:empty> 

 该句等同于:

 if   (userList.isEmpty())   {   

                ...   

 }


logic:notEmpty

该标签的应用正好和logic:empty标签相反,略。


logic:equal

该标签为等于比较符。
eg1. 比较用户的状态属性是否1,若为1,输出"启用";

 < logic:equal   name="user"  property="state"   value="1">

          启用

 < /logic:equal>
eg2. 如果上例中的value值是动态获得的,例如需要通过bean:write输出,因struts不支持标签嵌套,可采用EL来解决该问题。

<logic:equal   name="charge"  property="num"  value="${business.num}">   

                   ......

< /logic:equal>


logic:notEqual

该标签意义与logic:equal相反,使用方法类似,略。


logic:forward

该标签用于实现页面导向,查找配置文件的全局forward。

eg. < logic:forward name="index"/>


logic:greaterEqual

为大于等于比较符。
eg. 当某学生的成绩大于等于90时,输出“优秀”:

< logic:greaterEqual name="student" property="score"value="90">

                 优秀

< /logic:greaterEqual>


logic:greaterThan

此为大于比较符,使用方法同logic:greaterEqual,略;


logic:lessEqual

此为小于等于比较符,使用方法同logic:greaterEqual,略;


logic:lessThan

此为小于比较符,使用方法同logic:greaterEqual,略;


logic:match

此标签比较对象是否相等;
eg1. 检查在request范围内的name属性是否包含"amigo"串: 

   < logic:match name="name"scope="request" value="amigo">

     < bean:write name="name"/>中有一个“amigo”串。

   < /logic:match>

eg2. 检查在request范围内的name属性是否已“amigo”作为起始字符串:

           < logic:matchname="name" scope="request" value="amigo"location="start">

              < bean:write name="name"/>以“amigo”作为起始字符串。

            < /logic:match>

         eg3. 

            <logic:match header="user-agent" value="Windows">

              你运行的是Windows系统

            </logic:match>


logic:notMatch

此标签用于比较对象是否不相同,与logic:match意义相反,使用方法类似,略。


logic:messagePresent

该标签用于判断ActionMessages/ActionErrors对象是否存在;
eg. 如果存在error信息,将其全部输出:

    < logic:messagePresentproperty="error"> 

       < html:messagesproperty="error" id="errMsg" > 

           <bean:write name="errMsg"/> 

       < /html:messages>   

  < /logic:messagePresent >


logic:messagesNotPresent

该标签用于判断ActionMessages/ActionErrors对象是否不存在,使用方法与logic:messagePresent类似,略


logic:present

此标签用于判断request对象传递参数是否存在。
eg1. user对象和它的name属性在request中都存在时,输出相应字符串:

   < logic:present name="user"property="name">

            user对象和该对象的name属性都存在

   < /logic:present> 
eg2. 若有一个名字为“user”的JavaBean,输出对应字符串:

    < logic:present name="user" >

       有一个名字为“user”的JavaBean。

    < /logic:present>
eg3. 

    < logic:present header="user-agent">

        we got a user-agent header.

    < /logic:present>


logic:notPresent

此标签用于判断request对象传递参数是否不存在,意义与了logic:present相反,使用方法类似,略。


logic:redirect

该标签用于实现页面转向,可传递参数。

eg1. < logic:redirect href="http://www.chinaitlab.com"/>


logic:iterator

用于显示列表为collection的值(List ,ArrayList,HashMap等)。
eg1. 逐一输出用户列表(userlList)中用户的姓名:
< logic:iterate  id="user"name="userList">

     < bean:write name="user"property="name"/>< br>

< /logic:iterate>
eg2. 从用户列表中输出从1开始的两个用户的姓名:

< logic:iterate  id="user" name="userList"indexId="index"  offset="1" length="2">

   < bean:write name="index"/>.

   < bean:write name="user"property="name"/>< br>

 < /logic:iterate>
eg3. logic:iterator标签的嵌套举例

  < logic:iterate id="user" indexId="index"name="userList">

    < bean:write name="index"/>. 

    < bean:write name="user"property="name"/>< br>

    < logic:iterate id="address"name="user" property="addressList" length="3"offset="1">

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

    < /logic:iterate>

  </logic:iterate>

 


struts标签使用举例-BEAN


bean:write

该标签将指定的bean的属性值写到当前的JspWriter中,并且可以对输出进行格式化。

例如在struts的action着那个通过request.setAttribute("names","dddd");将属性值dddd中放入names,可在jsp页面中通过bean:write将names属性输出。

eg:<bean:write name="names"/>

对于日期型的属性,可在bean:write标签中指定format来输出日期格式,

eg:<bean:write name="date"format="MM/dd/yyyy"/>

如果要输出某对象的某属性,例如属性名为person的对象的name属性,可通过如下方式:

eg:<bean:write name="person"property="name"/>

 


bean:message

该标签用来从指定的locale中取回国际化的消息并输出,在这个过程中我们还可以传递5个以内的参数。message key可以通过key直接指定,也可以通过name和property间接的指定。

eg1.  <bean:messagekey="welcome.title.content"/>

该句要求在资源文件中有welcome.title.content的键值对(资源文件ApplicationSource.properties在struts的配置文件中指定)。

eg2.  传递参数信息的bean:message的用法,

<bean:messagekey="greeting" arg1="good morning" arg2="goodevening"/>

在资源文件中greeting的配置举例如下:

greeting = hello, {0}, {1}.


bean:parameter

该标签取回请求中的参数值。如果没有指定multiple属性则依据刚取回的值创建一个String类型的bean,否则根据刚取回的值创建一个String[]类型的数组。然后用id属性值将String或String[]绑定到page作用域中(这种绑定是为了其它标签能够使用该值),并创建对应的scripting变量(这种变量是为了JSP脚本能够使用该值)。

eg. 当请求如下的jsp页面时:http://localhost:8080/test.jsp?orgId=1

在test.jsp页中可通过如下方式获得orgId参数:

<bean:parameterid="ok" name="orgId"/>

  <bean:write name="ok"/>


bean:size

该标签创建一个java.lang.Integer类型的bean,该值为该标签指定的Collection或Map,List中所含元素的个数。它可和logic:iterate标签配合使用。

如下语句输出userList属性中元素的个数:

eg. <bean:sizeid="size" name="userList"/>

      <bean:write name="size"/>

 

 


 struts标签使用举例-NESTED


StrutsNested标签库的分两部分:

一部分用于表达JavaBean之间的嵌套关系

另一部分能够在特定的嵌套级别提供和其他Struts标签相同的功能。

<nested:nest>,定义一个新的嵌套级别

<nested:writeNesting>,输出当前嵌套级别信息

<nested:nest>标签可以表达JavaBean之间的嵌套关系

eg.

以三个JavaBean为例,分别是:PersonForm Bean,Person Bean和Address Bean,在PersonForm Bean中包含一个Person Bean类型的属性person,在Person Bean中又包含一个Address Bean类型的属性address。

则用nested标签表示如下:

定义两个<nested:nest>标签,第一个<nested:nest>标签嵌套在<html:form>标签中,如下:

<html:form action="/showPerson">

<nested:nestproperty="person">

LastName:<nested:textproperty="lastName"/><BR>

          .....

</nested:nest>

</html:form>

以上<nested:nest>标签的上层JavaBean位于<html:form>表单标签对应的PersonForm Bean,<nested:nest>标签的property属性为“person",代表PersonForm Bean的person属性,这个person属性代表Person Bean,因此嵌套在<nested:nest>标签内部的Nested标签都相对于这个Person Bean,例如第一个<nested:text>标签的property属性”lastName“,代表Person Bean的lastName属性。

第二个<nested:nest>标签嵌套在第一个<nested:nest>标签内部:如下

<html:formaction="/showPerson">

<nested:nestproperty="person">

.............

          <nested:nestproperty="address">

Current nesting is :

<nested:writeNesting/><br>

Street1:<nested:text property="street1"/><BR>

</nested:nest>

</nested:nest>

</html:form>

在以上代码中,第二个<nested:nest>标签的property属性为“address",代表PersonBean 的address属性,这个address属性代表Address Bean,因此嵌套在第二个<nested:nest>标签内部的Nested标签都相对于这个Address Bean。

第二个<nested:nest>标签内还嵌套了一个<nested:writeNesting>标签,它显示当前的嵌套级别,输出结果为”person.address".

在默认情况下,<nested:nest>标签的property属性为当前ActionForm Bean的某个属性,或者位于上层<nested:nest>标签对应的JavaBean的某个属性。

可以使用<nested:root>标签来显式指定顶层级别的JavaBean。

<nested:root>标签的name属性指定JavaBean的名字,嵌套在<nested:root>标签中的<nested:nest>标签的property属性为这个JavaBean的某个属性。


和其他标签库中的标签功能相同的Nested标签

许多Nestd标签库中的标签具有和其他标签库中的标签相同的功能,区别在于Nested标签库中的标签属性相对于当前的嵌套级别,例如

   <nested:nest property ="person">

        Last name:<nested:text property="lastName"/>

   </nested:nest>

上面的<nested:text>标签和<html:text>标签具有相同的功能,都可以生成文本框,两者的区别在于<nested:text>标签的property属性为位于当前嵌套级别对应的JavaBean的某个属性,而<html:text>标签的property属性为於当前表单对应的ActionForm
Bean的某个属性。比如我有一个User类和一个UserInfo类,前者记录用户的帐号密码,后者记录用户的详细信息。前者也有一个UserInfo属性,这样它们两者是嵌套了。   

现在我要把这个用户的帐号和详细信息都显示到界面上。  一种方式是在actionForm中用两个属性User user和UserInfo userInfo来存储,在jsp中就可以用如下方式显示出来:   

   <nested:nest property="user">   

           帐号:<nested:write    property="account"/>  

   </nested:nest>   

   <nested:nest property="userInfo">  

           姓名:<nested:write    property="name"/>  

           性别:<nested:write    property="sex"/>  

   </nested:nest>

   由于user和userInfo本身就是嵌套的,所以第二种方式就在actionForm中使用一个User  user属性即可:

   <nested:nest property="user">   

           帐号:<nested:write  property="account"/>   

      <nested:nest property="userInfo">  

          姓名:<nested:write    property="name"/>  

          性别:<nested:write    property="sex"/>  

      </nested:nest>   

   </nested:nest>   

    

   这样处理是不是很方便了,actionForm可以直接放上数据存储对象,如果使用了hibernate做数据持久层,我们就可以直接把持久化对象放入actionForm来显示到界面上,不用在actionForm里写很多属性来分别存储数据,也免去了给这些属性分别赋值的过程。   

    

   如果我们把上边例子中的<nested:write/>标记换成<nested:text/>,这就类似于<html:text/>标记,是一个输入框,这样我们就可以把界面上输入一次提交到actionForm中的这个数据存储对象,比如user。我们在action中就可以直接获得这个user进行处理,非常方便。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息