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.
The page redirect is used to move the redirect response to another resource of the web page. Basically we can call the redirect pages using sendRedirect() method in the jsp on client-side request can be used within the server or outside of the server.
In this JSP tags are used to insert JAVA code into HTML pages. It is an advanced version of Servlet Technology. It is a Web based technology helps us to create dynamic and platform independent web pages. In this, Java code can be inserted in HTML/ XML pages or both.
Include action tag is used for including another resource to the current JSP page. The included resource can be a static page in HTML, JSP page or Servlet. We can also pass parameters and their values to the resource which we are including.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With