lavi mendonca

Ranch Hand
+ Follow
since Apr 24, 2002
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by lavi mendonca

Thank you Sebastian Janisch and Campbell Ritchie for your reply.

I checked all the implementations - ArrayList, LinkedList and Vector.. All of them permit nulls.

Does this mean that this point in time, all the available list implementations permit nulls ?


Thanks.
15 years ago
Hi,

I have read on the java docs website that "A NullPointerException is thrown is an attempt is made to store a null object and null elements are not allowed in the list"

So, how do I know upfront if null elements are allowed in the list ??


Thanks.
15 years ago
I am facing an issue with fmt:formatNumber jstl tag. I am not able to display the currency symbol. It just contains the amount/ currency value. I tried using currencySymbol ($) as well as currencyCode as USD, but it did not work. Can anyone let me know what is wrong with the code?

The code is:


If the value of price is 2000, c:out displays the amount 2,000 without the currency symbol ($)

Any help is greatly appreciated.

Thanks.
16 years ago
JSP
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.
16 years ago
JSP
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.
16 years ago
JSP
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.
16 years ago
JSP
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.
16 years ago
JSP
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.
16 years ago
JSP
I figured out the mistake.

Should be using <x:out select="$var"/>
16 years ago
JSP
Hi,

I am reading the contents of an xml file in my jsp through the jstl xml tags. How do i retrieve the data and set it into another variable, so that i can use it to either output on the browser or set in the html input tag.

I tried the below, but it does not work. The xml document is in the memory in request scope (xmlDoc)

<x:choose>
<x:when select="$xmlDoc/document/validation/flag[text()='N']">
<x:set var="name" select="$xmlDoc/document/names/firstName"/>
</x:when>

<x:otherwise>
<x:set var="name" select="$xmlDoc/document/names/lastName"/>
</x:otherwise>
</x:choose>
<b><c:out value="${name}"/></b>
<input type="hidden" name="name-text" value="${name}"/>

Assume that the condition is not satisfied and it goes into the 'otherwise' loop. I verified this by putting a c: out in both the 'when' and 'otherwise' clause and 'otherwise' got executed. c:out for name displays question: null
The same value gets displayed for the value of the input tag.

If I change the select to $xmlDoc/document/names/firstName/text(), then c:out displays #text: ABC (i.e the value present for the element node question.

Could someone please let me know what is wrong and how do i correct it ?
I do not want to use x:out tag and input html tag in both 'when' and 'otherwise' loop.

Thanks.
16 years ago
JSP
The web app root - poc
This is under webapps folder of tomcat.

under poc folder, I have WEB-INF. Under WEB-INF, I have jsp, classes, lib folders. tiles-defs.xml (As tiles are also used) and web.xml are in WEB-INF. Under jsp folder, tiles\mainLayout contains the jsp for the actual layout. It defines the header, body and footer as below:

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<title> Tiles </title>
<body>
<div id="layoutWrapper">
<%--Header starts here --%>
<tiles:insertAttribute name="header"/>

<%--Rigth Content starts here --%>
<tiles:insertAttribute name="body"/>

<%--Footer starts here --%>
<tiles:insertAttribute name="footer"/>

</div>
</body>
</html>

Now, the jsps for the header, body and footer are in another folder - under the jsp folder i.e tiles\otherLayout. The footer jsp which contains the code for reading stock.xml is present here. This folder also contains stock.xml. Thus the code in footer.jsp is:

<div id="footerContentDiv">
<c:import url="stocks.xml" var="xmldoc"/>
<x:parse xml="${xmldoc}" var="output"/>

<p>
<table border="2" width="50%">
<tr>
<th>Stock Symbol</th>
<th>Company Name</th>
<th>Price</th>
</tr>


<x:forEach select="$output/portfolio/stock" var="item">
<tr>
<td><x:out select="symbol"/></td>
<td><x:out select="name"/></td>
<td><x:out select="price"/></td>
</tr>
</x:forEach>
</table>

<br/>
</div>

Hope this information is helpful. Please let me know if you need any other info.

I tried another approach without using tiles. Pure JSP, JSTL and this time, the c:import and x:parse xml worked and it was able to read the xml file (jsp containing the jstl code and xml document were in the same folder).

Thanks.
16 years ago
JSP
Hi Bear,

I used hardcoding just to check if it finds the file, but i also tried the context relative URL like below, but it wasn't able to read it. On the browser, no content is displayed.

<c:import url="stocks.xml" var="xmldoc"/>
<x:parse xml="${xmldoc}" var="output"/>
<x:out select="$output/mainEle/childEle/FirstName/text()"/>

where stocks.xml is currently present in the same location as the jsp containing the above text.

Also, if i do view the source from the browser (right click - view source), then it displays the jstl code along with html code as below. This shows that the jstl code is not intepreted.

<html>
<body>
<c:import url="stocks.xml" var="xmldoc"/>
<x:parse xml="" var="output"/>
<x:out select="$output/mainEle/childEle/FirstName/text()"/>
</body>
</html>

I am not able to figure out what is wrong here. As i mentioned before, i am able to read data from the xml document stored as a Reader in the request scope.

Thanks again.
16 years ago
JSP
Hi Bear Bibeault,

I tried using c:import (please see below), but it didn't work. I also used relative url in the c:import, even then it did not work. Right now, i am deploying this on my local machine using Tomcat, but we would be hosting this on a proper web server. Where am i going wrong in the syntax ?

<c:import
url="C:/usr/local/tomcat/webapps/poc/WEB-INF/jsp/tiles/layouts/stocks.xml" var="xmldoc"/>

<x:parse xml="${xmldoc}" var="output"/>

<x:out select="$output/mainEle/childEle/FirstName/text()"/>

Thanks again for your inputs and help.
16 years ago
JSP
Hi Bear Bibeault ,

I went through the JSTL specs, and I am a bit confused. I was able to read the content of the xml document which was present in the request scope (say xmlObj attribute, set by the business logic Java code).

<x:out select="$xmlObj/mainElement/childElement/FirstName/text()"/>

But at the same time, I also need a way of reading data from the xml which is not a String or Reader. This xml file may exist physically on the filesystem/ webserver where the jsp/ jstl code also resides.

Can you please let me know if the current c:import, x:parse JSTL tags support this ? If so, can you please give me some examples ?

Thanks again for your inputs and help.
16 years ago
JSP
Hi Paul,

Thank you for your inputs. XSLT is not an option. This is a usual web application and instead of retreiving data from the database etc, the application needs to display it from the xml document which is a stream. And the technology on the presentation side has to be JSP (JSTL).

Bear - I have a few questions
1. The JSTL XML library has a tag to import the xml document. Is it necessary that this xml file should exist as a physical file in the file system ?? Can we configure it to read the xml document as a inputstream ?

2. Also, on googlin i found there is Sun's JSTL and Jakarta's Standard Taglibs. Therefore, which is the actual one that we should be using in our web application ?

Thanks again.
16 years ago
JSP