?
Solved

JSF question: "ondblclick" attribute for "selectOneListbox" component

Posted on 2006-07-03
12
Medium Priority
?
1,756 Views
Last Modified: 2013-11-19
Hi,

I am using Java ServerFaces for the web development, in my jsp I have got a "selectOneListbox" tag, when the user double click any value in the listBox, I want in the server side I can detect the user double click action and then do something with it. How can I do this without put the processing code inside the valueChangeEvent of the listBox? Many thanks.
0
Comment
Question by:victorli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 17029861
Hi victorli

AFAIK you cannot do that unless you use JavaScript and use the onDblclick event on the generated HTML.

Cheers
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17030020
Since firing an event on the server requires a submit, you can't attach server-side code to double-click. You can catch it a ValueChanged event, and do a submit on double-click, so you end up with something like:

<h:selectOneListbox id="listbox1" valueChangeListener="#{bean.method}" ondblclick="window.forms[0].submit();" ...>

The tooling often helps you to create the various code snippets, e.g. IBM's tooling will create the valuechange listener code in the pagecode file.
0
 
LVL 1

Author Comment

by:victorli
ID: 17033658
Thanks for your reply, but as I said in the question, I want to do this without calling the code under valueChangeEvent, is there other way? like can I use Javascript to fire a hidden command in the jsp page which links to a ActionListener in the server side?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:bpmurray
ID: 17033888
Actually, you can use AJAX-like functionality, although this is non-trivial. It all depends on what you want to do on the server side. Do you want to change something that would affect the JSF view? In that case, you really have to go through the entire lifecycle. If you want to get information that is independant of the JSF lifecycle, something that can be retrieved from a different servlet, you can do that easily, but the most usual case is including a phase listener that manages the submit, calling the external method and then calling responseComplete() to cause immediate return.

Another possible solution would be to have a hidden submit button that's clicked by JS and the onclick action executed on the server.
0
 
LVL 1

Author Comment

by:victorli
ID: 17034010
Thanks bpmurry.

I think the both approaches you recommended are what I want, so could you please give me the detailed code example? I am doing the hidden submit button one but it does not work.
0
 
LVL 1

Author Comment

by:victorli
ID: 17034039
The reason I can not put the code inside the selectOneBox's ValueChangeListener is I need to change the view, after I set the value, the set value will be overidden by the previous data. Probably this is because the ValueChangeListener happens before the data model change phase. I found I can always change the view when I put the code under ActionListener.
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17034072
Not sure which you want, but I'll assume that you want the separate servlet ....

1. In your web.xml add the following:
    <servlet id="myServlet">
          <servlet-name>My DoubleClik Servlet</servlet-name>
          <servlet-class>com.foo.faces.webapp.DoubleClickServlet</servlet-class>
          <load-on-startup>-1</load-on-startup>
     </servlet>
        ---
     <servlet-mapping>
          <servlet-name>My DoubleClick Servlet</servlet-name>
          <url-pattern>/myAjax/*</url-pattern>
     </servlet-mapping>

2. The class referenced above:
package com.foo.faces.webapp;


public class ImgServlet extends HttpServlet {
      public void doGet(HttpServletRequest request,
                        HttpServletResponse response)
                      throws IOException, ServletException
      {
             ServletOutputStream os = response.getOutputStream();
             String uri = req.getRequestURI();
           // Do your thing here
      }
}

3. The Javascript
function getAjaxStuff(url, id) {
  if (document.getElementById) {
    var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
  }
  if (x) {
    x.onreadystatechange = function() {
      if (x.readyState == 4 && x.status == 200) {
        el = document.getElementById(id); // Assume we want to put the returned markup in to the section "id"
        el.innerHTML = x.responseText;
      }
    }
    x.open("GET", url, true);
    x.send(null);
  }
}

4. Changes to your page
<h:selectOne ondblclick="getAjaxStuff('http://www.system.com/myAjax/foo', 'contents');" ...>
...
<div id="contents"></div>
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17034080
Duh! I should have waited a minute! Rather critical information!!!!

OK, this sounds like it's the hidden submit and action method. Give me a minute ....
0
 
LVL 1

Author Comment

by:victorli
ID: 17034112
Thanks for your code example, but I really think this is quite costyif using servlet as you exampled the code. Can we hage simpler solution? If not using JSF, I know it is easy to implements a JS which submit the form and attaction some request to the serverside, then in the server side, i could use the request.getParameter("doubleClickAction") to detect and then write the processing code. Just not sure how to do this in JSF apart from the standard listener approach.
0
 
LVL 15

Accepted Solution

by:
bpmurray earned 1000 total points
ID: 17034136
1. Change the page:
<h:commandButton type="submit" value="Submit" style="display:none" id="button1" action="#{bean.actionMethod}"></h:commandButton>

<h:selectOne ondblclick="clickIt('form1:button1');" ...>

2. Add JavaScript:
function clickIt(obj) {
   var elem = (window.document.getElementById)? window.document.getElementById(obj) : window.document.all(obj);
   elem.click();
}

0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17034146
The reason I gave such a complex solution was because I wasn't aware of what you wanted to achieve, so I provided something that would give a generic solution. Had I realized that all you wanted was to change the view, I would have gone straight for the second one. BTW, I presume you're aware of how to set up navigation rules based on the return from your action.
0
 
LVL 1

Author Comment

by:victorli
ID: 17034193
Thanks, the problem is resolved.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question