tags:

views:

398

answers:

5

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?

A: 

Take a look at Tiles.

JeeBee
+1  A: 

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...

Lars Westergren
+1  A: 

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.

kosoant
+1  A: 

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.

xmjx
A: 

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.

  1. 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.

  2. 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

huo73