aspose file tools*
The moose likes JSP and the fly likes jsp inside jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp inside jsp" Watch "jsp inside jsp" New topic
Author

jsp inside jsp

Erik Skov
Greenhorn

Joined: Apr 03, 2012
Posts: 3
Hi,

I am looking for a way to have jsp code which is stored as a string in my database be evaluated and the result be displayed. The best I seem to be able to do is display the JSP itself.
Below we have a for loop which is building the table data elements for 1 row of my table.

<% for (int x=0; x < displayColumnArray.size(); x++) { %>
<td>
<%=TableColumnDisplay.getColumnJsp(displayColumnArray.get(x).getArrayId(0))%>
</td>
<% } %>

I know I am asking the compiler to call the function and then process what was returned. Maybe it can't handle that.
I have tried passing this as an arg to a separate jsp page, but to no avail. in fact, that adds some additional characters around the jsp.

<% for (int x=0; x < displayColumnArray.size(); x++) { %>
<jsp:include page="./tableColumnDisplay.jsp">
<jsp:param name="jspTdValue" value="<%=TableColumnDisplay.getColumnJsp(displayColumnArray.get(x).getArrayId(0))%>" />
</jsp:include>
<% } %>

tableColumnDisplay.jsp
<td>
${param.jspTdValue}
</td>

I guess what I am asking is, is there is a way to say f(g(x)) which would cause the jsp to be evaluated and then evaluated again?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18114
    
    8

Erik Skov wrote:I am looking for a way to have jsp code which is stored as a string in my database be evaluated and the result be displayed.


Ouch. Seriously? You put JSP code into a database with the plan of executing it? Why?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

It also looks like you are mixing scriptlets and the EL in a JSP? The only thing worse than using scriptlets (poor practice since 2002) is mixing it with the EL and JSTL.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Erik Skov
Greenhorn

Joined: Apr 03, 2012
Posts: 3
The plan is to build the column list for this table on the fly. I am supporting multiple departments with the same data, but they would like to see different columns in different orders. I put code related to displaying each column into a table in the database so I could easily add, subtract, and change the order of the columns by doing database updates instead of code changes in the jsp itself.

Table layout: id, column_header_text, display_jsp, dept1_order, dept2_order, dept3_order, dept4_order.

The display_jsp is basically if-true-false, "( value != null, value, space)", so that nulls do not appear in the chart, but in some cases "true" calls a java method and passes the value to it for a returned value. All of these work fine if I just put them directly on the jsp page.

The actual data to be displayed is in an ArrayList for each row returned from the database.

As for the scriplets vs EL, I am at a new job and this is the way the code is styled. HTML with <%...%> mixed in for control statements (if, for) and calling java methods to return values. I am also new to jsp, although I have used java and have used PHP mixed with HTML before. What would you recommend / what is the current standard ? can you point me to a page / tutorial ?
Erik Skov
Greenhorn

Joined: Apr 03, 2012
Posts: 3
I might be able to rework the code and data so I get this:

<% for (int x=0; x < displayColumnArray.size(); x++) { %>
<td>
<% tdColumn = TableColumnDisplay.getColumnJsp(displayColumnArray.get(x).getArrayId(0));%>
<% tdTest = (tdColumn != null);%>
<% tdTrue = process(tdColumn);%>
<%=(tdTest ? tdTrue : strSpace)%>
</td>
<% } %>
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18114
    
    8

Erik Skov wrote:The display_jsp is basically if-true-false, "( value != null, value, space)", so that nulls do not appear in the chart, but in some cases "true" calls a java method and passes the value to it for a returned value. All of these work fine if I just put them directly on the jsp page.


It sounds like this is a job for a taglib... or just using JSTL and EL, which automatically render null as the empty string. Also the JSTL <c:out> tag has a "default" attribute which might help.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
Having JSP code stored in a database to be compiled/evaluated is difficult.

From your description the actual jsp code in the database doesn't change that much - its just inclusion and ordering that is dynamic?

I would definitely go with tag files.
Each "column definition" gets its own tag file.

The table currently storing this code gets replaced with a reference to a tag. So instead of the actual code in the database, you tell it the custom tag to display.

Your process logic would then be a huge switch statement if "code1" then display "tag1" if "code2" then display "tag2".

- you can test each tag independently of the database. Storing JSP code in a database is difficult to maintain, and easy to introduce bugs in
- defining a new column is creating a new tag file and adding it to your application. Not quite as dynamic as your current solution, but I how often do you define new columns anyway? I think the trade-off is more than worth it.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp inside jsp
 
Similar Threads
Selecting a single row in a table, then saving that index in the struts form
path of jsp is repeated and i get error jsp not found
Take the quiz: Which sci-fi crew would you fit best in?
Do we have to configure web.xml for using jstl tags
ComboBox in jsp