內置組件和

第一部分:源碼
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/>的一個很簡化的版本。數組

相關文章
相關標籤/搜索