Creating JSP templates (views) dynamically from Database

0 views
Skip to first unread message

JavaStudent via StackOverflow

unread,
Aug 9, 2013, 4:49:20 AM8/9/13
to google-appengin...@googlegroups.com

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 - Creating content of JSP views in /WEB-INF/views dynamically from the database.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/18141348/creating-jsp-templates-views-dynamically-from-database

Costi Ciudatu via StackOverflow

unread,
Aug 9, 2013, 4:49:22 AM8/9/13
to google-appengin...@googlegroups.com

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



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/18141348/creating-jsp-templates-views-dynamically-from-database/18141969#18141969

user2310289 via StackOverflow

unread,
Aug 9, 2013, 4:49:24 AM8/9/13
to google-appengin...@googlegroups.com

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.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/18141348/creating-jsp-templates-views-dynamically-from-database/18142350#18142350

Costi Ciudatu via StackOverflow

unread,
Aug 9, 2013, 5:59:24 AM8/9/13
to google-appengin...@googlegroups.com

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 you 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 (named as the DB primary key or something) under your exploded webapp root, then RequestDispatcher.forward() to it.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/18141348/creating-jsp-templates-views-dynamically-from-database/18141969#18141969
Reply all
Reply to author
Forward
0 new messages