Two Laptop Bag*
The moose likes JSP and the fly likes Restoring checkbox state when redisplaying a form 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 "Restoring checkbox state when redisplaying a form" Watch "Restoring checkbox state when redisplaying a form" New topic
Author

Restoring checkbox state when redisplaying a form

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2833
    
  11

I'm working on an application that has one page for the user to fill in a form and submit it, and a second page to display results. If there are any validation errors in the submitted values, I catch these in a servlet (actually a portlet), set the submitted values to request attributes, and then redisplay the form page. I have the input fields set to fill in with these attributes, so a text input field looks like:



(I'm working with an ancient version of JSPs -- from JEE 1.3 I think -- so EL only works inside c: tags.)

So that works well. However, we've recently added multichoice selections implemented as checkboxes with a common name, so something like:



When the form is submitted, I get a request parameter "choices" with an array of Strings showing which checkboxes were checked. I'd like to restore the checked state of these boxes if there is a validation error with one of the other fields, but I'm not sure how to do it. Ideally, there would be EL like:



but I don't think there is. The best solution I can think of is to create a CSV string of all the previously selected items, put it in a hidden field, and then have a JavaScript load function parse the string and set up the checkboxes. Is that a reasonable approach, or am I missing something that would be much easier?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Can you use c:if or c:select in your JSTL dialect? I'm thinking of

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2833
    
  11

I can definitely use c:if, and probably c:when as well. The problem is what would the test be? As I said, it would ideally be something like "<c:if test=${item.id in choices}>", but that's not valid EL.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

Greg Charles wrote:so a text input field looks like:


Why?

The whole purpose of a JSP is to avoid markup in strings. Why the markup inside a <c: out>?

I understand that you can't use EL outside of JSTL (why so ancient a version? that's over 10 years old!). But why not just:


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Greg Charles wrote:I can definitely use c:if, and probably c:when as well. The problem is what would the test be?


You've got an array? Then (before forwarding to the JSP) I would convert it into a Map where the keys are the array entries and the values are just something non-empty (like "X"). Then the EL would be something like

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2833
    
  11

@Paul -- brilliant! That worked!

@Bear -- it's so funny; as I posted this question, I started wondering if I could embed a "c:out" tag inside an attribute value, and thought maybe I should try that before posting, because otherwise I'd risk Bear's goggle-eyed emoticon. Next time I'll listen to my instincts. :)

As to your parenthetical question, it's a government customer, and they're working on upgrading licenses and infrastructure so we can move to a more current platform. Apparently, they made it a top priority two and a half years ago, so I'm sure it'll happen, like, any day now. I complained about it for three months, tolerated it for three more, but now, once I get these pages working, I'm just getting the heck out of Dodge. Next month, I'll start on a new project using real 21st century technology and it can't come too soon for me.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Greg Charles wrote:I started wondering if I could embed a "c:out" tag inside an attribute value...


Yeah, you can. I used to have a lot of code like that, and the resulting thicket of quotes and angle brackets wasn't much fun to work with. Fortunately our upgrade process here is somewhat less bureaucratic and so last year I got to go through all of our JSPs and clean that all up.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

I feel your pain.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

Paul Clapham wrote:
Greg Charles wrote:I started wondering if I could embed a "c:out" tag inside an attribute value...

Yeah, you can.

More on this: anything that's not JSP actions is just template text. So the JSP engine doesn't know that the <c:out> is in an HTML element attribute -- it doesn't know anything about HTML at all!

Template text (HTML, JavaScript and so on) is passed along as is and the JSP engine could care less what it is.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

P.S. What you can't do is to embed a JSP action inside the attribute of another JSP action.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Restoring checkbox state when redisplaying a form
 
Similar Threads
problem while using input type="checkbox"
Accessing an ArrayList of objects in JSP
List of checkboxes and Array/ArrayList data binding not working.
Validating a JSP
how i get selected list data