Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Restoring checkbox state when redisplaying a form

 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you use c:if or c:select in your JSTL dialect? I'm thinking of

 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel your pain.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. What you can't do is to embed a JSP action inside the attribute of another JSP action.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic