UserID Conversion Error Parameter Value for null Converter

I get a conversion error when I try to delete or change a user from a table in my application. The value is passed in metadata from the listUser.xhmtl page to deleteUser.xhtml . The value is passed in metadata, but for some reason, after calling the delete action, I get Conversion Error setting value " someemail@somedomain.com " for 'null Converter' . User ID is a string.

This is the URL after the userDelete.xhmtl request:

 http://localhost:8080/lavpWebApp/user/deleteUser.xhtml?user=someemail%40somedomain.com 

This is a simplification of 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> 

This simplifies 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> 

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 userController class is 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(); } 
+4
source share
1 answer

null converter in the exception message indicates that no converter instance was found. Since you refer to the converter as a managed bean on converter="#{userConverter} , you could only find it if it was annotated with @javax.faces.bean.ManagedBean , and if the class is in a WAR (and therefore , not located in the EJB / EAR or elsewhere!).

The @FacesConverter annotation @FacesConverter not used in this construct and is actually redundant. and only confused starters. Delete this annotation. @EJB only works in managed beans.

If it’s really in vain (I could not explain why), try to manage it using CDI: replace the JSF @ManagedBean annotation with the CDI @Named annotation. Apparently, you are already successfully managing the beans front controller using CDI.


Unrelated to a specific problem, the converter seems to be designed for conversion based on a technical identifier (primary database key), rather than an email address. An email address can never match \d+ (this is a regular expression for digits only). Make sure you do not confuse the email address with the identifier.

+5
source

Source: https://habr.com/ru/post/1445388/


All Articles