数据标签主要包括:
action bean data debug i18n include param push set text url property
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@page import="com.sun.org.apache.bcel.internal.classfile.Attribute"%> 3 <%@ taglib prefix="s" uri="/struts-tags"%> 4 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 6 <html> 7 <head> 8 <title>bean.jsp</title> 9 </head> 10 11 <body> 12 <s:bean name="com.lihui.Student">zhe<!-- 没有id属性,只能在bean标签内部访问属性 --> 13 <s:param name="name" value="'lihui'"></s:param><!-- 这里如果传的是字符串一定要加单引号 --> 14 <s:param name="age" value="22"></s:param> 15 <s:property value="name"/><br> 16 <s:property value="age"/><br> 17 </s:bean> 18 19 <s:bean name="com.lihui.Student" id="student"> 20 <s:param name="name" value="'lvliansheng'"></s:param> 21 <s:param name="age" value="24"></s:param> 22 </s:bean> 23 <s:property value="#student.name"/><br> 24 <s:property value="#student.age"/><br> 25 <hr/> 26 <b>Date标签实例:</b> 27 <p>MM/dd/yyyy格式: <s:date name="currentDate" format="MM/dd/yyyy"/></p> 28 <p>MM/dd/yyyy hh:mm格式: <s:date name="currentDate" format="MM/dd/yyyy hh:mm"/></p> 29 <p>nice属性为false(不输出指定日期与当前日期之间的时差),并且不指定格式: <s:date name="currentDate" nice="false"/></p> 30 <p>nice属性为true,并且不指定格式: <s:date name="currentDate" nice="true"/></p> 31 <br/> 32 <br/> 33 <h2>s:url 来生成一个URL地址</h2> 34 (1)只指定value属性的形式。<br> 35 <s:url value="com.lihui"></s:url><br/> 36 (2)指定action属性,且使用param传入参数的形式。<br> 37 <s:url action="bean"> 38 <s:param name="currentDate" value="'时间'"></s:param> 39 </s:url><br/> 40 (3)既不指定action属性,也不指定value属性,且使用param传入参数的形式.<br> 41 <s:url includeParams="get"> 42 <s:param name="id" value="%{'22'}"></s:param> 43 </s:url><br> 44 (4)同时指定action属性和value属性,且使用param传入参数的形式。<br> 45 <s:url action="bean" value="com.lihui"> 46 <s:param name="name" value="'张三'"></s:param> 47 </s:url> 48 </body> 49 </html>
这里需要一个类:
1 package com.lihui; 2 3 import java.util.Date; 4 5 import com.opensymphony.xwork2.ActionSupport; 6 7 public class DateTag extends ActionSupport { 8 private Date currentDate; 9 10 @Override 11 public String execute() throws Exception { 12 setCurrentDate(new Date()); 13 return SUCCESS; 14 } 15 16 public void setCurrentDate(Date date){ 17 this.currentDate = date; 18 } 19 20 public Date getCurrentDate(){ 21 return this.currentDate; 22 } 23 24 }
struts.xml配置:
1 <package name="com.lihui" extends="struts-default"> 2 <action name="bean" class="com.lihui.DateTag"> 3 <result name="success">/pages/bean.jsp</result> 4 </action> 5 </package>
显示效果如下: