Announcement Announcement Module
Collapse
No announcement yet.
Implementing autocomplete to SpringApp Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Implementing autocomplete to SpringApp

    I'm working on a project which I want to add a autocomplete ajax function to. I've seen that there are some frameworks you can use like DWR, but I want to start simple, and just use a simple controller to send a response to the jspfile.

    In my code below I'm only only trying to return a simple textstring to the jsp-file. I figure if I get that working I can manage the rest of the autocomplete myself.

    Can you do it like in my code below?

    SimpleAjaxController
    Code:
    public class SimpleAjaxController implements Controller {
      
      public ModelAndView handleRequest(HttpServletRequest request, 
                 HttpServletResponse response) throws IOException {
        
        response.setContentType("text/xml");
        response.setHeader("Cache-Control", "no-cache");
        response.getWriter().write("Hello! How are you?");
        
        return null;
      }

    autocomplete.js

    Code:
    function doCompletion() {
      
      if (completeField.value == "") {
        clearTable();
      } else {
      var url = "/auto.do?action=complete&id=" + escape(completeField.value);
      var req = initRequest(url);
      req.onreadystatechange = function() {
        if (req.readyState == 4) {
          if (req.status == 200) {
            alert("Respons: " + req.responseText);
          } else if (req.status == 204){
          clearTable();
        }
      }
    Dispatcher-Servlet.xml
    Code:
    <bean id="urlMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
          <props>
            <prop key="/auto.do">SimpleAjaxController</prop>
          </props>
        </property>
      </bean>
    AjaxTest.jsp
    Code:
    <html>
      <head>
        <form name="autofillform" action="autocomplete" method="get">
          <input type="hidden" name="action" value="lookupbyname"/>
          <table border="0" cellpadding="5" cellspacing="0">
            <tr>
              <td><b>Employee Name:</b></td>
              <td>
                <input    type="text"
                          size="20"
                          autocomplete="off"
                          id="complete-field"
                          name="id"
                          onkeyup="doCompletion();">
              </td>
              
          </table>
        </form>
      </body>
    </html>

  • #2
    Just looking at the controller code only, you're missing a few things. Here's what I use for writing AJAX responses -

    response.setContentType("application/json");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setHeader("Pragma", "no-cache");
    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentLength(content.length());
    response.getOutputStream().write(content.getBytes( ));
    response.getOutputStream().flush();

    Change content type as required.


    Andy.

    Comment


    • #3
      Thaks a lot!!!

      If you want to use a framework, which one is the better?

      Comment


      • #4
        Which AJAX framework is better depends upon your requirements of course.

        I use script.aculo.us with prototype (http://www.prototypejs.org/) and use JSON (http://www.json.org/) to send complex data from the server to the browser.

        Andy.

        Comment


        • #5
          Thanks! I'll try them out!!

          When you're implementing the AJAX-functionality, what kind of pattern do you use?
          Are you using a MultiActionController in Spring that takes care of similar tasks?

          Comment

          Working...
          X