日期:2014-05-17  浏览次数:20869 次

Struts2 数据标签(二)输出转义的HTML格式
1.s:property标签概述
  s:property标签的功能就是输出数据,其属性及意义如下:
属性名默认值类型说明
DefaultStringvalue属性的值是null时的默认值
escapeTrueBoolean是否转义HTML代码
value<top of stack>Object要输出显示的数据源

2.新建Action实例类:
package com.wl.data.tagsAction;

import com.opensymphony.xwork2.ActionSupport;

public class PrintHTML extends ActionSupport {

    private String html;
    
    public String getHtml() {
        return html;
    }

    public void setHtml(String html) {
        this.html = html;
    }

    @Override
    public String execute() throws Exception {

        html="<table border=2><tr><td>我的名字是:张三</td></tr></table>";
        return "printhtml";
    }

}

3.新建JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
  <body>
     name的值是:
     <s:property value="html" escape="true" />
     <br/>
     name的值是:
     <s:property value="html" escape="false"/>
  </body>
</html>

4.修改struts.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <package name="Struts2.1" extends="struts-default">
       <action name="printhtml" class="com.wl.data.tagsAction.PrintHTML">
           <result name="printhtml">/dataTags/printhtml.jsp</result>
       </action>
   </package>
</struts>

5.运行结果