TechExplorer
TechExplorer

Reputation: 951

Creating JSP templates (views) dynamically from Database

I am developing an application using Java and Spring MVC. As usual, stored one JSP file in /WEB-INF/view/ folder which is working as the View for all the requests.

Normally we have this JSP hard-coded that also has some codes to process the Model (tags and EL). Things are working fine till this point.

Now instead of hard-coding the JSP, I want to populate this JSP file dynamically from the database. So the user can upload and select different templates/themes/layouts to display his pages.

Here is the code to explain what I am trying to do (I know this is not the way but for illustration purpose only).

/WEB-INF/views/index.jsp

<%@ page import="com.example.domain.Template" %>
<%@ page import="com.example.dao.TemplateStore" %>

<!-- Following code is supposed to return complete JSP template from the database as uploaded by the user. -->

<%= TemplateStore.getUserTemplate("userTemplate") %>

I searched web for this topic but could not find anything.

Any help on how to accomplish this would be highly appreciated.

Thanks in advance.

IMPORTANT: I have asked this question a few days ago but marked as "off-the-topic" by some members. I am yet to understand how this question is off-the-topic - https://stackoverflow.com/questions/18026092/creating-content-of-jsp-views-in-web-inf-views-dynamically-from-the-database.

Upvotes: 4

Views: 3017

Answers (2)

Costi Ciudatu
Costi Ciudatu

Reputation: 38225

If view templates are to be dynamically fetched from a database, you shouldn't think of JSP. JSPs are compiled into servlet classes and there's little support for doing that other than the standard way (static files somewhere under your webapp root).

Therefore, just consider switching the view technology (at least for the dynamic part) to some general-purpose templating library like Velocity or Freemarker. This comes with a security bonus, since there's less one can break from within such a template than from within JSP code.

You could even support multiple view technologies (perhaps anything that Spring MVC supports out-of-the-box, except for JSP) and allow your users to choose the type of template when uploading.

Then you can write a custom view resolver which would delegate to the appropriate standard resolver (Velocity, Freemarker, XSLT, whatever...) with the user-selected template.

However, if JSP is a hard requirement, I guess one ugly workaround for JSP (which should work in any servlet container) could be to fetch content from the DB and create an actual file (like WEB-INF/templates/${primarky-key}.jsp) under your exploded webapp root, then RequestDispatcher.forward() to it.

Upvotes: 3

Scary Wombat
Scary Wombat

Reputation: 44854

You might or might not be able to do this with JSP, but you can certainly compile Java code in memory and then call it.

http://www.java2s.com/Code/Java/JDK-6/CompilingfromMemory.htm

Of course going from JSP to Servlet would just be another step.

Upvotes: 0

Related Questions