I getting a Conversion Error when try delete or edit a user from a table in my application. The value is being passed in the metadata from the listUser.xhmtl to the deleteUser.xhtml page. The value is being passed in the metadata but for some reason upon calling the delete action I get the Conversion Error setting value "someemail@somedomain.com" for 'null Converter'. The user id is a String.
This is the url after requesting the userDelete.xhmtl:
http://localhost:8080/lavpWebApp/user/deleteUser.xhtml?user=someemail%40somedomain.com
This is the userList.xhmtl simplified:
<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>
This is userDelete.xhtml simplified:
<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>
This is the Converter class:
@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;
}
}
This is the userController class simplified:
@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();
}
The
null converterin the exception message indicates that the converter instance cannot be found. Since you’re referencing the converter as a managed bean byconverter="#{userConverter}, it would only be found if it’s annotated with@javax.faces.bean.ManagedBeanand if the class is located in the WAR (and thus not in the EJB/EAR or elsewhere!).The
@FacesConverterannotation isn’t been used in this construct and in fact superfluous — and only confusing to starters. Remove that annotation. The@EJBworks indeed only in managed beans.If really in vain (I could however not explain why), try managing it by CDI instead: replace the JSF
@ManagedBeanannotation by the CDI@Namedannotation. You’re apparently already successfully managing front controller beans by CDI.Unrelated to the concrete problem, the converter seems to be designed to convert based on a technical ID (a DB primary key), not an email address. An email address can never match
\d+(which is regex for “digits only”). Make sure that you’re not confusing email address with ID.