Templates in a Spring MVC web application - java

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

Related

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

Java HTML View Engine with Master Templates and Sub Views

I'm desperately looking for a Java HTML view engine that fulfills three main requirements:
Support for master template pages.
HTML templates can be used as subviews within other templates.
HTML templates do not have to be backed by config files or Java classes.
It's for a Java web application that mainly consists of read-only page and a few pages with forms. Most likely I'll use it in combination with an MVC framework.
Master Template Pages
The main structure of the HTML should be defined by a master HTML page. The different pages just provide the core content that's put into the master page to create the final page. This is more than just the inclusion of header and footer.
Subviews
A page should be able to use other HTML pages/templates as subviews within its own content. It should be able to pass at least one parameter to provide the data that the subview needs to display. Furthermore, it should be possible to recursively use further subviews within a subview. Again, this goes beyond a simple include mechanism.
No Backing
HTML templates should consist of a single file that basically is an HTML or XML page where certain parts will be substituted based on the provided data. They shouldn't need any additional (per template) config files. And they shouldn't have the need to implement any Java classes for backing.
I've already had a look at many Java template engine. But neither of them seems to meet these requirements. (In the .NET world, ASP.NET MVC with the Razor view engine would be a perfect fit though.)
Update:
So far, I've looked at the following engines (please let me know if I've overlooked a way to achieve my requirements with one of these engines):
JSP: Has just a very basic inclusion mechanism without master templates or proper scoping for subviews etc.
Velocity: Has a slightly advanced inclusion mechanism, but no master pages.
FreeMarker: Include mechanism, no master pages.
Tapestry: Good component based subviews as well as a templating mechanism. However, it doesn't seem to be easily possible to use template engine part without the rest of the framework, which is too page-centric to be combined with an MVC framework.
Tiles: Requires two jsp pages per final page. The two layers (tiles and jsp) makes it too complex, in particular for subviews.
Update (2):
I've changed some term: view engine instead of template engine, subviews instead of components.
You haven't really looked at template engines. JSP, Velocity and FreeMarker are not template engines (with your meaning of a template engine). They're languages allowing to generate markup dynamically, and get the data to display in the generated HTML from Java objects. Tapestry is a complete web application framework, based on components.
If you're using JSP to generate the HTML pages, you can use a template engine on top of JSP like Tiles or SIteMesh, which will handle the templating, and thus allow to have one JSP per "component" of a full page. JSP should generaly not be used without an web MVC framework like Stripes, Spring MVC or Struts2. All of these either have their own templating support, and/or support integrating another one like SiteMesh or Tiles.

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/

An HTML template engine for Java like Genshi or Zope's TAL

I need to work with a Web Designer so...
Can you suggest me any?
Thanks in advance
FYI:
Zope's TAL
The Template Attribute Language (TAL) is a templating language used to generate dynamic HTML and XML pages. Its main goal is to simplify the collaboration between programmers and designers. This is achieved by embedding TAL statements inside valid HTML(or XML, respectively) tags which can then be worked on using common design tools.
Genshi
Genshi is a Python library that provides an integrated set of components for parsing, generating, and processing HTML, XML or other textual content for output generation on the web.
The main feature is a template language that is smart about markup: unlike conventional template languages that only deal with bytes and (if you're lucky) characters, Genshi knows the difference between tags, attributes, and actual text nodes, and uses that knowledge to your advantage.
NOTE:
The key feature here is:
This is achieved by embedding TAL (or Genshi) statements inside valid HTML(or XML, respectively) tags which can then be worked on using common design tools
Example:
Genshi:
<div>
<b py:if="foo">${bar}</b>
</div>
TAL:
<h1 tal:content="string:Some altogether different headline">
The headline visible to the designer
</h1>
You can also check Apache Tapestry. It is more of a web framework than a HTML templating engine but has similar functionality that you need.
Checkout FreeMarker or Apache Velocity.
FreeMarker is more advanced but a little complex as compared to Apache Velocity.
Just adding another framework similar to Tapestry...
wicket, which has the feature that I asked for, too.
For a comparison between Tapestry and Wicket see this article.
I just found out about Thymeleaf (http://thymeleaf.org). Quoting DuyHai's Java Blog article Spring MVC part III: ThymeLeaf integration:
Unlike many Java templating engines of its kind, Thymeleaf is a full HTML engine, meaning that the template file itself is a HTML file that can be rendered by any Web browser.
As fas as I know, the only other attribute-based template engine out there is TAL (Template Attribute Language) using Python language.
The fact that the template itself can be displayed in a web browser is an important feature. Indeed while developing an Web page, people first start designing the static part of the GUI (css, color, layout) before focusing on the dynamic part (data injection, conditional rendering)…With Thymeleaf…we do not need to add any new tag, just new attributes.

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