• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamic XML and JSP

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create an application that will allow the users to change text and labels in an XML file, which in turn I will pull into the JSP. The idea is that I will code the static portion of the JSP with the include, but there after if they want to change a text paragraph or field label, it can be done in the XML file and will be available to the JSP next time the JSP is run.

In my JSP I have the following code:
.
.
.

<%
String newState = null;

if (userProfile.getPreviousState().equals("createstudentresponse")){
newState = student.getState();
} else {
newState = request.getParameter(oldState);
}

%>

<table border="0" width="650">
<form method="post" name="dealerp1">
<tr><td >

<%
Paragraph[] paragraphs = webPage.getParagraphs();

for (int i = 0; i < paragraphs.length; i++) {

Column[] columns = paragraphs[i].getColumns();

out.println("<table border=\"0\" width=\"100%\"><tr>");

for (int j = 0; j < columns.length; j++) {

out.println("<td width=\"100%\">");

out.println(UtilityMethods.replaceHTMLNull(columns[j].getDescription()));
out.println(columns[j].getFixedData());
}
out.println("</td>");
}
out.println("</tr></table>");
}
%>

</td><td width="1%"></td></tr>
</form>
</table>
.
.
.

The XML which is being parsed is as follows:
<Paragraph>
<Column>
<FixedData><input type="text" size="2" name="oldState" value="<%= newState != null ? newState : "" %>">
</FixedData>
<Description>Previously titled in </Description>
</Column>
</Paragraph>

The problem is that at run time the piece of Java code in the JSP is run and it outputs the description, but the variable newState which is determined by the Java code that is being pulled in from the XML file is not resolved. Any ideas on how I can make this work?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly why do you think it should be pulling in the external XML for every request?
This is a prime example of code that should be in a servlet or a bean class. Writing extensive Java logic in JSP is just hard to read and hard to debug.
Bill
 
Tania Smuts
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will move it to a bean. This is just a prototype. Even if this is in a bean, I will still have the same problem. At runtime, the code to access the bean will be resolved, but the expression coded in the xml will not have been resolved.

I want to put the text and labels in XML so that the users can change any field descriptions or text at any time and we don't have to make code changes. Next time the JSP is run, the new labels as defined in the XML will be pulled in and displayed. At the same time, through the use of html in the XML, they can change the format/font/color of the labels etc. A really scaled down versions of a dynamic content management system. It works really well in all instances where there is straight HTML in the XML attribute. The problem comes in when we have forms with fields and the value of the field needs to be resolved at runtime. The field (and expression to resolve it) is defined in the XML, but as I said before, the bean\java code gets resolved but it doesn't resolve the expression coded in the XML.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could read the XML file into a String and put it onto the request. Then, use the JSTL XML taglibs to access the data in the XML String and print it out in your response.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread just screams ResourceBundle at me. I'm not even sure it would be appropriate in this situation, but XML seems like an overkill for the needed feature described.

If you use JSP 2.0's message Resource Bundle features, you can just define all these replacable texts in a .properties file. Then you can even internationalize it if need be.
 
Tania Smuts
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The XML string will contain '<input type="text" size="2" name="oldState" value="<%= newState != null ? newState : "" %>">' If I write that out in the response, will value="<%= newState != null ? newState : "" %>"> get resolved?
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HTML output (e.g. through out.println() ) will be resolved
If you need to output JSP code you have to evaluate that output again
See for example Eval taglib here: http://www.servletsuite.com/servlets/evaltag.htm
 
Tania Smuts
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dan. The evalbody tag is exactly what I need.
 
A tiny monkey bit me and I got tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic