• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

JSTL query: Passing value from xml document into Java

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Is there a way to pass the value from xml document using JSTL tags to a Java class

In my application, i have an xml document which is saved in the request object in the Servlet Controller. In my jsp, i would like to read the value from some xml nodes and pass it to my java class. But i am unable to do so.
If i do an x:out it works, but i need to pass the value of the xml node to the Java method.

xml snippet:
<ACCOUNT>
<ACCOUNT_ID>12345</ACCOUNT_ID>
<ACCOUNT_NICKNAME>testAcc</ACCOUNT_NICKNAME>
<balance>
<loan_balance>1000</loan_balance>
</balance>
</ACCOUNT>

<ACCOUNT>
<ACCOUNT_ID>45678</ACCOUNT_ID>
<MASKED_ACCOUNT_NUMBER>xxxx5678</MASKED_ACCOUNT_NUMBER>
<balance>
<loan_balance>2000</loan_balance>
</balance>
</ACCOUNT>

The code is below.


<x:set select="ACCOUNT_ID" var="accountId"/>
<x:choose>
<x:when select="ACCOUNT_NICKNAME"><x:set select="ACCOUNT_NICKNAME" var="account"/></x:when>
<x:otherwise><x:set select="MASKED_ACCOUNT_NUMBER" var="account"/></x:otherwise>
</x:choose>
<x:choose>
<x:when select="balance/loan_balance"><x:set select="balance/loan_balance" var="balance2"/></x:when>
<x:otherwise><c:set var="balance2" value="N/A"/></x:otherwise>
</x:choose>

Now i want to pass the value contained in the variables - accountId, account and balance2 to a java method

<% util.setValues(accountId, account, balance2) where util is defined using jsp:useBean tag

I also tried using (String)pageContext.getAttribute("accountId") to pass the value, but it does not work. I also tried adding /text in the select clause, but when i print it (say account_id) on the screen, it displays [[#text: 12345]]

I would greatly appreciate it if anybody could let me know where i am wrong and what is the correct way of achieving this.

Thanks in advance.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why aren't you doing it in the servlet rather than in a JSP?
 
lavi mendonca
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

This is the requirement and the approach taken by the management. Also, the project is using JSTL for the presentation. The jsp-jstl code also reads other element nodes from the xml for displaying values on the web page, so it is necessary for us to access the xml document (stored in the request scope) here... As part of this, i need to pass some values from the xml document to a utility method that does some processing and returns the processed data.

Could you please suggest the right way of:
1. Using JSTL, reading the xml value from a particular node, setting it in some variable and then passing it to the Java utility method using the example xml i put in my last post.

Thanks again.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best way to pass information from the JSP to Java code would be to define a custom tag.
 
lavi mendonca
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

Thank you for your response.

Just so that i can be sure, using the approach of having JSTL tags to read xml document (object stored in request scope), i.e value of a element node and then pass that value to a Java method is not technically possible - Is that correct ??

I have another question - when i use xml:set tag and assign the node using a var="val" attribute, i have the node with me. How is it that xml:out prints the value of var (which is val) on the screen/ web page, but when i do a pageContext.getAttribute("val"), i get the node object and not the actual value ?
Isn't this a limitation of JSTL ??

Thanks again.
 
lavi mendonca
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear -

Another place that i needed this functionality was to format a number (e.g amount) after reading the value from the xml document. Say the value of amount on the xml node element is 3000. But on the web page (browser), it should display as 3,000.00

How can i do this in JSP after using the JSTL xml tag to read the value ?

Looking forward to hearing from you.

Thanks.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you have the value from the XML doc using the JSTL and El mechanisms, you can use it like any other value in the JSTL fmt tags for formatting.
 
lavi mendonca
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

I was able to read the value from the xml document and set it in a variable to be used in Java.

<x:set var="price" select="string($customer/@id)"/>

The value of price now contains the actual value rather than the node.

However, i am still facing an issue with fmt:formatNumber jstl tag. I am not able to get the currency symbol. It just contains the amount/ currency value. I tried using currencySymbol as $ as well as currencyCode as USD, but it did not work. Can you please let me know my mistake ?

The code is:

<c:set value="${price}" var="priceAmt"/>
<c:choose>
<c:when test="${priceAmt> 0}" >
<fmt:formatNumber value="${priceAmt}" type="currency" currencySymbol="$" currencyCode="USD" pattern="###,##0.00" var="balance"/>
<c:out value="${balance}" /> AAA
</c:when>
<c:when test="${priceAmt == 0}" >
<fmt:formatNumber value="${priceAmt}" type="currency" currencySymbol="$" currencyCode="USD" pattern="0.00" var="balance"/>
<c:out value="${balance}" /> BBB
</c:when>
<c:otherwise>
<fmt:formatNumber value="${priceAmt}" type="currency" currencySymbol="$" currencyCode="USD" pattern="###,##0.00" var="balance"/>
<c:out value="${balance}" /> CCC
</c:otherwise>
</c:choose>

c:out displays the amount without the currency symbol ($)


Thanks again for all your help.
 
I was born with webbed fish toes. This tiny ad is my only friend:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic