内置组件和

第一部分:源码
Output.javacss

// Copyright 2007, 2008 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry5.corelib.components;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.SupportsInformalParameters;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.internal.util.InternalUtils;

import java.text.Format;

/**
 * A component for formatting output. If the component is represented in the template using an element, then the element
 * (plus any informal parameters) will be output around the formatted value.
 */
@SupportsInformalParameters
public class Output
{
    /**
     * The value to be output (before formatting). If the formatted value is blank, no output is produced.
     */
    @Parameter(required = true, autoconnect = true)
    private Object value;

    /**
     * The format to be applied to the object.
     */
    @Parameter(required = true, allowNull = false)
    private Format format;

    /**
     * If true, the default, then output is filtered, escaping any reserved characters. If false, the output is written
     * raw.
     */
    @Parameter
    private boolean filter = true;

    /**
     * The element name, derived from the component template. This can even be overridden manually if desired (for
     * example, to sometimes render a surrounding element and other times not).
     */
    @Parameter("componentResources.elementName")
    private String elementName;

    @Inject
    private ComponentResources resources;


    boolean beginRender(MarkupWriter writer)
    {
        if (value == null) return false;

        String formatted = format.format(value);

        if (InternalUtils.isNonBlank(formatted))
        {
            if (elementName != null)
            {
                writer.element(elementName);

                resources.renderInformalParameters(writer);
            }

            if (filter) writer.write(formatted);
            else writer.writeRaw(formatted);

            if (elementName != null) writer.end();
        }

        return false;
    }

    // For testing.

    void setup(Object value, Format format, boolean filter, String elementName, ComponentResources resources)
    {
        this.value = value;
        this.format = format;
        this.filter = filter;
        this.elementName = elementName;
        this.resources = resources;
    }
}

OutputRaw.javahtml

// Copyright 2007, 2008 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry5.corelib.components;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.ioc.annotations.Inject;

/**
 * Used to output raw markup to the client. Unlike, say, an expansion, the output from OutputRaw is unfiltered, with any
 * special characters or entities left exactly as is. This is used in situations where the markup is provided
 * externally, rather than constructed within Tapestry.
 *
 * @see MarkupWriter#writeRaw(String)
 */
public class OutputRaw
{
    /**
     * The value to to render. If unbound, and a property of the container matches the component's id, then that
     * property will be the source of the value.
     */
    @Parameter(required = true, autoconnect = true)
    private String value;

    @Inject
    private ComponentResources resources;

    boolean beginRender(MarkupWriter writer)
    {
        if (value != null && value.length() > 0) writer.writeRaw(value);

        // Abort the rest of the render.

        return false;
    }

    // For testing:

    void setValue(String value)
    {
        this.value = value;
    }

}

第二部分:简介
<t:output/>和<t:outputraw/>都是用来格式化输出的。
<t:outputraw/>能够说是<t:output/>的一个简化版。
第三部分:测试
如下是测试用的最终代码
Start.tmljava

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
	<head>
		<title>t:output</title>
		<style>
			.red{color:red;}
			.green{color:green;}
		</style>
	</head>
    <body>
	    <t:output value="now" format="formatDate"/><br/>
	    <a href="#" t:type="output" t:value="now" t:format="formatDate" class="red"/><br/>
	    <t:output value="now" format="formatDate" class="green" elementName="componentResources.getElementName('a')"/><br/>
	    <t:output value="now" format="formatDate" class="green" elementName="componentResources.pageName"/><br/>
	    <t:output value="index" format="formatInt"/><br/>
	    <t:output value="message" format="formatMessage"/><br/>
	    <t:output value="message" format="formatMessage"/><br/>
	    <t:output value="html" format="formatHtml" filter="true"/><br/>
	    <t:output value="html" format="formatHtml" filter="false"/><br/>
    </body>
</html>

Start.javaweb

package jumpstart.web.pages;

import java.text.Format;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ChoiceFormat;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

@SuppressWarnings("unused")
public class Start {
	@Property
	private Date now = new Date();
	@Property
	private Format formatDate = new SimpleDateFormat("yyyy年MM月dd日");
	@Property
	private int index = 2;
	@Property
	private Format formatInt = new ChoiceFormat(new double[] { 1, 2, 3, 4, 5, 6, 7 }, new String[] { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" });
	@Property
	private Object[] message = { 0.25, new Date() };
	@Property
	private Format formatMessage = new MessageFormat("Now time is {1,time,long},first number is {0,number,percent}");
	@Property
	private Object[] html = { "<span class=\"error\">html text</span>"};
	@Property
	private Format formatHtml = new MessageFormat("this is span element:{0}");
}

最终的效果express

最终的效果(HTML)apache

<html>
	<head><link type="text/css" rel="stylesheet" href="/jumpstart/assets/f942239ffe3a84ba/core/default.css"></link>
		<title>t:output</title>
		<style>
			.red{color:red;}
			.green{color:green;}
		</style>
	<meta content="Apache Tapestry Framework (version 5.2.4)" name="generator"></meta></head>
    <body>
	    2011年04月03日<br/>
	    <a class="red" href="#">2011年04月03日</a><br/>
	    <a class="green">2011年04月03日</a><br/>
	    <Start class="green">2011年04月03日</Start><br/>
	    Mon<br/>
	    Now time is 上午11时06分23秒,first number is 25%<br/>
	    Now time is 上午11时06分23秒,first number is 25%<br/>
	    this is span element:&lt;span class="error"&gt;html text&lt;/span&gt;<br/>
	    this is span element:<span class="error">html text</span><br/>
    </body>
</html>


第四部分:分析
<t:output/>经过指定value值来对应页面类中的一个属性,能够是单个对象或者对象数组。
经过指定format属性,来使用对应页面类中的一个java.text.Format实现类及其子类来格式化输出value指定的属性。
经过指定filter是true(默认),决定format格式化value的结果须要转换特殊字符(例如HTML中的<和>是否须要转换为&lt;和&gt;)而后再输出到HTML页面,假如格式化后的字符串结果,包含HTML页面的一个element,那么输出的时候就是把element看做普通的文本。
经过指定filter是false,则不会对特殊字符进行过滤,格式化后的字符串结果包含element在输出到HTML的时候会被看成HTML的element进行解析。
elementName属性能够为<t:output/>增长一个element在格式化字符串外面,例如示例中的
     <t:output value="now" format="formatDate" class="green" elementName="componentResources.getElementName('a')"/><br/>
     <t:output value="now" format="formatDate" class="green" elementName="componentResources.pageName"/><br/>
两个被解析之后就变成了
     <a class="green">2011年04月03日</a><br/>
     <Start class="green">2011年04月03日</Start><br/>
这里根据个人理解,可能不正确,请你们慎用。
    @Parameter("componentResources.elementName")
    private String elementName;
上面源码中elementName属性的值默认是componentResources.elementName,则不会产生任何标签输出。
我猜想componentResources是org.apache.tapestry5.ComponentResources属性,elementName就是org.apache.tapestry5.ComponentResources中的一个get方法,而后我看org.apache.tapestry5.ComponentResources从org.apache.tapestry5.ComponentResourcesCommon集成了一个String getElementName(String defaultElementName);的方法,这个时候我就想出了
<t:output value="now" format="formatDate" class="green" elementName="componentResources.getElementName('a')"/><br/>
的测试代码,就产生了一个a标签的输出,因此我又增长了一个
<t:output value="now" format="formatDate" class="green" elementName="componentResources.pageName"/><br/>
的测试输出,这个时候我看到格式化字符串被一个<Start>的标签包围,因此我才猜想是这样的。
可是这里有一点不明白,默认的绑定表达式前缀是属性,那么componentResources.elementName应该是Output中的一个属性而已,可是只有一个同类型的resources属性,难道是根据类型来判断的?就想@SSO那样?但愿有知道的高手给点解释。api

<t:outputraw/>就是不过滤value属性值中的特殊字符,直接输出到HTML中(和<t:output/>中的filter属性为false一个效果),能够算是<t:output/>的一个很简化的版本。数组

相关文章
相关标签/搜索