2012-05-08 35 views
0

目前,我的應用程序對我的所有控制器映射都使用SpringMVC。我試圖實現一個tinyMCE拼寫檢查,它包括一個Servlet,我不確定如何正確地正確集成而不修改此文件本身。我想避免修改,所以如果我們以後得到一個新版本,我們會好起來的。如何使用SpringMVC與ServletWrappingController集成GoogleSpellChecker

servlet的樣子......

public abstract class TinyMCESpellCheckerServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    private static Logger logger = Logger.getLogger(TinyMCESpellCheckerServlet.class.getName()); 

    private static final String MAX_SUGGESTIONS_COUNT_PARAM = "maxSuggestionsCount"; 
    private static final String PRELOADED_LANGUAGES_PARAM = "preloadedLanguages"; 

    private static final String DEFAULT_LANGUAGE = "en"; 
    private static final String GET_METHOD_RESPONSE_ERROR = "This servlet expects a JSON encoded body, POSTed to this URL"; 

    private static String DEFAULT_PRELOADED_LANGUAGES = "en-us"; 

    private enum methods { 
     checkWords, getSuggestions 
    } 

    private int maxSuggestionsCount = 25; 


    @Override 
    public void init() throws ServletException { 
     super.init(); 
     preloadSpellcheckers(); 
     readMaxSuggestionsCount(); 
    } 

    private void preloadSpellcheckers() throws ServletException { 
     String preloaded = getServletConfig().getInitParameter(PRELOADED_LANGUAGES_PARAM); 
     if (preloaded == null || preloaded.trim().length() == 0) { 
      preloaded = DEFAULT_PRELOADED_LANGUAGES; 
     } 

     String[] preloadedLanguages = preloaded.split(";"); 
     for (String preloadedLanguage : preloadedLanguages) { 
      try { 
       preloadLanguageChecker(preloadedLanguage); 
      } catch (SpellCheckException e) { 
       //wrong servlet configuration 
       throw new ServletException(e); 
      } 
     } 
    } 

    protected abstract void preloadLanguageChecker(String preloadedLanguage) throws SpellCheckException; 

    /** 
    * This method look for the already created SpellChecker object in the cache, if it is not present in the cache then 
    * it try to load it and put newly created object in the cache. SpellChecker loading is quite expensive operation 
    * to do it for every spell-checking request, so in-memory-caching here is almost a "MUST to have" 
    * 
    * @param lang the language code like "en" or "en-us" 
    * @return instance of SpellChecker for particular implementation 
    * @throws SpellCheckException if method failed to load the SpellChecker for lang (it happens if there is no 
    *        dictionaries for that language was found in the classpath 
    */ 
    protected abstract Object getChecker(String lang) throws SpellCheckException; 


    private void readMaxSuggestionsCount() throws ServletException { 
     String suggestionsCountParam = getServletConfig().getInitParameter(MAX_SUGGESTIONS_COUNT_PARAM); 
     if (suggestionsCountParam != null && suggestionsCountParam.trim().length() > 0) { 
      try { 
       maxSuggestionsCount = Integer.parseInt(suggestionsCountParam.trim()); 
      } catch (NumberFormatException ex) { 
       //wrong servlet configuration, possibly a typo 
       throw new ServletException(ex); 
      } 
     } 
    } 


    /** 
    * @see javax.servlet.Servlet#destroy() 
    */ 
    public void destroy() { 
     super.destroy(); 
     //remove unused objects from memory 
     clearSpellcheckerCache(); 
    } 

    protected abstract void clearSpellcheckerCache(); 


    /** 
    * GET method is not supported 
    * @see HttpServlet#doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse 
    *  response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     setResponeHeaders(response); 
     PrintWriter pw = response.getWriter(); 
     pw.println(GET_METHOD_RESPONSE_ERROR); 
    } 

    /** 
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 
    *  response) 
    */ 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     setResponeHeaders(response); 
     try { 
      JSONObject jsonInput = readRequest(request); 

      String methodName = jsonInput.optString("method"); 
      if (methodName == null || methodName.trim().equals("")) { 
       throw new SpellCheckException("Wrong spellchecker-method-name:" + methodName); 
      } 

      JSONObject jsonOutput = new JSONObject("{'id':null,'result':[],'error':null}"); 
      switch (methods.valueOf(methodName.trim())) { 
       case checkWords: 
        jsonOutput.put("result", checkWords(jsonInput.optJSONArray("params"))); 
        break; 
       case getSuggestions: 
        jsonOutput.put("result", getSuggestions(jsonInput.optJSONArray("params"))); 
        break; 
       default: 
        throw new SpellCheckException("Unimplemented spellchecker method {" + methodName + "}"); 
      } 

      PrintWriter pw = response.getWriter(); 
      pw.println(jsonOutput.toString()); 

     } catch (SpellCheckException se) { 
      logger.log(Level.WARNING, se.getMessage(), se); 
      returnError(response, se.getMessage()); 
     } catch (Exception e) { 
      logger.log(Level.WARNING, e.getMessage(), e); 
      returnError(response, e.getMessage()); 
     } 

     response.getWriter().flush(); 
    } 
..... 

回答

0

看看ServletWrappingControllerjavadoc

Controller實現一個包裝,它在內部管理一個servlet實例。這種封裝的servlet在這個控制器之外是不知道的;它的整個生命週期都在這裏介紹(與ServletForwardingController相比)。

用於通過Spring的調度基礎結構調用現有的servlet,例如將Spring HandlerInterceptors應用於其請求。

在您的Spring上下文中配置其中一個,使用您的TinyMCE servlet類名配置它,並使用SimpleUrlHandlerMapping進行映射。

ServletForwardingController也是有用的,但略有不同。

+0

你能舉一個例子嗎?我很努力地明白,這看起來像它的功能 – Webnet

+0

@Webnet:javadoc鏈接有一個例子。 – skaffman

+0

我已經採取了文件,並試圖建立一個配置。我遇到了一個錯誤,但我無法確定原因。你可以看看更新後的帖子,看看你是否可以提供任何指導? – Webnet

相關問題