使用java表示法(Spring+Hibernate)将转换服务字符串添加到MyObject
我知道这已经被问了很多次了,但我真的无法找到一个有效的解决方案 由于这个问题,许多键盘和鼠标都被踩坏了…>: 我有一个@ManyToOne/@OneToMany关系:一个服务只有一个ServiceType,一个ServiceType可以关联到多个服务 问题1: 在我可以插入用thymeleaf创建的新服务的表单中,我有一个select,它允许我选择它的ServiceType,每个选项都有一个值ServiceType.id。 当我提交表单时,我得到转换错误: 无法将java.lang.String类型的属性值转换为属性ServiceType所需的com.lang.ServiceType类型;嵌套异常为java.lang.IllegalStateException:无法将[java.lang.String]类型的值转换为属性ServiceType所需的[com.destal.ServiceType]类型:未找到匹配的编辑器或转换策略 好的,我在StackOverflow上读到我需要一个转换器。我这样做:使用java表示法(Spring+Hibernate)将转换服务字符串添加到MyObject,java,spring,hibernate,Java,Spring,Hibernate,我知道这已经被问了很多次了,但我真的无法找到一个有效的解决方案 由于这个问题,许多键盘和鼠标都被踩坏了…>: 我有一个@ManyToOne/@OneToMany关系:一个服务只有一个ServiceType,一个ServiceType可以关联到多个服务 问题1: 在我可以插入用thymeleaf创建的新服务的表单中,我有一个select,它允许我选择它的ServiceType,每个选项都有一个值ServiceType.id。 当我提交表单时,我得到转换错误: 无法将java.lang.String
package com.lethal.converter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import com.lethal.model.ServiceType;
import com.lethal.service.ServiceTypeService;
//the second "Service" word refers to the interface with all add/edit/delete/get.. methods..
@Component
final class StringToServiceType implements Converter<String, ServiceType>
{
/**
* dependancy injection of the service
*/
@Autowired
private ServiceTypeService ServiceTypeService;
@Override
public ServiceType convert(String id)
{
/**
* check integer type
*/
try
{
Integer.parseInt(id);
/**
* creation of the object to return
*/
ServiceType service_type = ServiceTypeService.getServiceType(Integer.valueOf(id));
return service_type;
}
catch(NumberFormatException e)
{
System.out.println(e);
return null;
}
}
}
问题2每当我尝试导入StringToServiceType时,都会出现错误
com.converter.StringToServiceType类型不正确
可见的
好的。。。也许我必须把它定义为服务。。。由于我使用Java进行了配置,因此我在包中创建了ConversionConfig类,其中所有其他配置类都取自internet上的许多crud/身份验证教程:
package com.lethal.config;
import java.util.HashSet;
import java.util.Set;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import com.lethal.converter.*;
import com.lethal.converter.StringToServiceType;
/**
* in the line above, i keep getting the
* "type com.lethal.converter.StringToServiceType is not visible"
* error
*/
@Configuration
public class ConversionConfig {
@Bean(name="conversionService")
public ConversionService getConversionService() {
ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
bean.setConverters(getConverters());
bean.afterPropertiesSet();
ConversionService object = bean.getObject();
return object;
}
private Set<Converter> getConverters() {
Set<Converter> converters = new HashSet<Converter>();
converters.add(new StringToServiceType());
return converters;
}
}
问题1:有人对基于java配置的转换器有什么想法或教程吗
问题2:因为我将不得不为许多其他@ManyToMany/@OneToMany关系做同样的事情,例如:用户/地址、城市/州,。。。我要把其他转换器放在哪里,比如StringToState,StringToAddress
非常感谢我不知道怎么做,也不知道为什么,但我是这样决定的 资料来源: 实现转换的类:
老实说,我不明白为什么我必须添加addFormatters。。。没有这一点,它就不起作用了。。。bha在插入db:service.setId1之前,我尝试在控制器中手动设置id;ServiceService.addServiceservice;\u但我仍然得到uu无法将[java.lang.String]类型的值转换为所需的类型错误您必须将转换器类公开为final。谢谢@MartinFrey您的建议更正了问题2,但我仍然没有找到匹配的编辑器或转换策略错误。我只能找到xml配置的教程,但我使用的是java配置
package com.lethal.converter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import com.lethal.model.ServiceType;
import com.lethal.service.ServiceTypeService;
public final class StringToServiceTypeConverter implements Converter<String, ServiceType>
{
@Autowired
private ServiceTypeService ServiceTypeService;
@Override
public ServiceType convert(String id)
{
try
{
/**
* i try to get the object from the given ID
*/
Integer.parseInt(id);
ServiceType servizi_tipologia = ServiceTypeService.getServiceType(Integer.valueOf(id));
return servizi_tipologia;
}
catch(NumberFormatException e)
{
System.out.println(e);
return null;
}
}
}
package com.lethal.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.core.Ordered;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import com.lethal.converter.StringToServiceTypeConverter;
@Configuration
@EnableWebMvc
@ComponentScan("com.lethal")
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
// Maps resources path to webapp/resources
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
/**
* custom conversion
* es. from String (id) to ServiceType
*
* step 1
*/
@Bean
public StringToServiceTypeConverter getStringToServiceTypeConverter()
{
return new StringToServiceTypeConverter();
}
/**
* custom conversion
* es. from String (id) to ServiceType
*
* step 2
*/
@Override
public void addFormatters(FormatterRegistry formatterRegistry)
{
formatterRegistry.addConverter(getStringToServiceTypeConverter());
}
}