我正在尝试将转换器用于输入表单,其中我使用了一些" selectOneMenu"组件。
当我尝试在转换器中获取我的控制器对象" getAsObject"方法使用:
facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "usersController");
我总是空着。 这是我的转换器类,它嵌套在UserController类中。
@FacesConverter(forClass = User.class, value="userConverter")
public static class UsersControllerConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
UserController controller = (UserController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "usersController");
System.out.println(value);
System.out.println(getKey(value));
System.out.println(controller);
return controller.getUsers(getKey(value));
}
java.lang.String getKey(String value) {
java.lang.String key;
key = value;
return key;
}
String getStringKey(java.lang.String value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof User) {
User o = (User) object;
return getStringKey(o.getUsername());
} else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + User.class.getName());
}
}
}
UserController类声明:
@Named("userController")
@ManagedBean
@SessionScoped
public class UserController implements Serializable{
PS:我是新手,我刚开始学习Java EE / Faces。
答案 0 :(得分:3)
此,
UserController controller = (UserController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "usersController");
确实不是正确的方法。以下是正确的方法:
UserController controller = facesContext.getApplication().evaluateExpressionGet(
facesContext, "#{usersController}", UsersController.class);