Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring mvc Spring MVC转换器不';我根本不工作_Spring Mvc - Fatal编程技术网

Spring mvc Spring MVC转换器不';我根本不工作

Spring mvc Spring MVC转换器不';我根本不工作,spring-mvc,Spring Mvc,我对SpringMVC和JavaEE都是新手(我来自PHP+Zend2)。我的英语也很差。我使用NetBeans。 我的问题是,我的自定义转换器不工作。下面是一些代码: applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/200

我对SpringMVC和JavaEE都是新手(我来自PHP+Zend2)。我的英语也很差。我使用NetBeans。 我的问题是,我的自定义转换器不工作。下面是一些代码:

applicationContext.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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<bean id="universalDAO" class="dao.UniversalDAO"/>
<bean id="sessionManager" class="utils.SessionManager"/>

<bean id="idToEntityConverterFactory" class="utils.IdToEntityConverterFactory">
    <property name="dao" ref="universalDAO"/>
</bean>

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <ref bean="idToEntityConverterFactory" />
            <bean id="temp" class="utils.TempConverter" />
        </list>
    </property>
</bean>

<bean name="universalService" class="service.UniversalService">
    <property name="universalDAO" ref="universalDAO"/>
</bean>

<bean name="sessionApplicationService" class="service.SessionApplicationService">
    <property name="universalDAO" ref="universalDAO"/>
    <property name="sessionManager" ref="sessionManager"/>
</bean>

<bean name="systemUserApplicationService" class="service.SystemUserApplicationService">
    <property name="universalDAO" ref="universalDAO"/>
</bean>

<aop:aspectj-autoproxy />
<bean id="loggerAspect" class="aspect.LoggerAspect"/>

</beans>
IdToEntityConverterFactory是一个使用教程创建的转换器工厂,但它现在并不重要。我写了一篇更简单的文章,不要搞得一团糟

TempConverter.java

package utils;

import entity.Role;
import org.springframework.core.convert.converter.Converter;

public class TempConverter implements Converter<String, Role> {

    @Override
    public Role convert(String id) {
        return new Role();
    }
}
我找到了类似问题的解决办法。这和

<mvc:annotation-driven>
conversionService必须事先在Controller中设置,当然:

private ConversionService conversionService;
//...
public void setConversionService(ConversionService conversionService) {
    this.conversionService = conversionService;
}
调度程序servlet.xml

<bean class="controller.SystemUserFormController" p:applicationService-ref="systemUserApplicationService" p:sessionManager-ref="sessionManager" p:conversionService-ref="conversionService" />

它现在可以工作了,但有点不方便,因为:

  • 我必须在每个需要使用转换器的控制器中添加额外的代码p:conversionService ref=“conversionService”
  • 我在网上找到的每一个头像都是开箱即用的,但对我来说不是。我只是好奇我在做什么不同
最亲切的问候

你的问题: 我必须在每个需要使用转换器的控制器中添加额外的代码p:conversionService ref=“conversionService”

  • 您可以使用
    @Autowired
    注入
    ConversionService
  • 您可以使用
    @InitBinder
  • 您可以使用抽象父bean定义
我在网上找到的每一个头像都是开箱即用的,但对我来说不是。我只是好奇我在做什么不同

只需使用
。这个易于使用的配置就在那里,所以你不需要手动配置东西


怎么做 您可以实现。这个bean需要在处理程序适配器上设置

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer" ref="yourBindingInitializer" />
</bean>

但是,如果您还没有自己定义hanlder适配器,那么这种方法有点痛苦。当您定义这个bean时,它会禁用一些DispatcherServlet的默认行为。因此,您可能需要做的不仅仅是定义这个bean


离题建议
Spring的问题在于互联网上充斥着过时的教程。请使用官方指南和参考应用程序。开始使用名称空间(甚至Java)配置、自动连接、
@Controller
组件和
@RequestMapping

我相信没有mvc:annotation配置这样的配置。还有两件事:

  • 上下文:注释配置
  • mvc:注释驱动
如果我错了,请告诉我

我两个都试过了,但都不起作用。以下是我所做的:

  • 从我的控制器bean中删除了p:conversionService ref=“conversionService”
  • 在我的setter中添加了@Autowired注释

    @自动连线 public void setConversionService(ConversionService ConversionService){ this.conversionService=conversionService; }

  • 在applicationContext.xml中添加了context:annotation-config/(或mvc:annotation-driven/)

不幸的是,塞特从未被执行过

我的

Quote:“当Spring发现setter方法使用的@Autowired注释时,它会尝试对该方法执行byType自动关联。”


我还尝试过使用与bean类类型完全相同的setter-仍然没有任何结果。

mvc:annotation-config
是我答案中的一个输入错误,正确的是
mvc:annotation-driven
。此元素接受
conversion service=“conversionService”
属性。这就是你所需要的。无需手动进行活页夹初始化。
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    binder.setConversionService(conversionService);
}
private ConversionService conversionService;
//...
public void setConversionService(ConversionService conversionService) {
    this.conversionService = conversionService;
}
<bean class="controller.SystemUserFormController" p:applicationService-ref="systemUserApplicationService" p:sessionManager-ref="sessionManager" p:conversionService-ref="conversionService" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer" ref="yourBindingInitializer" />
</bean>