• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

c:set problem

 
Suman Sharma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
In the following code, <cut value = "${authorname2}" /> line is not printing the value of authorname2. I want to check if authorname2 has a value or not. I tried alert("authorname2 is: " + authorname2); also. It didnot work. I am not sure if c:set is assigning the value correctly or no. If not, how it can be fixed. Can anyone please help me?
Thank you.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Suman Sharma:
I tried alert("authorname2 is: " + authorname2);

How would JavaScript know anything about the JSP scoped variable?

Are you sure that your references to the authorsbean properties are correct? They look suspiciously non-bean-like. You should be using standard bean notation.
[ October 18, 2008: Message edited by: Bear Bibeault ]
 
Suman Sharma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the name authorbean to authorbean2. I was using the same variable name twice that I used at some other place in this page. So c:set is working fine now.
But it is now completely ignoring the line <c:if test = "${chosen2 == $authorname2}" >
It is supposed to populate the second box with the names of books written by a particular author that is selected in the first box. But the second box is showing all the books regardless of which author name is selected in the first box. It looks like something is wrong in c:if line. Can you please help me?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are mixing client-side JavaScript and server-side JSTL and expecting it to work. It can't. All of the JSP, including the JSTL tags are evaluated on the server long before the page is even sent to the browser for the JavaScript to execute.. So how could they possibly interact?

Please read this article to understand the life-cycle of JSP pages.
 
Salah Lejmi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Suman Sharma:
I changed the name authorbean to authorbean2. I was using the same variable name twice that I used at some other place in this page. So c:set is working fine now.
But it is now completely ignoring the line <c:if test = "${chosen2 == $authorname2}" >
It is supposed to populate the second box with the names of books written by a particular author that is selected in the first box. But the second box is showing all the books regardless of which author name is selected in the first box. It looks like something is wrong in c:if line. Can you please help me?


<c:if test = "${chosen2 == $authorname2}" > this test is done server-side where chosen2 is no attribute in the application.
But you can solve the problem:
var js_authorname2='${authorname2}';
and make the test in javascript:
if (chosen2 == js_authorname2) {

// anything you want to do in javascript;
// you can surely use any JSTL expressions

}

P.S.
<cut value="${authorname2}" /> : this is not good at this place it will break your javascript code
--> instead of it you can write: alert('${authorname2});


<c:if test = "${chosen2 == $authorname2}" >
this has wrong syntax
--> <c:if test = "${chosen2 == authorname2}" >
if ever used: chosen2 and authorname2 must both be attibutes in any scope: page, request, session or application
But like i said this is not the right way --> use javascript instead.
[ October 19, 2008: Message edited by: Salah Lejmi ]
 
Suman Sharma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is working fine. Thanks so much for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic