176142998

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  116 Posts :: 0 Stories :: 45 Comments :: 0 Trackbacks
Struts2提供了大量丰富的标签供使用,它不再像Struts1中一样,将种类标签进行分门列别,但可以根据其使用的用途用以区别。本文通过对Struts2中数据标签的学习,来对Struts2中标签的学习起到一个抛砖引玉的作用。文中将介绍Action标签、Bean标签、Data标签、Include标签、Param标签、Set标签、Text标签、Property标签等标签。
一、<s:action>标签

Action标签,顾名思义,是用来调用Action的标签,在JSP中页面中,可以指向具体指定某一命名空间中的某一Action。而标签的主体用于显示及渲染Actionr的处理结果。

1WebRoot\pages\dataTagssuccess.jsp处理页面

 

<%@ page contentType="text/html; charset=GBK" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>

       
<head>

              
<title>Action Tag 示例</title>

       
</head>

       
<body>

              
<h2>Action Tag 示例</h2>

                     
<s:action name="success">

                            
<b><i>s:action标签用于在页面显示结果.</i></b></div>

                     
</s:action>

       
</body>

</html>

 

这里使用<s:action>标签进行页面跳转,并用于显示处理的结果。

2.先来看struts.xml中的配置:

 

       <action name="actionTag" class="com.sterning.actionTag">

           
<result name="success">/pages/dataTags/success.jsp</result>

       
</action>

 

3.接着创建actionTag类:代码如下:

 

package com.sterning;

import com.opensymphony.xwork2.ActionSupport;

publicclass actionTag 
extends ActionSupport {

    
public String execute() throws Exception{

       returnSUCCESS;

    }


}

 

其实该类中没有做任何处理,只是进行页面跳转而已。

4.运行效果。


1.<s:action>标签

二、<s:bean>标签

Bean标签,当然需要一个JavaBean。其的属性值的操作是经由Bean标签中的参数属性来进行赋值。当然,它还有一个id属性可以进行赋值,由于就可以在上下文中使用这个Bean。请看如下的页面:

1WebRoot\pages\dataTags\beanTag.jsp,代码如下:

 

<%@ page contentType="text/html; charset=GBK" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>

       
<head>

              
<title>Bean Tag 示例</title>

       
</head>

       
<body>

              
<h2>Bean Tag 示例</h2>

                     
<s:bean name="com.sterning.companyName" id="uid">

                            
<s:param name="name">sterning</s:param> 

                                   
<s:property value="%{name}" /><br>

                     
</s:bean>

       
</body>

</html>

 

可参其关联的JavaBeancom.sterning.companyName,同时参数name赋值为sterning

2.首先创建Action进行跳转, src\com\sterning\beanTag.java,代码如下:

 

package com.sterning;

import com.opensymphony.xwork2.ActionSupport;

public class beanTag extends ActionSupport {

       
public String execute() throws Exception{

              
return SUCCESS;

       }


}

 

然后创建JavaBeansrc\com\sterning\companyName.java,代码如下:

 

package com.sterning;

public class companyName {

       
private String name;

       
public void setName(String name){

              
this.name =name ;

       }


       
public String getName(){

              
return name;

       }


}

 

3Struts.xml的配置

这里配置很简单,与前面的例子差不多。

 

              <action name="beanTag" class="com.sterning.beanTag">

                     
<result name="success">/pages/dataTags/beanTag.jsp</result>

              
</action>

 

4.运行效果


2.<s:bean>标签

三、<s:date>标签

Data标签方便在页面进行格式化的日期输出。格式有多种可供选择。同时,还可以通过在properties属性文件中定义好”struts.date.format”参数的值,从而自定义格式输出。

Date标签包含三个属性,可以从下面的代码中感受一下,分别是:

l         Name:

l         Nice

l         Format

1WebRoot\pages\dataTags\dateTag.jsp

 

<%@ page contentType="text/html; charset=GBK" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>

       
<head>

              
<title>Date Tag 示例</title>

       
</head>

       
<body>

              
<h2>显示当前的时间</h2>

                     
<table border="1" width="35%">

                            
<tr>

                                   
<td><b>日期格式</b></td>

                                   
<td><b>日期</b></td>

                            
</tr>

                            
<tr>

                                   
<td>Day/Month/Year</td>

                                   
<td><s:date name="currentDate" format="dd/MM/yyyy" /></td>

                            
</tr>

                            
<tr>

                                   
<td>Month/Day/Year</td>

                                   
<td><s:date name="currentDate" format="MM/dd/yyyy" /></td>

                            
</tr>

                            
<tr>

                                   
<td>Month/Day/Year</td>

                                   
<td><s:date name="currentDate" format="MM/dd/yy" /></td>

                            
</tr>

                            
<tr>

                                   
<td>Month/Day/Year Hour<B>:</B>Minute</td>

                                   
<td><s:date name="currentDate" format="MM/dd/yy hh:mm" /></td>

                            
</tr>

                            
<tr>

                                   
<td>Month/Day/Year Hour<B>:</B>Minute<B>:</B>Second</td>

                                   
<td><s:date name="currentDate" format="MM/dd/yy hh:mm:ss" /></td>

                            
</tr>

                            
<tr>

                                   
<td>Nice Date (Current Date & Time)</td>

                                   
<td><s:date name="currentDate" nice="false" /></td>

                            
</tr>

                            
<tr>

                                   
<td>Nice Date</td>

                                   
<td><s:date name="currentDate" nice="true" /></td>

                            
</tr>

                     
</table>

       
</body>

</html>

 

2src\com\sterning\beanTag.java

该项类更加简单,页面跳转

 

package com.sterning;

import com.opensymphony.xwork2.ActionSupport;

public class beanTag extends ActionSupport {

       
public String execute() throws Exception{

              
return SUCCESS;

       }


}

 

3Struts.xml配置

 

              <action name="dateTag" class="com.sterning.dateTag">

                     
<result>/pages/dataTags/dateTag.jsp</result>

              
</action>

 

4.运行效果


3.<s:date>标签

四、<s:include>标签

<s:include>标签用于在当前页面中包含来自其它servletJSP页面的处理结果。由于是页面与页面(或servlet)之间的页面包含,因此不需要action来进行页面的跳转。

1WebRoot\pages\dataTags\includeTag.jsp

 

<%@ page contentType="text/html; charset=GBK" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>

       
<head>

              
<title>Include Tag 示例</title>

       
</head>

       
<body>

              
<h2>Include Tag 示例</h2>

                     
<s:include value="myBirthday.jsp" />

       
</body>

</html>

 

这里包含了另外一个页面myBirthday.jsp,其实相当于在JSP页面里包含其它的页面。原理一样的。

2WebRoot\pages\dataTags\myBirthday.jsp

这个页面利用了上面所讲的<s:date>标签进行日期的格式化输出

 

<%@ page contentType="text/html; charset=GBK" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>

       
<head>

              
<title>Include Tag 示例</title>

       
</head>

       
<body>

                     
<table border="1" width="35%">

                            
<tr>

                                   
<td><b>Date Format</b></td>

                                   
<td><b>Date</b></td>

                            
</tr>

                            
<tr>

                                   
<td>Day/Month/Year</td>

                                   
<td><s:date name="myBirthday" format="dd/MM/yyyy" /></td>

                            
</tr>

                            
<tr>

                                   
<td>Month/Day/Year</td>

                                   
<td><s:date name="myBirthday" format="MM/dd/yyyy" /></td>

                            
</tr>

                            
<tr>

                                   
<td>Month/Day/Year</td>

                                   
<td><s:date name="myBirthday" format="MM/dd/yy" /></td>

                            
</tr>

                            
<tr>

                                   
<td>Month/Day/Year Hour<B>:</B>Minute</td>

                                   
<td><s:date name="myBirthday" format="MM/dd/yy hh:mm" /></td>

                            
</tr>

                            
<tr>

                                   
<td>Month/Day/Year Hour<B>:</B>Minute<B>:</B>Second</td>

                                   
<td><s:date name="myBirthday" format="MM/dd/yy hh:mm:ss" /></td>

                            
</tr>

                            
<tr>

                                   
<td>Nice Date (Current Date & Time)</td>

                                   
<td><s:date name="myBirthday" nice="false" /></td>

                            
</tr>

                     
</table>

       
</body>

</html>

 

3Struts.xml配置

 

              <action name="includeTag" class="com.sterning.includeTag">

                     
<result>/pages/dataTags/includeTag.jsp</result>

              
</action>

 

4.运行效果


4.<s:include>标签

五、<s:param>标签

Param标签用于传递参数,如给<s:bean>标签传递参数。它有如下两个属性:

l         Name(String):参数名;

l         Value(Object):参数值。

1WebRoot\pages\dataTags\paramTag.jsp

 

<%@ page contentType="text/html; charset=GBK" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>

       
<head>

              
<title>Param Tag 示例</title>

       
</head>

       
<body>

              
<h2>Param Tag 示例</h2>

                     
<ui:component>

                            
<ui:param name="empname">Emp1</ui:param><br>

                            
<ui:param name="empname">Emp2</ui:param><br>

                            
<ui:param name="empname">Emp3</ui:param> 

                     
</ui:component>

       
</body>

</html>

 

2Struts.xml配置

 

              <action name="paramTag">

                     
<result>/pages/dataTags/paramTag.jsp</result>

              
</action>

 

3.运行效果


5.<s:param>标签

六、<s:set>标签

Set标签比较简单。Set标签用户将某一值赋给某一变量,因此,任何对该项值的引用都可以通过该变量来得到该值。该变量的活动范围可自定义。如下例中,定义一健/值对,对值的引用,直接引用值就可以。。请看示例

1WebRoot\pages\dataTags\ setTag.jsp

 

<%@ page contentType="text/html; charset=GBK" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>

       
<head>

              
<title>Set Tag 示例</title>

       
</head>

       
<body>

              
<h2>Set Tag 示例</h2>

                     
<s:set name="technologyName" value="%{'Java'}"/>

                            Technology Name: 
<s:property value="#technologyName"/>

       
</body>

</html>

 

2Struts.xml配置

 

              <action name="setTag">

                     
<result>/pages/dataTags/setTag.jsp</result>

              
</action>

 

3.运行效果


6.<s:set>标签

七、<s:property>标签

Property顾名思义,可以与<s:bean>标签结合使用,一个是给bean赋值,一个是从bean中读取值。直接来看示例:

1WebRoot\pages\dataTags\propertyTag.jsp

 

<%@ page contentType="text/html; charset=GBK" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>

       
<head>

              
<title>Property Tag 示例</title>

       
</head>

       
<body>

              
<h2>Property Tag 示例</h2>

                     
<!-- Example to pick the value through bean class -->

                     
<s:bean name="com.sterning.companyName" id="uid">

                            
<s:param name="name">sterning</s:param> 

                                   
<s:property value="%{name}" /><br>

                            
</s:bean>

                     
<!-- Default value -->

                                   
<s:property value="name" default="Default Value" />

       
</body>

</html>

 

2Bean

当然这里用到了Bean. com\sterning\ companyName.java,代码如下:

 

package com.sterning;

public class companyName {

       
private String name;

       
public void setName(String name){

              
this.name =name ;

       }


       
public String getName(){

              
return name;

       }


}

 

3src"com"sterning" propertyTag.java

进行页面跳转

 

package com.sterning;

import com.opensymphony.xwork2.ActionSupport;

public class propertyTag extends ActionSupport {

  
public String execute() throws Exception{

    
return SUCCESS;

 }


}

 

4Struts.xml配置

 

              <action name="propertyTag" class="com.sterning.propertyTag">

                     
<result>/pages/dataTags/propertyTag.jsp</result>

              
</action>

 

5.运行效果


7.<s:property>标签

posted on 2008-08-04 10:58 飞飞 阅读(372) 评论(1)  编辑  收藏

Feedback

# re: Struts2中数据标签使用示例 2008-12-12 13:26 tpf
好,正需要了!
  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: