aspose file tools*
The moose likes JSP and the fly likes c:if question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "c:if question" Watch "c:if question" New topic
Author

c:if question

John Mattos
Greenhorn

Joined: Sep 17, 2004
Posts: 5
all,

I'm having a problem with the following even though the string reportID in my jsp equals "1". the include never happens. what am I doing wrong?

<c:if test='${reportID=="1"}'>
ReportID=1!
<!-- Begin Include -->
<%@ include file="testInclude.jsp"%>
<!-- End Include -->
</c:if>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

A little more info is needed. What is reportID? And is its value really equal to the String "1"?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

P.S. The include always happens (since it's a static include), but the results are never executed if the test fails.
John Mattos
Greenhorn

Joined: Sep 17, 2004
Posts: 5
Hi there

reportID is a string variable that I get from an object living on the session. It represents the current report being viewed on this jsp, ans is a String.

JSP Code:
<%
CurrentStatusBean status = (CurrentStatusBean) session.getAttribute("statusBean");
String reportID;
reportID = status.getReportID();
%>

My first thought was that I wanted to do something like this, but all I've got is "==" am I comparing references?

if (reportID.equals("0"));{
// do some stuff
}

Do I have a scope problem? when I do the following, I get "reportID=1" in the rendered page

reportID=<%=reportID%>

[ October 26, 2004: Message edited by: John Mattos ]
[ October 26, 2004: Message edited by: John Mattos ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66



In this example reportID is a scripting variable. The EL has no access to scripting variables. The EL operates on scoped variables (aka attibutes).

Since your status bean is a scoped variable and has an appropriate accessor for property reportID (getReportID()), you can use the following:



Much simpler, no?

The purpose of the JSTL and EL is to eliminate scripting and scripting expressions from JSP pages. So never the twain shall meet.
[ October 26, 2004: Message edited by: Bear Bibeault ]
John Mattos
Greenhorn

Joined: Sep 17, 2004
Posts: 5
Ah, that totally makes sense.

does EL have access to session scoped as well? It still isn't working. Currently, I have...

reportID=<%=status.getReportID()%>
<!-- Subsets Form -->
<c:if test="${status.reportID=='1'}">
ReportID=1!
<!-- Begin Include -->
<%@ include file="testInclude.jsp"%>
<!-- End Include -->
</c:if>

Note, in the rendered HTML, I still see that "reportID=1". Do I need to put "status" in the page context?
[ October 26, 2004: Message edited by: John Mattos ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

does EL have access to session scoped as well?


Yes. The problem is that in session scope the scoped variable is named statusBean, yet you are trying to reference it as "status".

I'd advise you to remove all unnecessary scripting elements from your page. They're just confusing. If you are going to be using the EL and JSTL, there's no need to create scripting variables.
John Mattos
Greenhorn

Joined: Sep 17, 2004
Posts: 5
Thanks a lot, it now works. One more question. I'd like to do something like the following rather than hard-coding the name of my bean (statusBean) but it doesn't like it. Is embedding <%%> stuff forbidden within a c:if?

<c:if test="${<%=ApplicationConstants.CURRENT_STATUS_SESSION_KEY%>.reportID=='1'}">
<!-- Begin Include -->
<%@ include file="testInclude.jsp"%>
<!-- End Include -->
</c:if>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Is embedding <%%> stuff forbidden within a c:if?


Yes. That is one of the things you give up when moving to JSTL/EL. That bothered me a bit at first (having to hard-code variable names and form element names), but having (almost) completed a large, complex web app using entirely scriptless pages, I think it's more than a worthwhile trade-off.
[ October 26, 2004: Message edited by: Bear Bibeault ]
John Mattos
Greenhorn

Joined: Sep 17, 2004
Posts: 5
Ah, okay.

thank you very much for all your help!

John
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
I might be involved in a volunteer project soon that would be building a web application, and one of the issues is whether to use JSP/Java or another scripting language.

Bear, what do you think the advantages were, primarily? What percentage of the development time did you save, and why?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Actually, since it was my first attempt at writing scriptless pages, it probably took longer than if I had just forged ahead with the script-laden pages of olde.

However, the results are spectacular. Even fairly complex pages (paged lists of results with sortable columns for example) are butt-simple and a joy to maintain, add features to, and refactor. I have few to no regression bugs when adding features, and its all just so, well, clean.

I had thought in advance that moving to scriptless pages would have tangible benefits, but this was one of those rare cases where reality actually surpassed the expectation (or maybe I'm just a cynic and was expecting the worst).

Now that I (think I) have a good handle on how to express the data to the pages (JSP 2.0 does require a bit of rethinking of the controller/page interface -- see my JR Journal article or blog on this subject), I find page authoring to be much easier and quicker. I don't have any quantifiable metrics; that's entirely based on a qualitative feel for the process. But I'm not only finding that the pages are easier and quicker to author, I certainly spend much (much) less time debugging than I use to. And what debugging is necessary happens in the servlets/actions/pojo code where it's inifinitely easier to deal with than in a JSP.

If you choose JSP 2.0 and adhere to the scriptless pages attitude for your project (and are willing to take the learning curve hit), I think you too may be pleasantly surprised by the outcome.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: c:if question