2017-07-11 84 views
1
package de.gdv.sp.configuration; 

import org.springframework.boot.web.servlet.ServletRegistrationBean; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import com.captcha.botdetect.web.servlet.CaptchaServlet; 

@Configuration 

public class CaptchaConfiguration { 

    @Bean(name = "captchaServlet") 
    public ServletRegistrationBean captchaServlet() { 

     return new ServletRegistrationBean(new CaptchaServlet(), "/kontakt"); 
    } 
} 

我想實現BotDetect驗證碼在我們的Spring MVC /啓動project.When我試圖創建與批註(不web.xml)中的servlet我總是得到如下畫面:screenshot of http://localhost:8080/kontakt未知命令-Spring MVC

此外,當我編寫此驗證碼的HTML代碼時,我得到以下結果。 Botdetect Captcha does not show picture

<botDetect:captcha id="exampleCaptcha"/> 
 

 
<div class="validationDiv"> 
 
    <input id="captchaCode" type="text" name="captchaCode" 
 
      value="${basicExample.captchaCode}"/> 
 
    <input type="submit" name="submit" value="Submit" /> 
 
    <span class="correct">${basicExample.captchaCorrect}</span> 
 
    <span class="incorrect">${basicExample.captchaIncorrect}</span> 
 
</div>

我怎樣才能解決這個問題?

[BotDetect驗證碼網站] [3]

回答

0

還有就是註冊在Spring MVC應用程序自定義的servlet標準的方式。您需要通過實現WebApplicationInitializer來創建初始化器類。

import javax.servlet.ServletContext; 
    import javax.servlet.ServletException; 
    import javax.servlet.ServletRegistration.Dynamic; 
    import org.springframework.web.WebApplicationInitializer; 
    import com.captcha.botdetect.web.servlet.CaptchaServlet; 
    public class MyServletInitializer implements WebApplicationInitializer { 
     @Override 
     public void onStartup(ServletContext servletContext) 
     throws ServletException { 
      Dynamic myServlet = servletContext.addServlet("kontakt", CaptchaServlet.class); 
      myServlet.addMapping("/kontakt"); 
     } 
    } 

你可以用這種方法來註冊的DispatcherServlet manually.Similarly,你可以通過創建WebApplicationInitializer的一個新的實現 註冊監聽器和過濾器。

1

您可以嘗試兩種:

  1. 延長WebApplicationInitializer

    package de.gdv.sp.configuration; 
    
    import org.springframework.boot.web.servlet.ServletRegistrationBean; 
    import org.springframework.context.annotation.Bean; 
    import org.springframework.context.annotation.Configuration; 
    import com.captcha.botdetect.web.servlet.CaptchaServlet; 
    
    @Configuration 
    
    public class CaptchaConfiguration extends WebApplicationInitializer { 
    
        @Bean(name = "captchaServlet") 
        public ServletRegistrationBean captchaServlet() { 
    
         return new ServletRegistrationBean(new CaptchaServlet(), "/kontakt"); 
        } 
    } 
    
  2. 你的bean定義移動到擴展WebApplicationInitializer類。

    @Configuration 
    public class WebXMLReplacement extends WebApplicationInitializer { 
    
        //other configurations 
    
        @Bean(name = "captchaServlet") 
        public ServletRegistrationBean captchaServlet() { 
    
         return new ServletRegistrationBean(new CaptchaServlet(), "/kontakt"); 
        } 
    }