我的转换器有问题 - 它停止工作,什么都没发生。 我有实体类“组”:
@Entity
@Table(name="users_group")
public class Group implements Serializable {
private int id;
private String name;
private boolean active = true;
private String code;
private List<User> users = new ArrayList<User>();
// getters, setters
@Override
public String toString() {
return "Group.id="+getId();
}
@Override
public boolean equals(Object obj){
System.out.println("OBJ :"+obj);
if(obj == null )return false;
if(obj instanceof String){
if(obj.toString().equals(this.toString())){
return true;
}
}
if(obj instanceof Group){
Group objGroup = (Group) obj;
if(objGroup.getId() == this.getId()){
System.out.println("EUREKA! Found");
return true;
}
}
return false;
}
}
这是我的jsf视图代码:
<p:selectOneMenu id="group" value="#{priviligeMB.groupPrivilige.group}" converter="#{groupConverter}" >
<f:selectItems value="#{priviligeMB.groups}"
var="group" itemLabel="#{group.name}" itemValue="#{group}" />
</p:selectOneMenu >
当然还有GroupConverter类:
@Component("groupConverter")
public class GroupConverter implements Converter {
@Autowired
GroupService groupService;
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
// arg2 is "Group.id=x" where x is just an id, for example "Group.id=2"
try {
System.out.println("getasObj: "+arg2);
return groupService.getGroupById(Integer.parseInt(arg2.split("=")[1]));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
// arg2 is object with toString() method that prints: "Group.id=x"
System.out.println("getAsString: "+arg2);
return arg2.toString();
}
}
这是我的日志:
> getAsString: Group.id=1
> getAsString: Group.id=2
> Hibernate: SELECT locale.code from messages_locale locale left join users_user u sers on
> users.locale_id = locale.id where users.username =?
> getasObj: Group.id=1
> Hibernate: select group0_.id as id0_0_, group0_.active as
> active0_0_, group0_.co de as code0_0_, group0_.name as name0_0_ from
> users_group group0_ where group0_. id=?
> OBJ :Group.id=1 EUREKA! Found
> OBJ :Group.id=1 EUREKA! Found
> OBJ :Group.id=2
之后......什么都没发生!没有错误,没有操作,没有页面重新加载,没有消息......
答案 0 :(得分:0)
我遇到了类似的问题,最终解决了。
在这种情况下,您需要确保“ getAsObject ”返回的对象可以匹配"#{priviligeMB.groups}"
中的一个对象,此处“匹配”意思是同一个对象。