Discussing the nuts and bolts of software development

Monday, April 21, 2008

 

Struts 2 doubleselect: A Detailed Example

Struts 2 provides a fantastic doubleselect tag, which allows you to specify two HTML select lists in such a way that the contents of the second list is dependent on the selection of the first (1). About a month ago, I came across a situation where this type of relationship was very advantageous, and set off towards the relevant Struts 2 documentation to learn how this mysterious tag worked. I found the list of attributes useful, but the examples at the bottom of the page left much to be desired (they're completely hard-coded). In fact, even after a good hour of searching, I couldn't find a solid example of the doubleselect tag that didn't hard-code both lists! Obviously it's much more useful to take advantage of the java back-end, and it took me two full hours of trial and error to finally figure out how to get my particular scenario working. What follows is a detailed example showing how to use the Struts 2 doubleselect tag to its fullest, for the next time I or anyone else wishes to make good use of it.

For our example, suppose you run some sort of automotive website, and would like to let the use specify the Make and Model of a car. The list of Makes should contain the car company, like Porsche, Ferrari or Lamborghini. The lists of Models should be the specific cars those companies make, like "911 Turbo" and "Boxster S" for the Porsche, "F430 Spyder" and "FXX" for Ferrari, etc. So when Porsche is selected in the first list, it's set of models will be shown in the second list. If we change the selection in the first list to Ferrari, the second list should update to contain only the Ferrari models. We also want to know what Make and Model the user has selected, and be able to set default selections in each list. This can actually be done with very little JSP syntax using the doubleselect tag:


<%@ taglib prefix="s" uri="/struts-tags"%>

<s:form action="selectCar" method="POST">
<s:doubleselect
name="makeId" doubleName="modelId"
list="allMakes" doubleList="models"
listKey="id" doubleListKey="id"
listValue="name" doubleListValue="name"
value="defaultItem" doubleValue="doubleDefaultItem" /
>
</s:form>


Of course, in addition to the jsp shown above, we need a struts.xml file, the struts action, a Make class and a Model class. It is assumed that you already know how to set up the struts.xml file and the basics of the struts action. Here's what we need in the other files:
When the page loads, here is how the doubleselect tag is interpreted:
When the form is submitted, the name and doubleName attributes will call setMakeId and setModelId in the action, and give them the values of listKey and doubleListKey respectively. So if Lamborghini is selected in the first list, and Murcielago in the second, the parameters passed in setMakeId and setModelId would be the id of the Make instance which has the name Lamborghini, and the id of the Model instance which has the name Murcielago.

So that's all there is to it! I hope this has been helpful, and saves a bit of time for the next person trying to implement a complex double-list system with back-end support.


(1) This functionality is provided using javascript which is automatically generated when Struts interprets the doubleselect tag. One downside to this approach is that it will clutter up your source quite a bit if you're using a large dataset, but this is unavoidable while using this tag.

Labels: ,


Comments:
I had a big problem with this tag.
My doubleSelectOnChange doesn't make call to my javascript function and I don't kwnow why.
When I saw source-code page, the call isn't exist in the page.
Anyone knows what's happen?
 
I did a bit of testing, and from what I can gather, the documentation is simply incorrect; it doesn't look like the doubleselect tag supports the onChange event (this could be because Struts uses this event on the first list to load the second list, though that doesn't explain why they would simply ignore it for the second list).

The onSelect event doesn't seem to be supported either, and although onBlur seems to fire, doubleOnBlur doesn't (this is also true for onFocus).

It looks like you might be out of luck for built-in event support. You may be able to extend the tag and manually add support for these events, or you could always just simulate the doubleselect behavior yourself using DHTML/AJAX.
 
Thanks, really i was suffering because i couldnt do work my example, noy is working accesing data from DB.

Regards
 
This was *very* helpful. Thanks!
 
This was very helpful.Thank you!
 
I have a problem implementing this tag. I get this in the trace:

Caused by:
freemarker.core.InvalidReferenceException: Expression parameters.formName is undefined on line 101, column 43 in template/simple/doubleselect.ftl.

 
@MichiIndian:

This is Struts' way of telling you that it is expecting a property called formName on the doubleselect tag. Looking at the tag's documentation, this is an inherited property, so the problem is probably on a parent tag, not the doubleselect tag itself.

Make sure that the doubleselect tag is inside a struts form tag, and if that still doesn't work, try specifying the formName property explicitly on the form/doubleselect tag.
 
This comment has been removed by the author.
 
can you give me the layout of strut.xml file for this example ?
 
what if i have four select boxes and on selection of an item in first select will populate second select box, and on selection of an item in second select, third and so on till fourth select box gets populated ? How to do this?
 
@ D: There wasn't anything special about the struts.xml file, and it wouldn't make sense without the back-end Java anyways. There are many great resources for learning struts online, try the official docs: http://cwiki.apache.org/WW/home.html
 
@ kiran: By design, the doubleselect tag will only make two paired select boxes. If you want four, you're going to have to do it yourself. The best way to do this would probably be to extend the doubleselect tag and implement your own quadrupleselect tag or something similar. Here is a decent post to get you started about extending tags: http://bodez.wordpress.com/2009/03/13/customising-struts2-jsp-tags/

If you do this, I'm sure it would make a great blog post and I'd love to read it — so be sure to send me a link!
 
Great article. Helped me a lot!
 
Unlike official documentation this article is so helpful! Even two years after publishing :)
 
It's still not working from me:
The stack trace says:


java.lang.ArrayIndexOutOfBoundsException: -1
at com.opensymphony.module.sitemesh.html.HTMLProcessor$1.currentBuffer(HTMLProcessor.java:74)
at com.opensymphony.module.sitemesh.html.State.handleText(State.java:64)
at com.opensymphony.module.sitemesh.html.HTMLProcessor$2.text(HTMLProcessor.java:103)
at com.opensymphony.module.sitemesh.html.tokenizer.Parser.parsedText(Parser.java:295)
at com.opensymphony.module.sitemesh.html.tokenizer.Parser.parseTag(Parser.java:160)
at com.opensymphony.module.sitemesh.html.tokenizer.Parser.start(Parser.java:112)
at com.opensymphony.module.sitemesh.html.tokenizer.TagTokenizer.start(TagTokenizer.java:33)
at com.opensymphony.module.sitemesh.html.HTMLProcessor.process(HTMLProcessor.java:90)
at com.opensymphony.module.sitemesh.parser.HTMLPageParser.parse(HTMLPageParser.java:52)
at com.opensymphony.module.sitemesh.filter.Buffer.parse(Buffer.java:49)
at com.opensymphony.module.sitemesh.filter.PageResponseWrapper.getPage(PageResponseWrapper.java:165)
at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:124)
at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:55)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:738)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
But if I insert sysout statements after double select they work..... wierd...
 
Hi, although you make me understand the code very nicely. 80% understood. But please-please-please could you paste the code of those three classes i.e. ActionClass, MakeClass & ModelClass. I'll be deeply thankful to you. I need those codes very badly. NO other site has yet make the doubleSelect tag dynamic..
 
Hi, although you make me understand the code very nicely. 80% understood. But please-please-please could you paste the code of those three classes i.e. ActionClass, MakeClass & ModelClass. I'll be deeply thankful to you. I need those codes very badly. NO other site has yet make the doubleSelect tag dynamic..
 
Thanks a lot ..your example is awesome :)

Sreenath
 
Struts is the most popular framework which is used for developing the java based applications. Struts framework is based on the model view controller architecture.
struts training in chennai | struts training
 
I have read your article. It is really useful for me. Start to learn Java programming because there is a massive demand for Java developers in IT sectors. It has a great scope in future.
JAVA Training in Chennai | JAVA course in Chennai

 

The strategy you have posted on this technology helped me to get into the next level and had lot of information in it. The python programming language is very popular and most widely used.
Python Training in Chennai | Python Course in Chennai
 
Thanks of sharing this post…Java is the fastest growing language that helps to get your dream job in a best way, so if you wants to become a expertise in Java get some training on that language.
Regards,
Core JAVA Training in Chennai | JAVA Training in Chennai

 
Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training

Hibernate Online Training Hibernate Online Training Spring Online Training Spring Online Training Spring Batch Training Online Spring Batch Training Online
 
I have read your blog its very attractive and impressive. I like it your blog.
Java Training institute in velachery
 
Excellent post!!! Java is most popular and efficient programming language available in the market today. It helps developers to create stunning desktop/web applications loaded with stunning functionalities. Java Course in Chennai | Best JAVA Training in Chennai
 
Try Articles Pool. One of the top rated do follow article submission sites with extreme SEO value and quick approval.
 
Are you looking for best website to download eBook torrents for free? Then Ebook Share will be the right place. ebookshare | kovalanj
 
Thanks for sharing such a great information..Its really nice and informative..

scada 6 months project based industrial training
Jvhdl 6 months project based industrial training
ansys 6 months project based industrial training
 
Great info, thanks for your valuable time, very useful.
Java Training in Chennai
 
Very impressive and nice blog, Thanks for sharing your valuable information.
Java Training institute in Velachery | Best Java Training in velachery
 
Billions of applications are running based on java language, so learning java is always worth. But choose a right inistitute for learning candid industrial training institute forjava training in chennai
 
Excellent post!!! Your article helped to under the future of java development. Being an open source platform, java is integrated in most of the software development industries to create rich featured applications.Java Course in Chennai | Best JAVA Training in Chennai
 
this post is very helpful.thanks for sharing.
Java courses in chennai
Best Android Training in Chennai
 
Interesting post! This is really helpful for me. I like it! Thanks for sharing!
Mobile application developers in Chennai | PHP developers Chennai
 
Thanks for the insightful article from https://godigitally.in/
 
Thanks for the insightful article from https://godigitally.in/
 
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
Best Hadoop Training Institute In chennai
 
Im no expert, but I believe you just made an excellent You certainly understand what youre speaking about, and I can truly get behind that.
Best JAVA Training in Chennai|JAVA Training|Salesforce Training in Chennai|Android Training in Chennai|Cloud Computing Training in Chennai|Cloud Computing Courses in Chennai
 
The young boys ended up stimulated to read through them and now have unquestionably been having fun with these things. Digital Marketing Training in chennai
 
Thank you for sharing this blog. This is awesome blog for beginners. This will help to improve my JAVA knowledge.
Struts Training in Chennai | Struts Training | Struts Training center in Chennai | Struts course in Chennai
 
Great Blog! Thanks for sharing with us.

Selenium Training in Chennai | Software Testing Training in Chennai


 
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

Android Training In Chennai
 
This was an nice and amazing and the given contents were very useful and the precision has given here is good.
AWS Training in Chennai

 
Im no expert, but I believe you just made an excellent You certainly understand what youre speaking about, and I can truly get behind that.
Digital Marketing Training In Chennai

 
Nice and usefull post thanks for sharing.


Selenium Training in Chennai
 
This was an nice and amazing and the given contents were very useful and the precision has given here is good.
aws training in chennai

 
awesome content thanks for sharing.
RPA Training in Chennai
 

Thankyou for sharing this good information.hadoop training in chennai
 
● I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.
Python training in Bangalore

 
Absolutly excellent post.
Python online training
 
Useful article.

Regards
SEO Training

 
keep sharing your information regularly for my future reference. This content creates a new hope and inspiration with in me. Thanks for sharing article like this


Selenium Training in Chennai

RPA Training in Chennai

 
Really very nice blog information for this one and more technical skills are improve,i like that kind of post.
Devops training in velachry
Devops training in OMR
Deops training in annanagar
Devops training in chennai
Devops training in marathahalli
Devops training in rajajinagar
Devops training in BTM Layout

 
Your story is truly inspirational and I have learned a lot from your blog. Much appreciated.
java training in chennai | java training in bangalore

java training in tambaram | java training in velachery

java training in omr
 
Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.

angularjs Training in bangalore

angularjs Training in btm

angularjs Training in electronic-city

angularjs Training in online
 
myTectra placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance.
 

I would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end. Thanks

angularjs-Training in annanagar

angularjs Training in chennai

angularjs Training in chennai

angularjs Training in bangalore

 
You truly did more than visitors’ expectations. Thank you for rendering these helpful, trusted, edifying and also cool thoughts on the topic to Kate.
nebosh course in chennai

 
hello sir,
thanks for giving that type of information.
digital marketing company in delhi
 
Very well written blog and I always love to read blogs like these because they offer very good information to readers with very less amount of words....thanks for sharing your info with us and keep sharing.
python training in velachery
python training institute in chennai

 
Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
Good discussion.
PMP Training Course in Bangalore
PMP Training Course in Dammam
PMP Training Course in Dubai
PMP Training Course in Jeddah
PMP Training Course in Riyadh

 
Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.

Devops Training in pune
DevOps online Training

 
Lyrics with music
 
Read all the information that i've given in above article. It'll give u the whole idea about it.
Selenium Training in Chennai | Selenium Training in Bangalore |Selenium Training in Pune | Selenium online Training
 
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us. Do check Six Sigma Training in Bangalore | Six Sigma Training in Dubai & Get trained by an expert who will enrich you with the latest trends.

 
hi, nice information is given in this blog. Thanks for sharing this type of information, it is so useful for me. nice work keep it up.
outdoor led flood lights in delhi
 
myTectra Placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance
 
Nice blog has been shared by you. it will be really helpful to many peoples who are all working under the technology. Thank you for sharing this blog.
offshore safety course in chennai
 
Those rules moreover attempted to wind up plainly a decent approach to perceive that other individuals online have the indistinguishable enthusiasm like mine to get a handle on incredible arrangement more around this condition.nebosh course in chennai
 
Thank you much for this tutorial; this is an informative and valuable blog. Visit for
Website Development Company in Delhi
Web Designing Company in Delhi
 
I and my friends were going through the nice, helpful tips from the blog then the sudden came up with an awful suspicion I never expressed respect to the website owner for those secrets.
safety course in chennai

 
Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
angularjs online Training

angularjs Training in marathahalli

angularjs interview questions and answers

angularjs Training in bangalore

angularjs Training in bangalore

angularjs Training in chennai
 
English Song lyrics
 
fridge repair in gurgaon
 
I am really happy with your blog because your article is very unique and powerful for new reader.
Click here:
selenium training in chennai
selenium training in bangalore
selenium training in Pune
selenium training in pune
Selenium Online Training

 
Awesome..You have clearly explained.it is very simple to understand.it's very useful for me to know about new things..Keep blogging.Thank You...
aws online training
aws training in hyderabad
aws online training in hyderabad

 
Awwsome informative blog ,Very good information thanks for sharing such wonderful blog with us ,after long time came across such knowlegeble blog. keep sharing such informative blog with us. Aviation Courses in Chennai | Best Aviation Academy in Chennai | Aviation Academy in Chennai | Aviation Training in Chennai | Aviation Institute in Chennai
 
It is a great post. Keep sharing such kind of useful information.

smarthrsolution
Article submission sites

 
Very informative blog! i liked it and was very helpful for me.Thanks for sharing. Do share more ideas regularly.

Best IELTS Institute in Chennai
IELTS Coaching Classes in Chennai
IELTS Courses in Chennai
IELTS in Chennai
IELTS Courses near me
IELTS Coaching Classes in Mumbai
IELTS Institute in Mumbai
 
Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.

Java training in Bangalore |Java training in Rajaji nagar | Java training in Bangalore | Java training in Kalyan nagar

Java training in Bangalore | Java training in Kalyan nagar | Java training in Bangalore | Java training in Jaya nagar
 
I am reading your blog regularly, what a post very interesting and great content. Thank you for your post!!!
CCNA Certification in Bangalore
CCNA Training Bangalore
CCNA Training institutes in Bangalore
CCNA Training in Aminjikarai
CCNA Course in Chennai Kodambakkam
CCNA Training in Vadapalani
 
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
excel advanced excel training in bangalore | Devops Training in Chennai


 
I found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article.
python course institute in bangalore
python Course institute in bangalore
python course institute in bangalore

 
This information is impressive. I am inspired with your post writing style & how continuously you describe this topic. Eagerly waiting for your new blog keep doing more.
Android Training in Bangalore
Android Developer Course in Bangalore
Android App Development Training in Bangalore
Android Training Center in Bangalore
Android Institute in Bangalore
 
The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.

angularjs Training in marathahalli

angularjs interview questions and answers

angularjs Training in bangalore

angularjs Training in bangalore

angularjs online Training
 
This blog is more effective and it is very much useful for me.
we need more information please keep update more.
Salesforce Training in Nungambakkam
Salesforce Training in Vadapalani
Salesforce Training in Nolambur
Salesforce Training in Perambur
 
Hi, Your blog is very impress to me. I am very glad to read your post. Thank you for your sharing.
Best PHP Training in Bangalore
PHP Coaching in Bangalore
PHP Course in Perambur
PHP Training in Nolambur
PHP Course in Nungambakkam
PHP Training in Saidapet
PHP Training in Navalur
PHP Course in Kelambakkam
 
thank you for sharing information, information on your site is very useful for many people. I think will often come back to your site.
digital marketing company in patna
 
Thanks for sharing such an amazing blog. It is really helpful for me and I get my lots of solution with this blog. also cheack our site. packers and movers in Patna
 
I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
Selenium Training in Chennai
software testing training institute chennai
Selenium testing training
Selenium Courses in Chennai
Best Software Testing Training Institute in Chennai
Testing training

 
THANKS FOR INFORMATION

you can search low-cost website with high-quality website functions.
Today Join Us
Call: +91 - 8076909847

website designing company in Delhi

levantro
interior designer in delhi


livewebindia
website development company in delhi

SEO company in Delhi

Best It Service Provider:

1. Website Designing And Development.
2. SEO Services.
3. Software Development.
4. Mobile App Development.
 
hello sir,
thanks for giving that type of information.
best digital marketing company in delhi
 
Laminated Doors manufacturer in hubli
Thanks for giving great kind of information. So useful and practical for me. Thanks for your excellent blog, nice work keep it up thanks for sharing the knowledge.
 
led lawn lights in delhi
Thanks for giving great kind of information. So useful and practical for me. Thanks for your excellent blog, nice work keep it up thanks for sharing the knowledge.
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?