• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSTL query: Passing value from xml document into Java

 
lavi mendonca
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • 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 xut 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>
<xtherwise><x:set select="MASKED_ACCOUNT_NUMBER" var="account"/></xtherwise>
</x:choose>
<x:choose>
<x:when select="balance/loan_balance"><x:set select="balance/loan_balance" var="balance2"/></x:when>
<xtherwise><c:set var="balance2" value="N/A"/></xtherwise>
</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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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 xmlut 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
  • 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
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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"/>
<cut value="${balance}" /> AAA
</c:when>
<c:when test="${priceAmt == 0}" >
<fmt:formatNumber value="${priceAmt}" type="currency" currencySymbol="$" currencyCode="USD" pattern="0.00" var="balance"/>
<cut value="${balance}" /> BBB
</c:when>
<ctherwise>
<fmt:formatNumber value="${priceAmt}" type="currency" currencySymbol="$" currencyCode="USD" pattern="###,##0.00" var="balance"/>
<cut value="${balance}" /> CCC
</ctherwise>
</c:choose>

cut displays the amount without the currency symbol ($)


Thanks again for all your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!