java - 异常 : "No adapter for handler. Does your handler implement a supported interface like controller?"

标签 java spring hibernate jsp hibernate-validator

我正在尝试使用 HibernateValidator 使用 Spring 和 Hibernate 在 JSP 中验证一个简单的表单. JSP页面Temp.jsp如下(web.xml中的url pttern为*.htm)。

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<form:form method="post" action="Temp.htm" commandName="validationForm">
     <!--validationForm is a model class-->
<table>
    <tr>
        <td>User Name:<font color="red"><form:errors path="userName" /></font></td>
    </tr>

    <tr>
        <td><form:input path="userName" /></td>
    </tr>

    <tr>
        <td>Age:<font color="red"><form:errors path="age" /></font></td>
    </tr>

    <tr>
        <td><form:input path="age" /></td>
    </tr>

    <tr>
        <td>Password:<font color="red"><form:errors path="password" /></font></td>
    </tr>

    <tr>

    <td><form:password path="password" /></td>

    </tr>

    <tr>
        <td><input type="submit" value="Submit" /></td>
    </tr>
    </table>

</form:form>

validationForm如下。

package validators;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;

final public class ValidationForm
{
    @NotEmpty
    @Size(min = 1, max = 20)
    private String userName;
    @NotNull
    @NumberFormat(style = Style.NUMBER)
    @Min(1)
    @Max(110)
    private Integer age;
    @NotEmpty(message = "Password must not be blank.")
    @Size(min = 1, max = 10, message = "Password must between 1 to 10 Characters.")
    private String password;

    public void setUserName(String userName)
    {
            this.userName = userName;
    }

    public String getUserName()
    {
            return userName;
    }

    public void setAge(Integer age)
    {
            this.age = age;
    }

    public Integer getAge()
    {
            return age;
    }

    public void setPassword(String password)
    {
            this.password = password;
    }

    public String getPassword()
    {
            return password;
    }
}

和应该处理验证的 Controller 类如下(我正在使用 SimpleFormController)。

package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import usebeans.TempService;
import validators.ValidationForm;

@SuppressWarnings("deprecation")
final public class Temp extends SimpleFormController 
{
    private TempService tempService=null;
    public Temp()
    {
        //setCommandClass(Temp.class);
        //setSuccessView("Temp");
        //setFormView("Temp");

        setCommandClass(ValidationForm.class); //Still not working.
        setCommandName("validationForm");
    }   

    public void setTempService(TempService tempService)
    {
        this.tempService = tempService;
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception
    {
        ModelAndView mv=new ModelAndView();
        ValidationForm validationForm=(ValidationForm) command;
        tempService.add(validationForm);
        return mv;
    }

    @Override
    protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors) throws Exception
    {
        ModelAndView mv=new ModelAndView();
        return mv;
    }
}

dispatcher-servlet 中,我添加了以下内容。

<bean id="tempService" class="usebeans.TempServiceImpl" />
<bean name="/Temp.htm" class="controller.Temp" p:tempService-ref="tempService" p:formView="Temp" p:successView="Temp" />

也尝试添加以下 bean 仍然没有运气。

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

其中TempService接口(interface)如下。

package usebeans;

import validators.ValidationForm;

public interface  TempService
{
    public void add(ValidationForm validationForm);
}

下面是 TempServiceImpl 类。

package usebeans;

import validators.ValidationForm;

final public class TempServiceImpl implements TempService
{
    public void add(ValidationForm validationForm)
    {
        System.out.println("Message");
    }
}

虽然 TempServiceImpl 类实现了 TempService 接口(interface),但我遇到了以下异常。

javax.servlet.ServletException: No adapter for handler [usebeans.TempServiceImpl@ab3cba]: Does your handler implement a supported interface like Controller?
at org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:982)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:770)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:619)

编辑:

尽管我遵循解释的内容 here ,问题仍然存在,我遇到了与上述相同的异常。我在这里缺少哪些配置设置。它可能位于 dispatcher-servlet.xml 文件中。整个dispatcher-servlet.xml文件如下。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"

       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

        <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>    
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />


        <bean id="tempService" class="usebeans.TempServiceImpl" />
        <bean name="/Temp.htm" class="controller.Temp" p:tempService-ref="tempService" p:formView="Temp" p:successView="Temp" />


        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="index.htm">indexController</prop>                
                    <prop key="Temp.htm">tempService</prop>                
                </props>
            </property>
        </bean>

        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/jsp/"
              p:suffix=".jsp" />


        //The index controller.

        <bean name="indexController"
              class="org.springframework.web.servlet.mvc.ParameterizableViewController"
              p:viewName="index" />

</beans>

我对抛出的异常没有任何确切的想法。您能弄清楚为什么会抛出该异常吗?我在这里缺少哪些配置设置或其他内容?

最佳答案

我已经解决了这个问题,请找到新的 xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <bean
        class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
    <bean
        class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />


    <bean id="tempService" class="usebeans.TempServiceImpl" />
    <bean id="tempController" class="controller.Temp"/>


    <bean id="urlMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
                <prop key="Temp.htm">tempController</prop> <!-- You need to mapp the url to the controller bean-->
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

    <bean name="indexController"
        class="org.springframework.web.servlet.mvc.ParameterizableViewController"
        p:viewName="index" />

</beans>

修复与 <prop key="Temp.htm">tempController</prop> 有关.

对于第二个错误更改类 Temp如下

package controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import usebeans.TempService;
import validators.ValidationForm;

@SuppressWarnings("deprecation")
final public class Temp extends SimpleFormController {
    private TempService tempService = null;

    public Temp() {
        // setCommandClass(Temp.class);
        // setSuccessView("Temp");
        // setFormView("Temp");

        setCommandClass(ValidationForm.class); // Still not working.
        setCommandName("validationForm");
    }

    public void setTempService(TempService tempService) {
        this.tempService = tempService;
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {
        ModelAndView mv = new ModelAndView();
        ValidationForm validationForm = (ValidationForm) command;
        tempService.add(validationForm);
        return mv;
    }

    @Override
    protected ModelAndView showForm(HttpServletRequest request,
            HttpServletResponse response, BindException errors)
            throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put(getCommandName(), new ValidationForm());
        ModelAndView mv = new ModelAndView("Temp", model);
        return mv;
    }
}

关于java - 异常 : "No adapter for handler. Does your handler implement a supported interface like controller?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11093818/

相关文章:

java - 从方法中获取实例

java - 简单形式的 Wicket AjaxFallbackButton(更新 : also Button)

java - 为什么不能使用 JPA 将列设置为 null?

hibernate - 使用非事务性 EJB "read"方法是否是一种好的做法?

java - 是什么导致我的 GWT 应用程序出现 HTTP 503 错误?

java:如何使用bufferedreader读取特定行

java - Android Studio java-library 模块类路径NoClassDefFoundError

java - 如何在Spring Integration中配置多个网关?

java - 测试时为类提供了错误类型的 id

java - 多线程共享一个套接字