Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring框架-如何通过web表单修改服务属性_Java_Spring_Spring Mvc_Thymeleaf - Fatal编程技术网

Java Spring框架-如何通过web表单修改服务属性

Java Spring框架-如何通过web表单修改服务属性,java,spring,spring-mvc,thymeleaf,Java,Spring,Spring Mvc,Thymeleaf,我使用Spring4和Thymeleaf构建我的应用程序。我需要在会话中存储一些数据,所以我决定创建一个会话服务。现在我想通过web表单修改服务属性。我试着这样做: MyService接口 MyService实现 控制器类 景色 提交表格后,我给出了以下例外情况: 嵌套异常是 org.springframework.beans.BeanInstantiationException:无法 实例化bean类[com.webapp.service.MyService]:指定的类 是具有根本原因的接口]

我使用Spring4和Thymeleaf构建我的应用程序。我需要在会话中存储一些数据,所以我决定创建一个会话服务。现在我想通过web表单修改服务属性。我试着这样做:

MyService接口

MyService实现

控制器类

景色

提交表格后,我给出了以下例外情况:

嵌套异常是 org.springframework.beans.BeanInstantiationException:无法 实例化bean类[com.webapp.service.MyService]:指定的类 是具有根本原因的接口] org.springframework.beans.BeanInstantiationException:无法 实例化bean类[com.webapp.service.MyService]:指定的类 是一个接口


如果我编写MyService类而不是MyService接口,那么问题就会得到解决。但是上面的代码只是示例。在我的实际案例中,我使用了许多服务实现,所以我需要使用接口。

您不能创建接口实例,这就是它引发异常的原因。当spring尝试为每个bean声明调用工厂方法时,它失败了,因为您声明了接口而不是它的实现

例如:

<bean id= "myServiceImpl" class="some.package.MyServiceImpl"/>

问题解决了。我将postAction方法修改为以下形式:

public String postAction(WebRequest webRequest) {
    WebRequestDataBinder binder = new WebRequestDataBinder(service, "service");
    binder.bind(webRequest);
    return "redirect:/my_test_action";
}

现在它可以正常工作了:

MyService和MyServiceImpl在同一个包中吗?您还可以共享dispatcher-servlet.xml中使用组件扫描的部分吗?@Aditya Jain:是的,MyService和MyServiceImpl位于同一个包中。一般来说,我的应用程序运行良好。提交表单后会引发异常。由于您有可用的接口,请尝试使用ScopedProxyMode.INTERFACES而不是ScopedProxyMode.TARGET_CLASS@Aditya杰恩:问题还没有解决,但你们可能是对的。我使用接口,所以我应该使用ScopedProxyMode.INTERFACES,谢谢。您如何引导spring?有任何配置或xml吗?
@Controller
public class MyController {
    @Autowired
    private MyService service;

    @RequestMapping(value = "my_test_action", method = RequestMethod.GET)
    public String getAction(Model model) {
        model.addAttribute("service", service);
        return "my_view";
    }

    @RequestMapping(value = "my_test_action", method = RequestMethod.POST)
    public String postAction(MyService service) {
        return "my_view";
    }
}
<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
</head>
<body>

<form th:object="${service}" th:action="@{'/my_test_action'}" method="post">
    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
    <input th:name="title" th:value="*{title}"/>
    <input type="submit" />
</form>

</body>
</html>
<bean id= "myServiceImpl" class="some.package.MyServiceImpl"/>
public String postAction(WebRequest webRequest) {
    WebRequestDataBinder binder = new WebRequestDataBinder(service, "service");
    binder.bind(webRequest);
    return "redirect:/my_test_action";
}