Reputation: 1593
I have a Model:
@Entity
@Table(name = "User")
public class User implements Serializable {
private static final long serialVersionUID = 546951187473649176L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(min = 3, max = 25)
@Pattern(regexp = "[A-Za-z ]*", message = " wroooooong")
private String name;
/** some setters and getters **/
}
I have a UserController.java
@ManagedBean(name = "userController")
@SessionScoped
public class UserController implements Serializable {
private static final long serialVersionUID = -4210819542193607967L;
@EJB
private UserProvider userProvider;
@Inject
EntityManager entityManager;
@PostConstruct
public void initNewUser() {
user = new User();
}
private User user;
public void registerUser() throws ValidatorException {
try {
userProvider.registerUser(new User(),user);
} catch (Exception e) {
throw new ValidatorException(new FacesMessage(e.getMessage()));
}
}
private String name;
/** some setters and getters **/
}
The UserProvider looks like this:
@RequestScoped
@Stateful
public class UserProvider {
@Inject
private EntityManager entityManager;
public void registerUser (User newUser, String name) throws Exception {
try {
newUser.setName(name);
entityManager.persist(newUser);
} catch (Exception e){
throw e;
}
}
}
and, finally my register.xhtml:
<h:form>
<label for="name">name</label>
<h:inputText id="name" value="#{userController.name}" required="true" />
<h:message id="register_name_error" for="name" errorClass="invalid"/>
<h:commandButton action="#{userController.registerUser()}" id="submit" value="send" />
</h:form>
When i enter something to short in the name field, i got this in my stack:
javax.validation.ConstraintViolationException: Validation failed for classes [de.liedl.bachelor.model.User] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='muss zwischen 3 und 25 liegen', propertyPath=accountName, rootBeanClass=class de.demotest.model.User, messageTemplate='{javax.validation.constraints.Size.message}'}
]
but the error is never thrown back to my <h:message
What is wrong?
I downloaded a demo-application, where it worked totally easy, but this was little bit different, without provider and controller and model, it was just the the model and a mix-class...
I also tried this with just throw exception and with and without try catch
which Validator do i need in the UserProvider?
Upvotes: 1
Views: 1018
Reputation: 1108762
You need to bind the field to a property of the model, not to a property of the controller.
<h:inputText id="name" value="#{userController.user.name}" required="true" />
Unrelated to the concrete problem, throwing a ValidatorException
inside an action method makes no sense. You have 2 options:
Let it go so that it end up in container's default error page or a custom one definied in web.xml
.
Create and add a faces message yourself by FacesContext#addMessage()
.
Further I also wonder how it's useful to have a @Stateful
service in this particular case. It could be as good just @Stateless
. That @RequestScoped
on an EJB is completely unnecessary and only confusing. That whole try-catch
and throws Exception
in the service method is also completely unnecessary. That @Inject
won't work at all in side a @ManagedBean
. The one inside the EJB must be @PersistenceContext
instead. You're completely mixing JSF/CDI/EJB annotations. Please be very careful with this.
Upvotes: 2