Apache Tapestry CSS

Cascading Style Sheets

Most web applications delegate to Cascading Style Sheets (CSS) the stylistic details of the page – fonts, colors, margins, borders and alignment. This helps the remaining HTML to remain simple and semantic, which usually makes it easier to read and maintain.

Tapestry includes sophisticated support for CSS in the form of annotation-based linking, far-future expire headers, automatic duplicate removal, and other features provided for assets.

Default style sheet

Tapestry includes a built-in style sheet, default.css, in all HTML documents (documents that have an outer <html> element and a nested <head> element). The default.css style sheet is always ordered first ... any additional style sheets will come after. This allows you to override Tapestry's default styles with your own.

All the styles in the default style sheet are prefixed with "t-" (for Tapestry).

Adding your own CSS

A page or component (for example, a layout component) that is rendering the <head> tag can add a style sheet directly in the markup.

<head>
  <link href="/css/site.css" rel="stylesheet" type="text/css"/>
  . . .

If you want to leverage Tapestry's localization support, you may want to make use of an expansion and the "asset:" or "context:" binding prefix:

<head>
  <link href="${context:css/site.css}" rel="stylesheet" type="text/css"/>
  . . .

The "context:" prefix means that the remainder of the expansion is a path to a context asset, a resource in the web application root (src/main/webapp in your workspace). By contrast, the "asset:" prefix tells Tapestry to look in the class path. See Assets.

Using the @Import annotation

Another approach to adding a style sheet is to include an @Import annotation (starting with Tapestry 5.2) on your component class:

@Import(stylesheet="context:css/site.css")
public class MyComponent
{

}

(For Tapestry 5.0 and 5.1, use the deprecated @IncludeStyleSheet annotation instead.)

As with included JavaScript libraries, each style sheet will only be added once, regardless of the number of components that include it via the annotation.

Conditionally loading IE-only style sheets

For Tapestry 5.2 and later, if you need to load a different style sheet for Internet Explorer browsers, or for certain versions of IE browsers, you can use Tapestry's built-in support for IE conditional comments. Just add something like the following to your page or component (or layout) class:

@Environmental
private JavaScriptSupport javaScriptSupport;
     
@Inject @Path("context:layout/ie-only.css")
private Asset ieOnlyStylesheet;

// add an IE-only style sheet if browser is IE
void afterRender() {
    javaScriptSupport.importStylesheet(new StylesheetLink(ieOnlyStylesheet, new
                        StylesheetOptions(null, "IE")) );
}

The above will render something like:

<!--[if IE]>
<link type="text/css" rel="stylesheet" href="/assets/1.0-SNAPSHOT/ctx/layout/ie-only.css"></link>
<![endif]-->

Naturally, the conditional part can be any other IE conditional expression, such as "lt IE 8".

Suppressing the default style sheet

Though it should be rarely needed, you can prevent Tapestry's default style sheet from loading by overriding the configuration in your application's module (normally AppModule.java):

AppModule.java (partial)
@Contribute(MarkupRenderer.class)
public static void deactiveDefaultCSS(OrderedConfiguration<MarkupRendererFilter> configuration)
{
    configuration.override("InjectDefaultStyleheet", null);
}

Note: In Tapestry 5.3 and later, the misspelled "InjectDefaultStyleheet" is corrected to "InjectDefaultStylesheet".


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值