tags:

views:

147

answers:

4

I'm new to JSP, and I'm trying to reduce a massive amount of cut-and-pasted code.
On each page in the project, there are around 25 lines of mixed JSP,Struts tags,JSTL tags, and HTML, which have been cut and pasted at various points in the page. These ~25 lines of re-used code are not even remotely similar from page to page (and there ~250 pages), but exactly the same within each page. Ultimately this (business logic) code should be moved out of the View, but doing so would be a larger project than my schedule permits at the moment, so I'm wondering if there is a simple way to re-use Mixed Tags+JSP within a page, as a temporary fix so that the code can be refactored in stages as time permits.

For clarity, I am looking for a way to encapsulate code without creating a new file (/local to page scope) - i.e. it should be defined in the same page it is called from.

Some have suggested that this can be done with Tiles - if that is the case, please show me how.

+2  A: 

you could create one include file and jsp:include it wherever it's needed. the variable parts could be done with jsp conditionals, jstl EL attributes, or struts EL.

i wouldn't necessarily want to re-code existing pages tho since they're already working.

jspcal
Thanks for the suggestion. I've used that approach as a temporary fix previously, but sadly in this case it would either significantly increase the number of jsp pages in the project or create a tangled mess of conditionals.The problem is that the pages aren't really working - I've been tasked with making the site cross browser compatible. I'm trying to cut down on debugging time by reducing the copy-pasted code while rewriting only what needs to be rewritten for now.
T.R.
+2  A: 

Take a look at Apache tiles. Since you are working with Struts, I'm surprised you haven't found it already. It is basically a templating engine and I think fits your requirements.

The already suggested <jsp:include> can be used with <jsp:param> in order to pass variables. Like

<jsp:include file="includedFile.jsp">
    <jsp:param name="username" value="jsmith" />
</jsp:include>

Actually, if only wanting to include 1 file with the common code, I'd recommend the simplicity of <jsp:include> over the power of Tiles.

Bozho
I actually just started using tiles to clean up some of the code a few weeks ago, though I'm not as familiar with it as I really should be. Is it possible to create a tile and insert it within the same file? Since the code isn't really structurally or logically similar between files, I'm hoping for something that won't require creating separate files for the encapsulation, so I can refactor in stages.
T.R.
+1  A: 

Struts Tiles are the best to be used in Struts for templating a particular page so that the part which is common to all pages need not be coded everytime. Refer http://struts.apache.org/1.x/struts-tiles/.

A more simpler approach very specific to JSP is jsp:include as discussed above.

Just for information there is a third approach which is the approach of include directive

Difference between jsp:include and include directive is as follows

One should use the include directive (< @ include file relativeURL >) only if 1) if the file includes static text 2) if the file is rarely changed (the JSP engine may not recompile the JSP if this type of included file is modified) 3) if you have a common code snippet that you can reuse across multiple pages (e.g. headers and footers)

One should use the jsp:include only if 1) The content of the included JSP is dynamic and can change at runtime 2) to select which content to render at runtime (because the page and src attributes can take runtime expressions)

I hope this will help you to decide.

Kalpak
A: 

Having not found a reasonable language or framework structure that seems to fit my requirements, I compromised. I separated out the business logic and used it to fill out a data structure. I then had the HTML reference this data structure.

Not at all an optimal solution, as the copy-and-pasted HTML still appears multiple times throughout the file, but at least the business logic is factored out, and no longer copy-and-pasted all over the page. Without a way to fully eliminate the copy-pasted code, this at least minimizes it.

T.R.