How to implement a keyListener in JSF/JSP

Hello,
I�m using JSC and want to implement a keyListener in my application. I want to press ENTER and a method in my java class should be called (regardless of the component, which has the focus). Is this possible (normally, a component must have the focus for that)?
I can get the pressed key using javascript but how can I call the corresponding java method? And if I use javascript, that means that a UI-component should gain the focus first to call its javascript method. In this case I don`t even need javascript, because I need a general possibility to catch a key pressed event.
Thanks for suggestions

Hi Dan,
Here is some key listner code that makes sure the user only enters numbers. Maybe you can change it for the return button (I think the charCode of the return button is 10, btw)
<script type="text/javascript">
function checkIt(evt)      
     var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
     if (charCode > 31 && (charCode < 48 || charCode > 57))
          alert("Please make sure entries are numbers only.");
          return false;
          return true;
</script>And in the h:inputText I have onkeypress="return checkIt(event)"
Cheers,
Illu

Similar Messages

  • How to implement smart navigation in JSF or JSP

    hi,dear all:
    we design a jsp page with tiles,everything is ok.
    But we got a problem: when we design many input text or radio button or listbox on our page1.jsf,it will reload to itself when it got error with the component that user keyin and render the page on the top place.
    we want user will see the place that he keyin error (droll automatic) not the top of the page.
    we see that already done by asp.net's "smart navigation",but we must write in jsf.
    How to do that next step? thank you very much.

    Hi Dan,
    Here is some key listner code that makes sure the user only enters numbers. Maybe you can change it for the return button (I think the charCode of the return button is 10, btw)
    <script type="text/javascript">
    function checkIt(evt)      
         var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
         if (charCode > 31 && (charCode < 48 || charCode > 57))
              alert("Please make sure entries are numbers only.");
              return false;
              return true;
    </script>And in the h:inputText I have onkeypress="return checkIt(event)"
    Cheers,
    Illu

  • How to do a KeyListener in JSF ?

    Hello,
    I have written a jsf application and I need to implement a key listener but I don't know how.
    In fact throughout the program I have to save in a file everytime a "T" is pressed and this could be in every pages and even several time in a page.
    Can someone tell me if this is possible.
    Thanks in advance for any help.

    Your JSF:
    <h:form id="YourForm" onkeypress ="if (event.keyCode == 84) {saveForm(); return false;}; return true;">
            <h:inputHidden id="hiddenSave" action="YourBean.SaveAction"/>
    </h:form>Your Javascript:
    function saveForm(){
         if (event.keyCode == 84){
              document.getElementById('YourForm:hiddenSave').click();
              return false;
         return true;
    }You can also look into ajax4jsf, if you want to implement the same asynchronously.
    NOTE: The above approach will trigger the save action, even if you enter a "T" into a text field.
    ~SirG
    Edited by: SirGeneral on Feb 2, 2009 11:50 AM

  • How to implement this interface using jsf

    This problem's been bothering me for serval days.
    Just a simple question, how to render the following case purely by JSF?
    http://www.hcj-sh.com/temp/case.gif
    case 1.
    It includes 2 radio button2, these 2 radios are in virtual layout, this is not different,
    but I need attach other items(such as input text filed, select menu... etc) after each
    radio button,,,,,
    case 2.
    5 raidos, 3 one the first row, and the others 2 on the second row....
    I had sleep on it for a long time, but still no answer...
    anyone can help me?
    Thanks a lot.

    I am not sure what you mean by case.? do you want to
    insert other components between the radio buttons ? in
    that case why don't you make them as two separate
    lists ?yes, I want to insert other components between the 2 radio buttons.
    the 2 raido buttons are in the same group, check one will auto uncheck another,
    so i think two spearate lists is not fit my require.
    For case 2, try using two SelectItemGroups, one for
    each set of radio buttons and see if that gives you
    the layout you want.SelectitemGroups? i donot found this tag in JSF, can you tell me more about it
    Thanks.
    David

  • How to implement JavaScript validation in JSF

    Dear all,
    if i wish to implement JavaScript front end validation function in JSF. what should i do?
    thanks.

    Hi,
    in Core JSF from David Geary there is a chapter on using
    Jakarta Commons Validator for javaScript validation
    source could be found here:
    http://www.horstmann.com/corejsf/
    HTH,
    Matthias

  • How to implement the following code into jsp

    import java.io.File;
       import javax.swing.*;
       public class Dirs {
         public static void main(String args[]) {
           SwingUtilities.invokeLater(new Runnable() {
             public void run() {
               JFileChooser fileChooser =
                      new JFileChooser(".");
               fileChooser.setMultiSelectionEnabled(true);
               fileChooser.setFileSelectionMode(
                      JFileChooser.FILES_AND_DIRECTORIES);
               int status = fileChooser.showOpenDialog(null);
               if (status == JFileChooser.APPROVE_OPTION) {
                 File selectedFiles[] =
                     fileChooser.getSelectedFiles();
                 for
                   (int i=0, n=selectedFiles.length; i<n; i++) {
                         System.out.println("Selected: "
                            + selectedFiles.getParent()
    + " --- "
    + selectedFiles[i].getName());
    System.exit(0);

    You don't, it's Swing code, client side only. The JFileChooser will open up a file dialog used to open or save files on the client machine: you have no access to the client filesystem from a jsp.

  • How to Implement Dynamic Tree structure in jsp

    Hi,
    Does any body help me to solve out my requirement??
    I want to display all my folder contents (parent & childs) in windows explorer format, the root folder path will come from database.
    Thanks in advance

    There are many ways to achieve this. The simplest would be to use some existing tag library that renders a tree structure and feed it with the required data.

  • How to implement this function in JSP/Servlet env?

    Hi all,
    I working on a project, it provides functionality to upload file using JSP/Servlet. In the first JSP page, there is file location and submit button. After user select a file to upload and click submit button, a message, like "sending file to XXXX", will be shown on the screen. Once uploading and validation are done on the server-side, a successful/error msg will be shown to user.
    Here I have a question for the "sending..." msg and the successful/error msg. They should be put in one jsp page or in two separate page? how to implement them?
    Thanks for any help!
    Tranquil

    For the sending message... Well, the thing is, when you click submit, it's sending the file to the server and the server is processing it, and this is all done before the "complete" page is sent to the server. So one would need to use some Javascript on the page before the actual submit happens to show some message. This is done on Ebay when you put something for sale, you can upload an image, and there is a little popup message telling you that it's uploading, and it is removed when the process is done. Now, I'm not sure the exact details of how this works, but my educated guess is this:
    1) The onsubmit function of the form checks that the file upload fields have a value (no need to popup a message if no file upload, since that's what usually takes the time, although it could just be assumed there is a file). If a file is to be uploaded, or just want to show the message anyway, a new popup window is opened with the window.open method and the "sending" message is shown (either written via Javascript or just load a small web page to the window).
    2) The popup window, since you can't transfer the window object from the form page to the next page, has to check window.opener for some value that the success/error page would have to set. The success/error page could use it's body onload function to set a variable in it's own window object to denote that the page is loaded. The popup window can use a looping check using setTimeout or setInterval in Javascript to check for window.opener.isLoadedVariable to be present, and if so, close itself.
    I've never done that, but I see no reason why it wouldn't work.

  • How to implement Servlet/JSP in PAR to be used standalone (not as iView)

    Hi everyone!
    I want to develop a Servlet (or a JSP) that is part of my PAR with the main iView because it will be called in an iframe within that iView.
    1. It has to be possible to call it directly from the Browser (iframe).
    2. The output has to be exactly what i generate in it, meaning there must not be added anything to the output by the portal.
    3. Because it uses the same classes that the iView itself is made of, I think it has to be part of the same PAR file.
    4. Creating the servlet in an EAR and then copying (duplicating) the class files from the iView into it or putting them into their own jar and copying that into the PAR and the EAR is not really an option (for maintenance and development reasons)
    What I tried so far:
    -- implement the functions in a JSP which I put into dist/jsp => JSP is accessible from the browser but importing classes from the src.core is not possible (neither from default package, nor from any other named package in there)
    -- implement in a JSP which I put into dist/PORTAL_INF/pagelet => JSP is not accessible from the browser (tried with the URL from IResourceInformation), so I don't know whether the imports would work
    -- implement in a Servlet class in the src.core => don't know how to access that from the browser or how to configure it to be accessible or if this is possible at all
    Maybe I am looking at the whole thing from the wrong side and the solution is something completely different from what I thought.
    I have looked for information on this for nearly 8 hours now but did not find anything that really helped me. So any help is really appreciated.
    Kind regards, Roland

    First, thanks for the links.
    But
    3.      Put the compiled servlet class in the private lib or classes folder.
    seems not very pratical to me as you always have an additional manual step to do after each and every little change and therefore another source for possible errors. Also you have to switch perspectives in Developer Studio to do the copying. Isn't there a way to use the already existing class files from the iView?
    Another question away from that: How do I access such a servlet via the web browser? What is the URL to it?
    Quite trivial, but didn't find information on that, too.
    Thanks anyway, Roland

  • Create jsf jsp page / page implementation / third option is not visible

    The third option (check box) for page implementation settings in the create jsf jsp page is not visible. The window is not resizable.

    I see the option radio button and text "Automatically Expose UI Components in an Existing Managed Bean" that is disabled but i see only partially the label what is a disabled input text field i suppose just below the radio button.
    I see also the Directory input field and Browse button a little bit truncated at the bottom (1-2 pixels missing).
    I see also the Render in Mobile Device check box and combo box ... cutted by the half when i expand the page implementation part.
    As said the window is not resizable.
    Let me know a location i can send you a hard copy of the screen.

  • How to implement isTokenValid in JSF ?

    Hi guys,
    How to implement a same function in JSF for the org.apache.struts.action.Action.isTokenValid in struts ? Please help, Thanks !
    cheers,
    Mark

    Put your question in a Struts forum.

  • How to implement content caching for jsp page ?

    Hello everyone,
    I am reading an article <Servlets and Jsp Best Practice>,at
    http://developer.java.sun.com/developer/technicalArticles/javaserverpages/servlets_jsp/#author, on one section it is saying :
    "Cache content: You should never dynamically regenerate content that doesn't
    change between requests. You can cache content on the client-side, proxy-
    side,or server-side. "
    Now I am working on a project. For every user, some of the content servlet generated will be the same for at least a week . I am thinking if I implement caching for these jsp pages, that would increase performace a lot.
    But I have no idea how to implement it either client-side or server-side, can someone give me a hint ?
    Thanks,
    Rachel

    You mean actually you are caching the response stream
    and the key to distinguish between different response
    streams are made of user's different request
    parameters. And the filter's function is to intercept
    the request to see if this request parameter's
    combination already exists in the Hashmap,then either
    use the cached response or forward to
    servlet.....really interesting...Do I get it right ?Yes that's it in a nutshell.
    >
    Then how do you build those response streams in
    advance ? You did it manually or have some mechanism
    to build it automatically ?
    The data gets cached the first time somebody visits the page.
    Find some examples on Filters, and take a look at the HttpServletResponseWrapper class. You need to cache response headers as well as the body. Another pitfall that you might run into is handling an If-modified-since header on the request. Don't cache the results of those requests.
    -Jonathan
    >
    Thanks again !
    Rachel

  • How to Implement Strut Tiles  Framework in JSF

    Hi
    I am new to JSF tried to implent struts tiles in jsf 1.2 but i am facing some problm.
    is any one have ideas about how to implementing struts tiles framework in jsf??....
    Cheers
    Sekar M

    Hope the below URLs should answer your question
    [http://www.jroller.com/HazemBlog/entry/how_to_use_struts_tiles]
    [http://www.ibm.com/developerworks/library/j-integrate/]
    [http://www.laliluna.de/blog/2007/02/28/struts_tiles_jsf_myfaces_migration_or_integration.html]

  • How to show a PDF with a JSF jsp.

    Hello,
    I want to show a PDF in a JSF jsp.
    I'm a newbie with this, and don't know where to look for information about that.
    Any example on the web ?
    Thanks in advance

    I think you will need to eventually get it to the HTML <object> tag anyway.
    <OBJECT data="yourPDFhere.pdf" type="????""/>
    and to get the byte[] to the client you may need to make a Servlet to respond to *.pdf requests and feed the correct session 'in memory' pdf out the stream.
    Here is an example from some svg 'in memory' per session I was testing:
    * SessionServlet.java
    * Created on June 3, 2004, 12:09 AM
    package com.hypernex.xml.svg;
    import java.io.*;
    import java.net.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    * @author  Roger I Martin, PhD
    * @version
    public class SessionServlet extends HttpServlet
        /** Initializes the servlet.
        public void init(ServletConfig config) throws ServletException
            super.init(config);
        /** Destroys the servlet.
        public void destroy()
        /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
         * @param request servlet request
         * @param response servlet response
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
            ServletContext context=getServletContext();
            java.util.Enumeration e=context.getServlets();
            while(e.hasMoreElements())
                System.out.println("servlets \t"+e.nextElement());
            /*e=context.getAttributeNames();
            while(e.hasMoreElements())
                System.out.println("AttributeNames \t"+e.nextElement());
            e=request.getParameterNames();
            while(e.hasMoreElements())
                System.out.println("ParameterNames \t"+e.nextElement());
            System.out.println("getContextPath "+request.getContextPath());
            System.out.println("getRequestURI"+request.getRequestURI());
            response.setContentType("text/xml+svg");
            PrintWriter out = response.getWriter();
            HttpSession httpSession=request.getSession();
            System.out.println("getQueryString"+httpSession);
            if(httpSession!=null && request.getRequestURI().endsWith("wafermap.svg"))
                /*e=httpSession.getAttributeNames();
                while(e.hasMoreElements())
                    System.out.println("httpSession AttributeNames \t"+e.nextElement());
                SVGProducer svgProducer=(SVGProducer)httpSession.getAttribute("waferMap");
                out.print(svgProducer.getSVGView());
            else if(httpSession!=null && request.getRequestURI().endsWith("hardware.svg"))
                SVGProducer svgProducer=(SVGProducer)httpSession.getAttribute("hardwareBean");
                out.print(svgProducer.getSVGView());
            else if(httpSession!=null && request.getRequestURI().endsWith("geometry.svg"))
                SVGProducer svgProducer=(SVGProducer)httpSession.getAttribute("geometryBean");
                out.print(svgProducer.getSVGView());
            else
                try
                    java.io.BufferedReader br=new java.io.BufferedReader(new java.io.FileReader("./webapps"+request.getRequestURI()));
                    String line=null;
                    while((line=br.readLine())!=null)
                        out.println(line);
                    br.close();
                catch(java.io.IOException ioe)
                    System.out.println("ioe:"+ioe.getMessage());
            out.close();
        /** Handles the HTTP <code>GET</code> method.
         * @param request servlet request
         * @param response servlet response
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
            processRequest(request, response);
        /** Handles the HTTP <code>POST</code> method.
         * @param request servlet request
         * @param response servlet response
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
            processRequest(request, response);
        /** Returns a short description of the servlet.
        public String getServletInfo()
            return "SVG graphics handler";
    }

  • How to use frames in a jsf page

    hi all,
    I need my page to be divided into three frames each frame with scrollable option. Each page should be able to display different jsp pages.Can anyone give a solution how can i implement this in a jsf page
    regards,
    Prasant

    Hello Prasant,
    I think page fragments is what your after, one of the Sun guys did a good blog on page layout with jspf but i can't access the weblogs, but you could try this tutorial in the meantime:
    http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/pagefragments.html
    Then search the weblogs when its available. i hope this helps
    Cheers
    Gaz

Maybe you are looking for

  • How can I insert clips in order?

    I am using 5.0.2. I have made an album of the photos that I want in my movie, and put them in the order I want. I would like to put in about 9 at a time, but iMovie does not keep them in the order I have. As an example-In iMovie I select photos 01,02

  • Setting the co-ordinates of a J componant?

    Is there any way to set the co-ordinates of a J componant? Such as place a button in the centre of a J Menu or place one at any place in there; What i need it for; Im making a helper for my 2d mmorpg, its for mappers, so they can enter different info

  • Help!!! HD video question, answers needed

    Hey guys. I just received a 5th generation iPod Touch as a Christmas gift, but I have one quick question about it. When playing videos on YouTube, how can I make them not HD? On my old iPod Touch (it was a 1st gen iPod) it didn't have a Retina Displa

  • Error trying to update apps

    When i try to update an application if i hit the "update" button what i get is the application starting instead of updating, how to solve? never happened before. Thank for the help

  • Need help opening project

    when I open adobe premiere 8 for a new project I olny get a blank screen, and when I try to open a  old project it said this project was been made with Premiere Pro and I create the file in premiere elemets 8