使用:带有 Liferay 5.2.3 和 Tomcat 6.0.18 的 Spring 3.2 portlet MVC
我正在尝试创建一个 PropertyEditor 来在 和 之间进行转换Set<Type>
,String
然后再返回。我已经成功地开始Set<Type>
工作String
,没有任何问题。但我无法让属性编辑器被识别为反向。我已经使用Type
-> String
->成功完成了这项工作Type
,但是为 a 进行转换Set
却让我望而却步。
我已经确定 SetAsText 方法从未被调用,并且我得到一个运行时错误,表明转换没有完成。关于 propertyEditors 的信息非常稀少,除了一两个例外,我能找到的唯一模糊相关的问题是 4 年或更长时间。
要么我错过了一些我看不到的基本内容,要么是框架中更深层次的东西,但我将不胜感激任何帮助或建议。
这是我的控制器中的@InitBinder 片段:
@InitBinder("formBacking")
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Set.class, "field", new SetEditor(listService, Set.class));
logger.info("FormBacking Binder initialization");
}
这是我的属性编辑器:
public class SetEditor extends PropertyEditorSupport {
protected static Logger logger = Logger.getLogger("PropertyEditor");
private ListService listService;
public SetEditor(ListService listService, Class clazz) {
super(clazz);
this.listService = listService;
}
@Override
public String getAsText() {
Stack<String> returnString = new Stack<String>();
Set<Type> types = new HashSet<Type>();
try {
types = (Set<Type>)this.getValue();
for (Type type:types) {
returnString.push(type.getTypeId().toString());
}
} catch (NullPointerException e) {
logger.info("getAsText is \"\"");
return "";
} catch (Exception e) {
logger.info("getAsText Other Exception: " + e.getMessage());
return "";
}
return "[" + StringUtils.collectionToDelimitedString(returnString,",") + "]"; // a very useful Spring Util
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
Type type = new Type();
Set<Type> result = new HashSet<Type>();
try {
String[] typeArray = text.split("[,]"); //this may not be correct, but I can't get here to debug it!!
for(String type:typeArray) {
if(!type.isEmpty()) {
type = listService.getType(Long.valueOf(text));
result.add(type);
}
}
}catch(NullPointerException e) {
logger.info("SetAsText is \"\" ");
setValue(null);
}
catch(Exception e) {
logger.info("setAsText Other Exception: " + e.getMessage());
}
setValue(result);
}
}
类型类片段是:
@Entity(name="Type")
@Table(name="type")
public class Type {
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "playListImages",
joinColumns={@JoinColumn(name="superTypeId")},
inverseJoinColumns={@JoinColumn(name="typeId")})
private Set<Type> types = new HashSet<Type>();
getters and setters...