File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Checkbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Checkbox" Watch "Checkbox" New topic
Author

Checkbox

R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
Hi everybody,
I have a jsp file; in spme part of file, I created a check box like this:



I check whether the checkbox is checked or not in a Java Script function like this, and send the value of filter to my Java code:



Communicates with Java code with this method:




The submitSearch() is called only once.

Up to here everything works fine.

In some other part of jsp file, I try to go to some other page:



The ${minBudget}, ${maxBudget}, ${startResult-pageSize} values are available, but the filter value is not? This is the url I get which has no indication of filter variable?

http://localhost:8080/search/0/10000/21/

It should be something like:

http://localhost:8080/search/0/10000/21/false

The Java code method that controls this part has been modified accordingly.

Does anybody has any idea? Any help is greatly appreciated.


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Of course that's not going to work. The filter variable is a JavaScript variable. How could you possible expect to access it in the JSP before the HTML page is even sent to the browser?

Sounds like you need to read this article to understand how JSP operates and why that is impossible.

You'll need to add the filter value to the URL in JavaScript.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
Thanks for the reply. Then I'm wondering how the values of minBudget and maxBudget are available thru out the jsp file? They are also defined in the same funcion:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

R Ta wrote:Then I'm wondering how the values of minBudget and maxBudget are available thru out the jsp file?


They must be available as scoped variables set up for the JSP. The JSP is certainly not accessing the value of JavaScript variables that will not exist until some time in the future and on a completely different system.
R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
If I search for those variables thru out the jsp file, I cannot find anywhere which they are set up...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Conventionally, scoped variables are set up in the controller.
R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
Thanks so much. Your point, solved the problem.
R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
One more thing... anytime the page loads, the checkbox is unchecked. How can I modify the code, that with every page load, the checkbox appears checked - if it was previously checked - and appears non-checked - if it was previously non-checked?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Add the checked attribute to the checkbox when you want it to be checked. Easy to do with the ternary operator or <c:if>.
R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
I modified the code like below, but the checkbox is still un-checked regardless of whether it was checked before or not. Any idea?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Again, document.search.filter.checked is JavaScript. You cannot use it to make any decision in the JSP code. Did you read the article?

Is the information you want to use to decide whether the control should be checked or not part of the previous submission?
R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
> Is the information you want to use to decide whether the control should be checked or not part of the previous submission?

Yes, user enters two numbers and also he can check the checkbox or leaves it un-checked. But if he checked it, after I load the page with the result, I want the checkbox to be appeared as checked.

Actually my code looks like this now but still no result:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

If there aren't any redirects between the submission and the redisplay of the JSP, the value of the parameter will be available on the param built-in scoped variable.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Also, using <c:choose> is excessively wordy for this usage. It'd be terser to use <c:if> or better yet, the ternary operator.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

And, checked="yes" is a bad bad idea. It makes you think that checked="no" will make the checkbox unchecked but it won't. If there is a checked attribute with any value on the control, the control will be checked. Use one of checked or checked="checked".
R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
Got it, the problem was solved. Thanks so much.
R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
Ah, I was working on your comment of the "param" built in... and that solved the issue, THEN I saw other comments of yours...

> Also, using <c:choose> is excessively wordy for this usage. It'd be terser to use <c:if> or better yet, the ternary operator.

I looked into web and noticed that if I need to have an <if><else> condition, I should use <c:when and <c:otherwise>, that's why I used that way. Since its in fact an <if, else> condition, I don't know how to use only <c:if Or the ternary operator.

I modified my code to set checked="checked".
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

But you don't have an if/else condition -- you have an if condition. You want to include the attribute if the condition is true, and do nothing if not. So there's no else part.

Personally I'd use the ternary operator along the lines of: ${param.whatever == 'on' ? 'checked" : ''} (substituting whatever test you are using to turn the attribute on.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

From a maintenance point of view, if you have code like this:

then you are generating what's called "Technical Debt". In other words, it was easy to write that code but you will pay later when you have to change something in the big lump of code which you have two slightly different copies of.

I can attest to this personally because we used that anti-pattern in a big way over the last few years in our web applications, and it's one of the things we are fixing in a wide-ranging cleanup. Ternary operator? Absolutely!
R Naijat
Ranch Hand

Joined: Nov 01, 2011
Posts: 40
Dear Bear an Paul,
Thanks so much for the comments. I tried to apply what Bear mentioned but looks like I don't know what I'm doing! as it completely crashed my page!!!

So I modified the code to its original - not-efficient! - way to have my page back. This is my code at this point; if you have enough patient, please let me know the modified version that works with only <if> or the ternary operator, if not, I leave it as is, until I learn more! :-)

 
Don't get me started about those stupid light bulbs.
 
subject: Checkbox
 
Similar Threads
JSP does not call the right method in the controller class
New value is inserted into db in @ManyToOne relation instead of update
@Controller annotation query
Multiple RequestMapping annotations on Spring 2.5.6
Spring 3 @PathVariable problem with slash