English 中文(简体)
How can I reuse HTML/JSP within a page?
原标题:

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.

问题回答

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.

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.





相关问题
Convert typed-in Text to lowercase

I ve got an index.jsp with [snip] <% String name = request.getParameter("name"); String pass = request.getParameter("pass"); String globalname = "webeng"; String globalpass = "2009"; ...

session transfer issue from Tomcat to ASP.Net

I am using Tomcat to host JSP and using IIS 7.0 to host aspx (C# + .Net 3.5 + VSTS 2008), and I have some session transfer issue from JSP page to ASPX page. JSP page is in one domain and all other ...

Setting the default value in Struts2

I am setting the value(kind of default value) for a drop down select value from action class in a page(given below). When the page loads the value is beig displayed but the other elements of the ...

Evaluate dynamically constructed JSP at runtime

I have a requirement where in the JSP page itself is created by the user and stored in the database. When viewing results we need to render this JSP to the client, evaluating all tags inside this JSP. ...

How to Pack/Encrypt/Unpack/Decrypt a bunch of files in Java?

I m essentially trying to do the following on a Java/JSP-driven web site: User supplies a password Password is used to build a strongly-encrypted archive file (zip, or anything else) containing a ...

JSP exception - class not found (tomcat)

I m setting up an existing application on a new Tomcat 5.5 server connecting to a Postgres database (running on Debian Lenny). When I access it I get a series of stack traces with the following root ...

ArrayList to Table in JSP

I have an ArrayList and i am trying to display it in a table ..... ArrayList rows = .... ..... <table cellspacing="1" cellpadding="4" border="3"> <tr> <TH>...

热门标签