How do I Evaluate a Statement inside of another Statement?

I have an array containing all of the columns available for a consultant and I only need to view the selected columns for

the consultant array. I could have just used 搃f?statements in my JSP for each column, but I wanted to make the JSP more

dynamic so I used an additional array of selected columns. I used a data bean and methods to add the header text and

column to be displayed to the array. I then run through the selected columns array to display the column headers,

which works great. The problem occurs when I run through the consultant array and for each element in the consultant

array, I run through the selected columns array. I cannot get the data in the consultant array to display because the statement

is embedded within the selected columns statement. The selected columns statement evaluates, but the

consultant statement will not. I have tried this with ?lt;%=statement%>? 搒tatement? and using an EL on both sides.

I have also tried to use the evaluate() method, but could not get it to work.

I know the solution has to be fairly simple, but with only 2 months of Java experience, I am at a loss.

All help is very much appreciated.

Thanx

Pam

Data Bean

publicclass SelectList{

private String header;

private String column;

Typical accessors

}

Methods to Set and Add each Element of the Array

public SelectList[] getConsultantSelect(String pbAddr_City_Main,

String pbAddr_State_Main,

String pbAddr_Zip_Main,

String pbMainAddr,

String pbMainPhone,

String pbMainFax)

{

Vector retval =new Vector();

if (pbMainAddr !=null && pbMainAddr.equalsIgnoreCase("address")){

retval.add(getSelectColumn("Physical Address","<%=currentConsult.getPhysAddress()%>"));

retval.add(getSelectColumn("Mailing Address","<%=currentConsult.getMailAddress()%>"));

}

else{

if (pbAddr_City_Main !=null && pbAddr_City_Main.equalsIgnoreCase("on")) retval.add(getSelectColumn("City","<%=currentConsult.getPhys_Addr_City()%>"));

if (pbAddr_State_Main !=null && pbAddr_State_Main.equalsIgnoreCase("on")) retval.add(getSelectColumn("State","<%=currentConsult.getPhys_Addr_State()%>"));

if (pbAddr_Zip_Main !=null && pbAddr_Zip_Main.equalsIgnoreCase("on")) retval.add(getSelectColumn("Zip","<%=currentConsult.getPhys_Addr_Zip()%>"));

}

if (pbMainPhone !=null && pbMainPhone.equalsIgnoreCase("phone")) retval.add(getSelectColumn("Phone","<%=currentConsult.getPhone()%>"));

if (pbMainFax !=null && pbMainFax.equalsIgnoreCase("fax")) retval.add(getSelectColumn("Fax","<%=currentConsult.getFax()%>"));

return (SelectList[])retval.toArray(new SelectList[0]);

}

private SelectList getSelectColumn(String pvHeader, String pvColumn)

{

SelectList curColumn =new SelectList();

curColumn.setHeader(pvHeader);

curColumn.setColumn(pvColumn);

return curColumn;

}

Code inside my JSP

<%

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

SelectList curColumn = selectColumns[i];

%>

<td><%=curColumn.getColumn()%></td>

<%

}

%>

[5096 byte] By [SnowXTCa] at [2007-10-3 1:45:13]
# 1

You can't evaluate a string as an expression in JSP. By the time you are running the page, the translation phase and conversion into java code is long finished.

However there are other solutions.

You can use JSTL and EL? Then the solution becomes a lot easier, using square brackets notation with EL.

Rather than putting the evaluation string "<%= currentConsult.getPhysAddress() %>" into your datastructure, just put the name of the attribute, ie "physAddress". What you will then get is a string being the property name you want to access.

You can then use square brackets notation with EL to access the properties.

Couple of things:

- The whole expression:

if (pbMainAddr !=null && pbMainAddr.equalsIgnoreCase("address"))

can be replaced with

if ("address".equalsIgnoreCase(pbMainAddr))

and have the same effect, without worrying about null.

- When using the toArray method, it is better to pass in an array already created with the correct number of items you need.

public SelectList[] getConsultantSelect(String pbAddr_City_Main,

String pbAddr_State_Main,

String pbAddr_Zip_Main,

String pbMainAddr,

String pbMainPhone,

String pbMainFax)

{

Vector retval = new Vector();

if ("address".equalsIgnoreCase(pbMainAddr)){

retval.add(getSelectColumn("Physical Address", physAddress"));

retval.add(getSelectColumn("Mailing Address", "mailAddress"));

}

else {

if ("on".equalsIgnoreCase(pbAddr_City_Main)

retval.add(getSelectColumn("City", "phys_Addr_City"));

if ("on".equalsIgnoreCase(pbAddr_State_Main)

retval.add(getSelectColumn("State", "phys_Addr_State"));

if ("on".equalsIgnoreCase(pbAddr_Zip_Main)

retval.add(getSelectColumn("Zip", "phys_Addr_Zip"));

}

if ("phone".equalsIgnoreCase(pbMainPhone)

retval.add(getSelectColumn("Phone", "phone"));

if ("fax".equalsIgnoreCase(pbMainFax)

retval.add(getSelectColumn("Fax", "fax"));

// when converting List to array, it is more efficient to give it the correct size array. See the javadoc.

return (SelectList[])retval.toArray(new SelectList[retval.size()]);

}

and then in your JSP, something like this:

<c:forEach var="currentConsult" items="${consultList}">

<tr>

<c:forEach var="col" items="${selectColumns}">

<td><c:out value="${currentConsult[col.column]}"/></td>

</c:forEach>

</tr>

</c:forEach>

evnafetsa at 2007-7-14 18:43:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thank you evnafets.

Believe it or not, I actually understand this and what it is doing. Your reply was awesome.

However, I have something strange going on, that is beyond me. I made all the code changes, nothing, not even the name

which is not in the selected columns list. It would not go into the forEach. Ran the debugger and it was not entering it.

I changed the code to put items first and var second, no luck. I have checked both the consultants array and the

selectedColumns array and they both have data. I had originally left the script tags and for to do the header

columns and this was working great. I changed this to a forEach and now it will not enter the forEach for the header.

I know JSF tags will put the form id in front of the id/name, does JSTL do this as well? Do I need to add an id

to the JSP and use it in front of the array names? Am I missing a library or something I need to load on my PC?

(I declared the library in my directives)

Not lost, but definitely against a brick wall!!!!

Code to display the Headers

selectColumns contains 3 elements, each has a header and column and the correct values for each.

<c:forEach items="${selectColumns}" var="col">

<td><c:out value="${col.header}"/></td>

</c:forEach>

Libraries

<%@ page contentType="text/html;charset=windows-1252"

import="java.lang.Object,

CIS.Consultant.datasource.ConsultantService,

CIS.Consultant.bean.Consultant,

CIS.Consultant.bean.SelectList"%>

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<%@ taglib uri="/web-inf/lib/CisTagLib.tld" prefix="ct"%>

SnowXTCa at 2007-7-14 18:43:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Probably the thing I missed, is that you need to put the variable "selectColumns" somewhere that JSTL can see it.

JSTL does not access scriptlet variables, only attributes in scope.

There needs to be code something like this:

<%

SelectList[] selectColumns= getConsultantSelect(.......);

request.setAttribute("selectColumns", selectColumns);

%>

That will make the list available to EL expressions.

evnafetsa at 2007-7-14 18:43:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

[nobr]SUCCESS!!!!

Well data anyway!!!

I will admit, I have a LOT LOT more to learn. Programming capabilities obviously far out way my knowledge of the language, but I will get there.

So here is my current dilema:

I had formatting in the addresses to add returns and for some reason EL converted those to real text.

Actual Output on the Screen

PO BOX 1063<br/>CORVALLIS, OR ON 97339-<br/>USA

I set nulls to a blank space so now I get

& nbsp;

instead of a blank space. (note: there is no space between the & and nbsp;, but the 4m converts it to a blank space, if I do not put the space in.)

Here is what I get when I view the source:

<td>PO BOX 1063& lt;br/& gt;CORVALLIS, ORON 97339-& lt;br/& gt;USA</td>

<td>& nbsp;</td>

(Again note: there is no space after the &, but the 4m converts it, if I do not put the space in.)

How do I get my JSP to not convert this stuff?

Code to create the Physical or Mailing Addressif (getPhys_Addr_Line_1() != null ) physicalAddress += getPhys_Addr_Line_1() + "<br/>";

if (getPhys_Addr_Line_2() != null ) physicalAddress += getPhys_Addr_Line_2() + "<br/>";

if (getPhys_Addr_City() != null ) physicalAddress += getPhys_Addr_City();

if (getPhys_Addr_City() != null && getPhys_Addr_State() != null) physicalAddress += ", ";

if (getPhys_Addr_State() != null ) physicalAddress += getPhys_Addr_State();

if (getPhys_Addr_Zip() != null ) physicalAddress += "" + getPhys_Addr_Zip();

if (getPhys_Addr_Country() !=null ) physicalAddress += "<br/>" + getPhys_Addr_Country();

if (physicalAddress == "") physicalAddress = "& nbsp;";

THANX AGAIN FOR ALL YOU HELP

Pam[/nobr]

SnowXTCa at 2007-7-14 18:43:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

<c:out value="xxxxxxx" escapeXml="false"/>

That will stop the c:out tag from escaping the html characters for you.

WARNING: this will stop the c:out tag from escaping the html characters for you. So if they type an address with a special character in, it will not be escaped, and could even create an error on your page. You have to trust the address data to not contain html elements, which is always a potential source of errors.

evnafetsa at 2007-7-14 18:43:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
I remember reading that too. Just so over-stressed with work and doctors, hospitals, and procedures these days it is very hard to see straight, let alone think straight.I really do appreciate the help. It has been awesome.Pam
SnowXTCa at 2007-7-14 18:43:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...