未知命令 - 弹出MVC

时间:2017-07-11 12:24:20

标签: java spring spring-mvc servlets spring-boot

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");
    }
}

我正在尝试在我们的Spring MVC / Boot项目中实现BotDetect Captcha。当我尝试使用注释创建servlet时(没有web.xml)我总是得到以下屏幕:screenshot of http://localhost:8080/kontakt

此外,当我编写此验证码的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>
&#13;
&#13;
&#13;

我该如何解决这个问题?

[BotDetect Captcha网站] [3]

2 个答案:

答案 0 :(得分:1)

你可以尝试:

  1. 扩展WebApplicationInitializer

    @Configuration
    public class WebXMLReplacement extends WebApplicationInitializer {
    
        //other configurations
    
        @Bean(name = "captchaServlet")
        public ServletRegistrationBean captchaServlet() {
    
            return new ServletRegistrationBean(new CaptchaServlet(), "/kontakt");
        }
    }
    
  2. 将bean定义移动到扩展WebApplicationInitializer的类中。

    function renderSelectedNode($node, $selectedNodes) {
        //dd($selectedNodes);
        $fullchild = [];
        $fulltext = $node->name;
        $fullid = $node->id;
        $fullparentid = $node->parent_id;
        $fullslug = $node->slug;
    
        if ( $node->children()->count() > 0 ) {
            foreach($node->children as $child){
                $fullchild[] = self::renderSelectedNode($child, $selectedNodes);
            }
        }
        $mainArray = ['text'=>$fulltext,'slug'=>$fullslug, 'id'=> $fullid  ,"parent_id"=> $fullparentid,'children'=>$fullchild];
    
        foreach($selectedNodes as $selectedNode){
            if($node->id == $selectedNode['category_id']){
                 $mainArray['state'] = ['selected'=>true];
            }
        }
        return $mainArray;
    
    }
    

答案 1 :(得分: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 同样,您可以通过创建新实现来注册侦听器和过滤器 WebApplicationInitializer。