开发者

GWT, Struts2, JSF - Manage CSS - provide only if necessary

I'd like to know if its possible, by using GWT, Struts2 of JSF, provide to the client the resources (such as .css file) only if a page need it in that moment.

I don't mean include the CSS on the page loaded in that moment, but if exist a mechanism that manages this开发者_开发百科 process automatically.


In JSF2 that's possible by using <h:outputStylesheet> or <h:outputScipt> anywhere in your template or composite component. JSF will take care about putting it in the generated HTML <head>.

E.g.

<ui:composition
    xmlns:h="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
>
    <h:outputStylesheet library="css" name="specific.css" />
    <h:outputScript library="js" name="specific.js" />
    ...
</ui:composition>

in combination with those files in /resources folder of the webcontent.

  • /resources/css/specific.css
  • /resources/js/specific.js

Also, in a JSF2 UIComponent you can force loading of specific resources using the @ResourceDependency or @ResourceDependencies annotation.

@ResourceDependency(library="css", name="specific.css")
public class CustomComponentWithCSS extends UIComponentBase {
    // ...
}

@ResourceDependencies({
    @ResourceDependency(library="css", name="specific.css"),
    @ResourceDependency(library="js", name="specific.js")
})
public class CustomComponentWithCSSandJS extends UIComponentBase {
    // ...
}

So whenever you use <custom:componentWithCSS> in your view code, JSF2 will include the annotated resources in the generated HTML <head>.


It seems to have been resolved in the answer by BalusC that automatic inclusion of resources as you're looking for is not possible. I agree. Thinking about it I could imagine a way to do this but it would so horribly complicate and bloat the resulting page and require a tonne of overhead ajax as to make it unreasonable.

Then there would be the matter of configuration to explain when page x asks for css class "hot" that it needs "hot" from "main.css" and not "company.css" in other words... the amount of configuration would probably equal the amount of work to manage it yourself.

Now with that out of the way if you're using Struts2 and like a composition based strategy Tiles2 is straight forward way to go.

Basically you make templates and then there is a separate definition file the definition states what resources to move into the template. So you can move in certain css files, script files, jsps, even compose multiple tempates. Then you inherit that definition for subsequent definitions and are able to override the parts that change and extend the definition if needed (similar to what OOP inheritance gets you). The name tiles comes from the idea that your page is thought of as a number of ceramic tiles which are positioned to produce a page, then though inheritance you can say a particular set of pages will be based on a certain layout. When you use tiles inheritance it can feel like you getting magical resource management... It makes things easier but it's straight forward composition.

Tiles is naturally geared towards composition but it also supports decoration. Many swear by sitemesh and so prefer primarily a decoration approach. You'll need to look into these two layout paradigms and see what makes the most sense to you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜