尝试从应用程序的表中删除或编辑用户时出现转换错误。该值正在从元数据中传递listUser.xhmtl
到deleteUser.xhtml
页面。该值正在元数据中传递,但由于某种原因,在调用删除操作时,我得到了Conversion Error setting value "someemail@somedomain.com" for 'null Converter'
. 用户 ID 是一个字符串。
这是请求 userDelete.xhmtl 后的 url:
http://localhost:8080/lavpWebApp/user/deleteUser.xhtml?user=someemail%40somedomain.com
这是 userList.xhmtl 的简化:
<h:column>
<f:facet name="header">Edit</f:facet>
<h:link outcome="/user/editUser.xhtml" value="Edit User">
<f:param name="user" value="#{item.email}"/>
</h:link>
</h:column>
<h:column>
<f:facet name="header">Delete</f:facet>
<h:link outcome="/user/deleteUser.xhtml" value="Delete User">
<f:param name="user" value="#{item.email}"/>
</h:link>
</h:column>
这是 userDelete.xhtml 的简化:
<f:metadata>
<f:viewParam name="user" value="#{userController.user}" converter="#{userConverter}"/>
</f:metadata>
<h:body>
Do you want to delete #{userController.user.name}?
<h:form>
<h:commandButton action="#{userController.deleteUser()}"
value="Delete"/>
<h:commandButton action="#{userController.doCancelDeleteUser()}"
value ="Cancel"/>
</h:form>
</h:body>
这是转换器类:
@ManagedBean
@FacesConverter(value="userConverter")
public class UserConverter implements Converter{
@EJB
private UserSellerEJB userEjb;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.isEmpty())
{
return null;
}
if(!value.matches("\\d+"))
{
throw new ConverterException("The value is not a valid email: " + value);
}
String id = value.toString();
return userEjb.findUserById(id);
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if(value == null)
{
return null;
}
if(!(value instanceof UserSeller))
{
throw new ConverterException("The value is not a User: " + value);
}
String id = ((UserSeller) value).getEmail();
return (id != null) ? id.toString() : null;
}
}
这是 userController 类的简化:
@Named
@RequestScoped
public class UserController{
@EJB
private UserSellerEJB userEJB;
private UserSeller user = new UserSeller();
private List<UserSeller> usersList = new ArrayList<UserSeller>();
// ------------------------------------------------------------- Constructor
public UserController() {
}
// -------------------------------------------------------- Business Methods
public String doCreateUser()
{
user = userEJB.createSeller(user);
usersList = userEJB.findAllSellers();
return "listUser?faces-redirect=true";
}
// update user
public void PreRenderView()
{
if(user == null)
{
user = new UserSeller();
}
}
public String doUpdateUser()
{
if(user.getEmail() != null)
{
userEJB.updateSeller(user);
}
else
{
userEJB.createSeller(user);
}
return "listUser?faces-redirect=true";
}
public String deleteUser()
{
userEJB.deleteSeller(user);
return "listUser?faces-redirect=true";
}
public String doCancelDeleteUser()
{
return "listUser?faces-redirect=true";
}
@PostConstruct
public void init()
{
usersList = userEJB.findAllSellers();
}