Vanilla JSF application with javascript? - java

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/

Related

Safe API to transform HTML (Java)

I'd like to take a web page and add some tags to its head. Specifically, a CSS link and a JavaScript link. I need to do this programatically for a wide variety of web pages. Now, I could hack this out with a regex or two, but I'd like to use something more robust.
What's a good way to inject or transform HTML? I'm using Scala, but anything Java or JVM will work.
You can use jsoup.
An example for modifying content in html is here

Templates in a Spring MVC web application

I have a lot of common areas in my web application design, for example footer, header sidebar, blocks...etc and going through all of the application JSP files to change something for example in the header is pretty hectic so I was wondering if I can make something like template files for common areas for example header template, sidebar template, footer template, then I can include such templates in any JSP inside my application?
I am using Spring MVC framework to build my application
As for templating you have the following options:
Custom JSP tags. These tags may exist as Java classes or JSP snippets and provide a basic facility to encapsulate and reuse parts of JSP pages (headers, footers, etc.). See more in this manual
Template engines like Tiles or decorators like Sitemesh. They are quite powerfull in building complex page layouts.
If you're looking for custom UI themes support please take a look at custom themes support in Spring MVC.
I have used Apache Tiles. It can be very easily integrated with Spring MVC. You can also consider using sitemesh. As you mentioned header template, footer template you can have base template for
header, footer, menu and you can integrate them into base template,
Note : You can only have jsp and not html here.
Check this link for spring with tiles.
http://www.springbyexample.org/examples/dynamic-tiles-spring-mvc-module.html
If you want to seperate Header, Footer and some other common stuff from the "real" content, then you can use Apache Tiles.
It is easy to integrate in spring. So for example Spring Roo use it.
Ideally yes, You need to create common files instead of redundant code because it might leads to many changes if u want to change a single thing. So try to use below code based on your requirement.
<%# include is a static include, <jsp:include is a dynamic include.
Another solution: As you are using Spring framework try to use spring tiles search in google for more help for example SpringByExample

nested templates using velocity

I have a small java web project which should create a few web pages.
All pages should have the same boilerplate (css, javascript, etc.) but indiviual content (i.e. the content of the main div.)
What is the usual way of implementing this kind of setup in velocity?
Currently the only way I see is rendering the boilerplate template which uses #parse to include the actual page which is given as a context parameter.
Is there an easier way?
I come from sinatra where you render the actual page and implicitly add the boilerplate around it. Do you know of any similar templating framework in Java?
In sinatra you could for example do:
get '/' do
haml :index, :layout => :master
end
to render the index page in the master layout. (Or by leaving out the layout part in the default layout.)
I'd recommend you use the VelocityLayoutServlet from the VelocityTools project: http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/view/VelocityLayoutServlet.html

Are there any drawbacks of using technology JSP(instead XHTML) in JSF-2.0?

I used to working with JSP as view technology. But with JSF-2.0, the new push is for developers to use Facelets as their templating technology. But whether this is a serious matter? Will I lose anything if i will continue to use the JSP for views in JSF? Than exactly this technology is better? Is there any fundamental differences between the work XHTML and JSP? I would be very grateful if someone could answer it! Thank you
New view scope and the inclusion of Facelets is major improvements of JSF along with support ajax and annotations. This technology is a very important and powerful property in the JSF 2.0. So I would not recommend ignore it without having a necessity.
JSP is discouraged from JSF 2.0, because Facelets provide with a much clearer and flexible mechanism for developing user interfaces than legacy JSP. Facelets are far better for several basic reasons:
Facelets tags don't need declaration in a tag library descriptor file(TLD).
Attributes in a tag are dynamic and they automatically get mapped to a properties. This is one of the main features not available in JSP is page.
Very usable page-templating feature. You define a template that defines a generic layout to all the view pages with the scope for customization.
JSP-side of the JSF specification is standing still. None of the new features that involve new tags (composite components, ajax, system events, etc…) are exposed through JSP.
In addition, Facelets are faster in execution than JSPs.
In general, JSP as view technology in JSF 2.0 is considered more trouble and difficult to maintain
If you use JSF, you should really be using Facelets instead of JSP, as JSP and JSF do not match really well. The main problem is that the JSP world does not know anything about the JSF life cycle. You can read this article for some examples. Facelets does not have these problems as it was created specifically to match JSF.
There is no choice between xhtml and jsp. JSP is a server-side templating mechanism for creating text-based responses to send to the the browser. That text response could be xhtml, html, json, xml, or any other text format. So you can use jsp to create xhtml - you don't choose between them.
Just because you changed the extension doesn't mean you're doing anything different. You're just changing what you call it - there's no particular reason to do so, and it's confusing.

Different layouts and i18n in JSP application

I have a bunch of JSP files and backend in Tomcat. I have 3 different versions of JSP with same logic inside but with different layouts. So if I change some logic I have three JSP file to fix.
What is the proper soution for such a scenario? I thought of some XML and XSLT stack: backend gives only data in XML and than for each layout I have XSLT that does some magic.
Just imagine that you need to provide stackoverflow in various languages in JSP. Logic the same but layout different.
Could you point me into some direction?
Learn about MVC (Model View Controller) and the idea that JSP should be the View part of it and should not contain any logic whatsoever. Logic belongs in a Model class.
This is usually solved by using some templating engine - you create smaller page fragments, and then you declare to the template engine that certain views should consist of these parts, put together in a certain way.
Struts tiles is the classic example in the Java world, but it is really getting old and crufty compared to more modern framworks in Java and other languages. Tapestry and Wicket are two more modern ones (haven't used them though).
For only 3 pages applying a whole web framework is probably overkill though, but if your site grows...
With plain old JSP without any kinds of fameworks:
1)
Use controllers to do the processing and only use jsp to display the data
2)
Use jsp include directives to include header, navigation, menu, footer and other necessary common/shared elements to all of those layouts.
Or/and:
Use the following in web.xml
<jsp-property-group>
<url-pattern>/customers/*</url-pattern>
<include-prelude>/shared/layout/_layout_customers_top.jsp</include-prelude>
<include-coda>/shared/layout/_layout_customers_bottom.jsp</include-coda>
</jsp-property-group>
The url pattern determines which jsps get which jsp fragments (partials in Ruby on Rails) attached to top/bottom.
Take a look at Tiles.
This is a very classical problem domain and there lots of concepts and frameworks out there that are trying to deal with this issue (MVC frameworks like Struts and JSF, SessionBeans to name but). As I suspect you're not really a Java enterprise "evangelist" I will give you 2 simple advices.
You obviously have a lot of redundant code in you're JSPs. Extract this code into "real" Java-Classes and use them on all of your JSPs. That way you will be able to modify business logic in one place and redundancy will be less of a problem.
Take a look at Cascading Style Sheets (CSS). This is the state of the art way to layout webpages. You may not even need different JSPs for different layouts, if you have well designed html + CSS.
Regards

Categories