• Post Reply Bookmark Topic Watch Topic
  • New Topic

Detecting selected html options using Struts and JSTL?  RSS feed

 
Jimi Svedenholm
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a struts form that includes among other things a <select> with multiple <option>. Each <option> is constructed using information from a CMS. An example output can be like this:

<select name="multiField(ideaCategories)" multiple="true">
<option value="1">abc</option>
<option value="2">123</option>
<option value="5">xyz</option>
<option value="17">abc123</option>
<option value="22">987</option>
</select>

The corresponding set method for this in the form is:
public void setMultiField(String name, String[] values)

This works great, in regards to the action class getting the data from the form ect. But when some form validation failed, and the user is requested to fix the problems, I need the previously selected values to be marked selected="selected". And I would like to accomplice this using JSTL. Is that possible?

The different approaches I have thought of so far are:

1. fetch the array of multiField values for "ideaCategories", loop through it and mark each <option> selected that existed in the array.

2. create a method "public boolean isExistingMultiFieldValue(String multiFieldName, String multiFieldValue)" in the form class. This method would return true if the specified field exists with the specified value. So for the example above, one could make the method call isExistingMultiFieldValue("ideaCategories", "1") etc, and mark each as selected if the method returned true.

But I can't see how I can do any of these using JSTL since I can't call a method that takes parameters, and I have no idea how to search an array for a specific value using JSTL.

Any suggestions?

Regards
/Jimi
[ July 30, 2008: Message edited by: Jimi Svedenholm ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you getting back to the JSP after the Struts action? If by a forward, the parameter value for the select is still available and you can simply test it to see which option needs to be selected.
 
Jimi Svedenholm
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
How are you getting back to the JSP after the Struts action? If by a forward, the parameter value for the select is still available and you can simply test it to see which option needs to be selected.


I'm afraid that I don't quite follow you now. I guess I should use the implicit paramValues object (since it can be more then one value). But how do I get the values using JSTL? As I wrote before, the name of the input field is "multiField(ideaCategories)", and I have no idea how to fetch the values using that name. I tried ${paramValues.multiField(ideaCategories)} and ${paramValues.'multiField(ideaCategories)'}, but the first one gave me the error:

"The function multiField must be used with a prefix when a default namespace is not specified."

and the second one gave me the error:

javax.servlet.jsp.el.ELException: Encountered "'multiField(ideaCategories)'", expected one of [<IDENTIFIER>]


Also, even if I knew how to fetch the array of string values, how do I test using JSTL to see if it contains a particular value? I have searched but not found any information about some jstl function like "indexOf" or "contains" for arrays.

So if you have some example code on how I solve these things, then it would be great. Otherwise I guess I just have to use scriptlets.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really should grab the specs on the EL and JSTL. These are some very basic operations.

The general syntax for fetching uses the [] operators. The dot notation can be used when the property (or param name) is a valid identifier, otherwise the [] operator must be used. For example:

When a scoped variable is an indexed value (array, list) the [] operators can be used in the same way as in Java to index into the array. From there, it should be easy to figure out how to use these values.

To determine if a value contains a string value, yes, the JSTL fn functions are what you need.
[ July 31, 2008: Message edited by: Bear Bibeault ]
 
Jimi Svedenholm
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
You really should grab the specs on the EL and JSTL. These are some very basic operations.

The general syntax for fetching uses the [] operators. The dot notation can be used when the property (or param name) is a valid identifier, otherwise the [] operator must be used. For example:



Thanks. ${paramValues['categoryField(ideaCategories)']} works great to get the String[] that I need.



To determine if a value contains a string value, yes, the JSTL fn functions are what you need.


Well, I'm terribly sorry, but I simply can't find such a JSTL function. And I have really looked. As far as I understand, here are all the JSTL functions:

http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/fn/tld-summary.html

And there is no function indexOf(String[] array, String string), contains(String[] array, String string) or similar. So if you still insist that there is some JSTL function out there that can help me, could you please give an example in code? That would help alot, because as far as I can tell no such function exists.

Regards
/Jimi
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to find out? If any of the param value contains a string? Yeah, there's not gonna be one function or tag that does that. You'll need to use a combination of tags and functions. Just as in Java, when you can't find an operation that does a complex operation, you use the combination of more discrete operations in concert to get things done.
[ July 31, 2008: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. This sounds a lot like data processing. Is it something that's perhaps more appropriately handled in the page controller?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jimi Svedenholm:
Otherwise I guess I just have to use scriptlets.
Another P.S.: resorting to antiquated scriptlets is never appropriate. If all else fails, either factor the code out into the page controller, create a custom function, delegate it to a bean, or create a custom tag.

There is never a good reason to resort to scriptlets in a modern web application.
 
Jimi Svedenholm
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
What are you trying to find out? If any of the param value contains a string?


No, if the array contains the string. Just like the contains method works for ArrayList, for example.

For example, lets say that the array contains the strings "1", "12" and "22", then I should be able to test if the array contains "1" (and it does, so it should return true), "12" (should also return true) and "2" (should return false).
 
Jimi Svedenholm
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
P.S. This sounds a lot like data processing. Is it something that's perhaps more appropriately handled in the page controller?


I don't agree. I consider this a simple requirement, although for some strange reason it seems to be hard to resolve. What I want is simply to know if an array contains a specific value, it is really that simple. But I still haven't found a way to do this using JSTL/EL.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jimi Svedenholm:
I don't agree.
That doesn't carry much weight if you don't explain why.

But, assuming that this is something appropriate to do in a JSP...
although for some strange reason it seems to be hard to resolve.
Not in the least. It's actually quite straight-forward.
What I want is simply to know if an array contains a specific value, it is really that simple. But I still haven't found a way to do this using JSTL/EL.
Assuming the dearth of a handy-dandy contains() method on the list, how would you do it in Java? You'd iterate over the entries looking for a match, right? OK, now, how would you do that with JSTL/EL?
[ July 31, 2008: Message edited by: Bear Bibeault ]
 
Jimi Svedenholm
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
That doesn't carry much weight if you don't explain why.


Well, you kind of removed that part when you trimmed down my quote:
"What I want is simply to know if an array contains a specific value". Would you regard that as data processing? I wouln't. Instead I would consider it just as simple a task as determining if a string contains a specified substring, as "fn:contains(...).



Assuming the dearth of a handy-dandy contains() method on the list, how would you do it in Java? You'd iterate over the entries looking for a match, right? OK, now, how would you do that with JSTL/EL?


That is a totally different issue. I don't have to resort to such a "low level" approach to test if a string contains a substring, so why should I have to that when it comes to determining if an array contains a specific element? I really would consider that to be reinventing the wheel.

But are you telling me that this is the solution you thought of right from the start, and the solution you had in mind when you said "you can simply test it to see which option needs to be selected"? Then why couldn't you just have said "Sorry dude, there is no such jstl function out of the box, the only solution I can think of is iterating over the array values and check each one"? I really don't mean that in an aggressive way (the sentence felt that way when I read out load what I wrote). I am just wondering. We seem to have been doing this "dance" back and forth just because of this missunderstanding. Because *ofcourse* I know that I can iterate over the array values and check for if a specific value exists, but I was wondering if there was some JSTL/EL way that did that for me. And I know that I probably should have been more specific about that right from the start, but it is just that it never crossed my mind that a "iterate over all values" approach is acceptable (for me it isn't really, it is almost up there with scriptlets).

Regards
/Jimi
 
Jimi Svedenholm
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Assuming the dearth of a handy-dandy contains() method on the list, how would you do it in Java? You'd iterate over the entries looking for a match, right?


Just to be clear, I would first of try to use some pre-written functionallity. For example, using the contains-method of ArrayList. If I really wanted to do it using real arrays (ie String[] or Object[] or WhatEver[]) and I couldn't find any pre-written method that does this, then I would write my own and put it in a generic class and use that whenever I need that functionallity. I would never put the iterate-and-look-for-item-X code in each place that I need that functionallity (and that is what I would have to resort to if I would use your suggested approach in the JSP).
[ July 31, 2008: Message edited by: Jimi Svedenholm ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jimi Svedenholm:
Would you regard that as data processing?
That entirely depends upon why you need to know. Is it to make a display decision? Is it to make a business decision? Other? And in any case, yes, if this is a display decision, most often I'd handle something like this in the page controller, and pass a simple boolean to the JSP with the result.

Then why couldn't you just have said "Sorry dude, there is no such jstl function out of the box
I did say that:
there's not gonna be one function or tag that does that. You'll need to use a combination of tags and functions.


but I was wondering if there was some JSTL/EL way that did that for me.
That's not what you seemed to be saying. You seemed to be saying, if there's no JSTL/EL way to do it, I'll do it in scriptlets. Why resort to scriptlets when there is a JSTL/EL solution? You seem to be hung up on there being a pre-made JSTL/EL mechanism. When there isn't, build your own.

And I know that I probably should have been more specific about that right from the start, but it is just that it never crossed my mind that a "iterate over all values" approach is acceptable (for me it isn't really, it is almost up there with scriptlets).
OK, then how would you go about fixing that? What would you do to create a simple, parameterized, resuable solution that you can use on your pages and share with others?
 
Jimi Svedenholm
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edit: I was trying to have multi level quotes here, but it doesn't seem to work. But I guess it is still possible to see which quotes are from who.]



Then why couldn't you just have said "Sorry dude, there is no such jstl function out of the box


I did say that:

there's not gonna be one function or tag that does that. You'll need to use a combination of tags and functions.



But that was your answer to your own question "What are you trying to find out? If any of the param value contains a string?", and that was not the problem I was trying to solve.



but I was wondering if there was some JSTL/EL way that did that for me.


That's not what you seemed to be saying. You seemed to be saying, if there's no JSTL/EL way to do it, I'll do it in scriptlets.


The only time I talked about resorting to scriptlets was in post #3 in this thread, and the first problem mentioned there was the problem with fetching a multi value request parameter with the name "multiField(ideaCategories)". That was before I knew that about the alternative way to fetch them (using ${paramValues['categoryField(ideaCategories)']}), and it was mainly *that* part of the problem that I almost used scriptlets for.


You seem to be hung up on there being a pre-made JSTL/EL mechanism. When there isn't, build your own.


Well, I considered this so extremely fundamental programming wise (just as indexOf of the String class, or contains in the ArrayList class) that I really thought that it was already implemented and I just hadn't found it yet.

Lets say that there was some alternative language to Java, that was very similar to it, but one difference was that it lacked a function indexOf in the class corresponding to the String class in Java. Wouldn't you be even a little bit surpriced when you noticed this? Wouldn't you consider that function quite fundamental when it comes to strings? Would you really just think "oh ok, it doesn't seem to exist such a function. Very well then, I just write my own", without any feeling that you are "reinventing the wheel" and stuff? This is exactly how I felt when I finally realised that the jstl/el function I was looking for doesn't exist. I mean: amazed, chocked and flabbergasted are words that really describe how I feel about this right now... =)


OK, then how would you go about fixing that? What would you do to create a simple, parameterized, resuable solution that you can use on your pages and share with others?


Well, that might be an option. But I would write that code with constant thoughts going through my head like "can it really be so that this fundamental functionallity isn't already implemented?!"...
[ July 31, 2008: Message edited by: Jimi Svedenholm ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jimi Svedenholm:
But I would write that code with constant thoughts going through my head like "can it really be so that this fundamental functionallity isn't already implemented?!"...
In over 8 years of writing JSPs, I've never had a need for such a mechanism. So perhaps it's not as fundamental and common a need as you might be thinking.

In any case, personally (in case you have the least little interest in how I'd solve this) I'd bundle this up as an EL function.
 
Jimi Svedenholm
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
In over 8 years of writing JSPs, I've never had a need for such a mechanism. So perhaps it's not as fundamental and common a need as you might be thinking.


But you must have been handling cases similar to what I described in post #1, or haven't you? If you have, how did you solve it then?



In any case, personally (in case you have the least little interest in how I'd solve this) I'd bundle this up as an EL function.


Yes, this seems to be the most straight forward way to do this, since no such functionallity exists in JSLT or EL. And I have already been looking into how to write custom EL functions for jsp-pages.

And now, here are some so called "Graemlins" that summarize my different states of emotion that I went through during this thread (mostly based on my growing knowledge about JSTL and EL), and to lighten up the atmosphere here:

First:
Then:
Then:
Then:
And now: (regarding your suggestion about a custom EL function)

Take care

Regards
/Jimi
[ July 31, 2008: Message edited by: Jimi Svedenholm ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jimi Svedenholm:
But you must have been handling cases similar to what I described in post #1, or haven't you? If you have, how did you solve it then?
Just as we did here. The JSTL and EL are purposefully kept lean and mean by design. The whole idea is to force JSP page developers to think about what they're doing and to make sure that they're keeping as much off the pages as possible; Smart controllers and dumb pages are a maintainers delight.

The various extension mechanisms: tag files, beans, EL functions, and the simplified custom tag handling were added to allow page developers to create their own mechanisms using the fundamental building blocks.

And now, here are some so called "Graemlins" that summarize ...
Reminds me of the five state of acceptance...
 
Jack Lantern
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you have to do is iterate through the values using forEach and then an if inside the forEach loop to test if the indexed value is the same as the current option to select it.
For example:



In the above code myScopedObj is a bean that is used to store the form field state. This stupid trick will at once satiate the MVC purist morons who denounce "scriptlet" code in the JSP and avert you from having to resort to more complex solutions.

-ec
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Jack Lantern", please check your private messages for an important administrative matter.

And please read this with regard to posting to old topics.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!