How to remove default theme from Java Faces application? - java

I've never used Java before (C# is my domain), but now I have a task that I must apply a specific style to for a tiny Java webpage. The style is from another ASP.NET application that we're rolling out and that I've helped to develop. Seems that this style is on the way to become our new company web-style. :P
The webpage is really tiny - it has only a login form and an action form with a dozen textboxes and a button. When you click the button, a file is generated and available for download.
This application has already been made by a colleague of mine, who has good experience with Java, but nearly no experience with the web. So he's no help anymore. For now the application has NO style at all. Just a bunch of textboxes.
The application is made in NetBeans and Java Faces - as far as I understand. Or maybe it's "Visual Faces". Not sure - can't make heads or tails with all these names from the Java world. Anyway, it's this ASP.NET look-alike where you have an XML page with tags like <ui:textField> and <ui:passwordField>, and then a code-behind file with all your Java code. (Actually I suspect that ASP.NET was created after this, but that's besides the point)
I've successfully converted the HTML layout to the one I need and set my own CSS/JavaScript files in the header. There's just one problem - the Faces thing is hell-bent on including its own CSS stylesheets as well. Sounds reasonable of course, but for some mind-boggling reason these stylesheets also deem it necessary to override styles on all the common tags - <body>, <div>, <span>, etc. Which is annoying, because that breaks my design. Now, I could change my own stylesheets to over-override these default Faces stylesheets, but that seems... well, not the right solution. It's like a workaround, not a "correct" solution. Why should I redefine font attributes for every tag, when it should easily cascade down from the "body" tag?
Is there any way how I can tell Faces not to include its own stylesheets? I won't be using any of their controls anyway - just textboxes and buttons. And labels, when I find them. :P
Oh, yes, and I tried to re-create it all in simple JSP (no Faces), but that failed, because the code-behind (there's some big library there) is dependent on it... This is also the reason why I can't move it to the ASP.NET I'm so familiar with.
Added: Since it seems there are many Java Faces frameworks out there, here's a snippet of code that might help:
<jsp:root version="1.2" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:ui="http://www.sun.com/web/ui">
<jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>
<f:view>
<ui:page id="page1">
<ui:html id="html1">
<ui:head id="head1">
Also I forgot to mention that there is a folder in NetBeans called "Sun Web UI Components Themes" which has three items under it - "Gray Theme", "Green Theme" and "Default Theme". I can choose one of them as the active one, and that also changes the CSS which is included by default. Unfortunately I cannot choose none.
OK, I already changed my style to override these stylesheets. Still it would be nice to hear more about what I'm dealing here with. I don't even know where I should look for documentation...
Added 2: Also, the namespace for say, TextField, is com.sun.rave.web.ui.component.

Which JSF (JavaServer Faces) library is this using? MyFaces? RichFaces? Something different?
I don't know if you've found this resource yet: JSF Tag lib.
If it's using <ui: type tags, then it's also using Facelets which is a templating system for JSF. However the tag parameter you are probably looking for is styleClass and Style.
There really isn't a default styling for standard JSF, and I think there's probably another JSF library that's being used. Under the WEB-INF directory is probably a directory called lib where the jar files are. Those should give you a clue as to what library's are being used.
RichFaces comes with its own styling, and is completely themeable. In your particular case, it's going to be more of a pain than it's worth for your situation. Other JSF library's may do the same.
Unfortunately I can only point you at places and ideas of where the problem might be. Given the vast number of choices when it comes to JSF, it's the best thing I can do.

Related

How to use Markdown with links in JSF?

I am developing an JSF web application and would like to introduce a lot of documentation to be visible directly in the web application. Technically I would like to use Markdown language and made already first experimence with.
I am currently playing around with flexmark Java library to render e. g. HTML strings from a markdown document. Also this seems to work fine. But what to do with links to other md files?
If I do have my markdown part: See also [here](Background.md)
Then this will be rendered correct to HTML with a link like: See also here.
But how should I tell my web server to react on this link and update the document part of the page with the rendered md file?
I would need to manually find such links in the generated HTML and change them to a kind of JavaScript call, telling my server to render the panel using the other md file.
Or should I create an IFrame so that within this frame, I could follow the link to e. g. a web servlet, rendering the md files to new HTML?
But this all feels a bit clumbsy to me. Am I missing a more easy solution?
Ok, no other answers, so I answer on my own.
The comment about primefaces extension with localized is interesting, but too far away from my focus and some features did not really match to my requirements.
Therefore I stayed with a pure markdown library and made the rest on my own.
With the links it was much more easy than expected! Within JavaScript you can very easily detect all links of the page (document.links), iterate over them and just set an onclick function (see here).

Using plain JSP tags in Play

My apologies if this is a stupid question, however I can't find any information about this.
I would like to use a JSP tag (not play 'tags') in my application views - specifically, the Joda Time JSP tags for formatting purposes.
I can't work out how to call these tags - in JSP I just had to import the taglib and off I went.
<%#taglib prefix="joda" uri="http://www.joda.org/joda/time/tags" %>
Have I missed something simple - how do I do this in Play view definitions? At the moment my taglib calls are rendered as HTML. I understand that Play's view stuff is based on Groovy's - I have tried finding the relevant information there too but haven't been successful.
Thanks.
Edit For what it's worth I'm starting to suspect that I'm very much misunderstanding the situation here - am I correct in my assumption that the Play templating engine has nothing to do with JSP and is an alternative rather than an extension?
Indeed, you cannot use JSP tags. The Play framework uses (by default, although there are alternative modules that can replace the rendering engine) the Groovy templating engine to create the views. It is these views that are responsible for both the templates and for the majority of tags you will create.
There are also the concepts of FastTags, which may be the easiest way for you to expose the JodaTime functionality that you find in the JSP tag library. A good example of how to build FastTags can be found here...
Can someone explain how to use FastTags
However, if all you are trying to do is to neatly format dates, then you should take a look at the JavaExtensions for date formatting. http://www.playframework.org/documentation/1.2.1/javaextensions#date. To format dates is really simple, your code should be as simple as the following (assuming your date is passed into your view as mydate.
${mydate.format('dd MMMM yyyy hh:mm:ss')}
You can even create your own JavaExtensions if the ones built-in to Play don't offer everything you need.
Finally, It is important to understand that Play is not Java EE. Play has its own stack, its own set of rules and its own set of technologies. It can be difficult at first to separate yourself from the Java EE tools, but you almost certainly find that the Play approach saves you time and effort.
Yes, you are right. Play templates are based upon Groovy. That is why the syntax is altogether very different from jsp tags. And thus, I don't think it is possible to use jsp or replace the groovy system with a Jsp sytem.

Wicket: proper caching of CSS resources

We just moved from GWT to Wicket 1.4. While very happy in general, we still have a bit of a learning curve for certain things that GWT made simple. One of those things is resource caching.
How can I configure our app's resources (CSS in particular) so that they aren't downloaded with each page request?
We're seeing some flickering on certain page elements because the styles aren't being applied immediately, presumably because the style sheets are being downloaded each time. I traced through our logs and indeed, I'm seeing requests for the stylesheets on nearly every page request.
We're referencing the CSS directly in our HTML files like so:
<link href="/css/ag.screen.css" rel="stylesheet" type="text/css">
<link href="/css/ag.base.css" rel="stylesheet" type="text/css">
I'm not sure it matters, but all our pages are mounted with IndexedParamUrlCodingStrategy, as we need them to be both bookmarkable and have particular URL structures.
I tried implementing what Stefan Fußenegger suggests in his blog (i.e. referencing the CSS via a header contribution rather than in the HTML directly), but that didn't seem to help (I tried checking the headers with "curl -I").
Is there a standard way of setting Cache-Control or Expiration headers for resources in Wicket 1.4? Is it worth moving to Wicket 1.5 for this? (I'm hesitant to do so because it's not officially released yet).
Edit: I just gave 1.5 another try, but the lack of documentation is really a roadblock at this point. IndexedParamUrlCodingStrategy and related classes are are gone, and the migration guide has almost nothing to say about it.
Edit 2: I just noticed something very odd- when I arrive at one of my pages either by directly entering the URL, or by clicking a BookmarkablePageLink, the resources referenced on that page (CSS, images, etc) seem to get cached (I see no requests for them in my logs). But if I arrive at the same page via a form submission, all of the resources are downloaded again. Why does a form submission cause resources referenced on the page to be re-downloaded?
Since you are moving to Wicket I'd recommend you to use 1.5RC4.2. Looking at the bug reports about this RC I think the next one may be 1.5.0Final.
About caching see this wiki page: https://cwiki.apache.org/WICKET/caching-in-wicket-15.html.
It is about 1.5 but it also says what are the differences against 1.4.
I had the same need as you, the need to cache resource client side the more I can.
For now I am stuck under 1.4.x and I found a solution that suits me.
I use the great library Stefan Fußenegger wrote and he made available here. Implementation is pretty straightforward, I use a StaticResourceVersionProvider in production mode. I had no issues so far (rock solid for my clients for now).
Don't hesitate if you need details on how I use this solution.
To manage the headers of non-Wicket resources you can use plain Servlet Filter that is listed before WicketFilter in your web.xml. Just check the request Url and if the file extension is .css/.js/... then just set the cache headers in the response.
About **UrlCodingStrategies in Wicket 1.5 read http://wicketinaction.com/2011/07/wicket-1-5-request-mapper/ and the next two articles. I hope it will be more clear for you.
You get fingerprints on resources out-of-the-box. The strategy for creating cache fingerprints can be modified or altered quite easily.
There's more information at the Wicket wiki page about how resource caching works in 1.5.

Vanilla JSF application with javascript?

Hey guys I have been trying different Javascript/AJAX jsf frameworks. I find most of them extremely heavy. Icefaces tends to add so much javascript just for simple things and it uses a notoriously slow javascript framework also. Primefaces is a little better since it uses jquery but I still find it kind of heavy. What if I just want to use straight vanilla jsf and add javascript on top of that.
What is the best way to go about this. I would need to be able to output javascript to the page from the backing bean. Would a servlet or restful service be good way to output javascript/html to a page?
I basically want to use basic jquery animations. Maybe do a datatable filter. Thanks for any help.
What you want is definitely supported in JSF. You can use the <h:outputScript> tags for this and additionally the <h:outputStylesheet> tag if you also need CSS.
With these tags you can include scripts per view (page), although you can also opt to include them for all pages by creating a master Facelets template and include those there.
You can also create very simple components of your own by using JSF/Facelet's composite component feature. Those components just consist out of a simple .xhtml template file and those can include the javascript libraries you need and contain your own lightweight tailor-made javascript.
See this for some examples of using the <h:outputScript> tag in JSF: http://www.mkyong.com/jsf2/resources-library-in-jsf-2-0/

Why would I use a templating engine? jsp include and jstl vs tiles, freemarker, velocity, sitemesh

I'm about to choose to way to organize my view (with spring-mvc, but that shouldn't matter much)
There are 6 options as far as I see (though they are not mutually exclusive):
Tiles
Sitemesh
Freemarker
Velocity
<jsp:include>
<%# include file="..">
Tiles and Sitemesh can be grouped; so can Freemarker and Velocity. Which one within each group to use is not a matter of this discussion, there are enough questions and discussions about it.
This is an interesting read, but can't quite convince me to use tiles.
My question is - what do these frameworks give that can't be properly done with <# include file=".."> and JSTL. Main points (some taken from the article):
Including parts of pages, like header and footer - there isn't a difference between:
<%# include file="header.jsp" %>
and
<tiles:insert page="header.jsp" />
Defining parameters in the header - like title, meta tags, etc. This is very important, especially from SEO point of view. With the templating options you can simply define a placeholder which each page should define. But so you can in jsp with JSTL, using <c:set> (in the including page) and <c:out> (in the included page)
Layout reorganization - if you want to move the breadcrumb above the menu, or the login box above another side-panel. If page inclusions (with jsp) is not well organized, you might need to change every single page in such cases. But if your layout is not overly complex, and you put the common things in header/footer, there is nothing to worry about.
Coupling between the common components and the specific content - I don't find an issue with this. If you want to reuse some fragment, move it to a page that doesn't include any header/footer, and include it wherever needed.
Efficiency - <%# include file="file.jsp" %> is more efficient than anything else, because it is compiled once. All other options are parsed/executed many times.
Complexity - all non-jsp solutions require additional xml files, additional includes, pre-processor configurations, etc. This is both a learning curve and introducing more potential points of failure. Also, it makes support and changing more tedious - you have to check a number of files/configurations in order to understand what's happening.
Placeholders - do velocity/freemarker give anything more than JSTL? In JSTL you put placeholder, and use the model (placed in request or session scope, by controllers) to fill these placeholders.
So, convince me that I should use any of the above frameworks instead of/in addition to plain JSP.
A few arguments for Velocity (I haven't used Freemarker):
Potential to re-use templates outside of a web context, such as in sending emails
Velocity's template language syntax is far simpler than JSP EL or tag libraries
Strict separation of view logic from any other sort of logic - no possible option to drop down to using scriptlet tags and doing nasty things in your templates.
Placeholders - do velocity/freemaker give anything more than JSTL? In JSTL you put placeholder, and use the model (placed in request or session scope, by controllers) to fill these placeholders.
Yes, references are really the core of VTL:
<b>Hello $username!</b>
or
#if($listFromModel.size() > 1)
You have many entries!
#end
Efficiency - <%# include file="file.jsp" %> is more efficient than anything else, because it is compiled once. All other options are parsed/executed many times.
Not so sure I agree with or understand this point. Velocity has an option to cache templates, meaning the abstract syntax tree they are parsed into will be cached rather than read from disk each time. Either way (and I don't have solid numbers for this), Velocity has always just felt fast for me.
Layout reorganization - if you want to move the breadcrumb above the menu, or the login box above another side-panel. If page inclusions (with jsp) is not well organized, you might need to change every single page in such cases. But if your layout is not overly complex, and you put the common things in header/footer, there is nothing to worry about.
The difference is, with a JSP approach, wouldn't you be re-organzing this layout in every JSP file that uses the same header/footer? Tiles and SiteMesh allow you to specify a base layout page (JSP, Velocity template, etc - both are JSP frameworks at their heart) where you can specify whatever you want and then just delegate to a "content" fragment/template for the main content. This means there would be just one file to move the header in.
The choice between jsp:include and Tiles/Sitemesh/etc is the choice between simplicity and power that developers face all the time. Sure, if you only have a few files or don't expect your layout to change very often, then just use jstl and jsp:include.
But applications have a way of growing incrementally, and it can be hard to justify the "stop new development and retrofit tiles (or some other solution) so we can fix future problems more easily", which is required if you don't use a complex solution in at the start.
If your sure your application will always remain simple, or you can set some benchmark of application complexity after which you will integrate one of the more complex solutions, then I'd recommend not using tiles/etc. Otherwise, use it from the get-go.
I'm not going to convince you to use other technologies. For all I know everyone should just stick to JSP if it works for them.
I work mainly with Spring MVC and I find JSP 2+ in combination with SiteMesh the perfect match.
SiteMesh 2/3
Provide decorators to be applied to the views mostly like inheritance works in other templating engines. Such feature is unthinkable to work without nowadays.
JSP 2+
People claiming that JSP will make it hard to avoid Java code in templates is bogus. You just shouldn't do it and with this version it's unnecessary to do so. Version 2 supports calling methods using EL which is a big advantage compared to previous versions.
With JSTL tags your code will still look like HTML so it's less awkward. Spring packs a lot of support for JSP through taglibs which is very powerful.
The taglibs are also easy to extend so customizing your own environment is a breeze.
One of the best arguments for facelets (not in your list, but I'll just mention it) opposed to using JSP is that the compilation is integrated with the interpreter instead of being delegated to the JSP compiler. This means that one of the most annoying things I had with JSF 1.1 - having to change the id-attribute on a surrounding JSF-tag when saving a change in order for the runtime engine to discover the change - went away, giving the save-in-editor, reload-in-browser cycle back, along with much better error messages.
A good view technology eliminates most and most anoying if/switch/conditional statements, simple include does not. Using a 'complex' view technology results in a 'simple' application.
You didn't provide information about specific of your applications. For example I do not use JSP just for few reasons:
It is hard to avoid using Java code in JSP templates so your break concept of pure View, and as result you will have difficulties to maintain code in several places as view and controller
JSP automatically creates JSP context which establishes a session. I may want to avoid it, however if your applications always use session it can be not a problem for you
JSP requires compilation and if target system doesn't have Java compiler, any minor tweaking will require to use other system and then redeploy
Minimal JSP engine is about 500k of bytecode plus JSTL, so it can be not suitable for embedded systems
Template engine can generate different content type of the same model, let say JSON payload, webpage, e-mail body, CSV and so on.
Non Java programmer may have difficulties to work with JSP templates, when non technical people never had difficulties to modify regular templates.
I was asking same question long time ago and ended by writing my framework (surely template engine based) which was free from all disadvantages I saw in other solutions. Needless to say it is about 100k of byte code.
I realize that this comes off as a smart ass answer, but the truth of it is, if you don't see any advantage to using templating over code in your current project, it's probably because in your current project, there isn't one.
Part of it is about scale. You might think that includes are every bit as powerful as let's say sitemesh, and that's certainly true, at least for a small number of pages (I'd say probably about 100), but if you have several thousands it starts becoming unmanageable. (So for eBay it's not necessary, for Salesforce it probably is)
Also, as has been mentioned before, freemarker and velocity are not servlet specific. you can use them for anything (mail templates, offline documentation, etc.). You do not need a Servlet container to use freemarker or velocity.
Lastly your point 5 is only partially true. It is compiled every time it is accessed if it has not been so already. This mean that whenever you change something you need to remember to delete your servlet containers "work" directory, so that it recompiles the JSP. This is unnecessary with a templating engine.
TL;DR Templaing engines were written to address some (perceived or real) shortcomings of JSP + JSTL. Whether you should use them or not depends entirely on your requirements and the scale of your project.

Categories