I am getting the null Converter error for what I thought was a very simple scenario:
<!-- My View -->
<ui:composition template="/template/template_v1.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<!-- Simplified for clarity -->
<h:form>
<div class="block-panel customer-data">
<h:outputLabel for="txtUsername">Username:</h:outputLabel>
<h:inputText id="txtUsername" name="Username"
value="#{userBean.user.id}"
styleClass="text" />
<rich:message id="errorUsername" for="txtUsername"/>
</div>
<!-- Other fields omitted for clarity -->
</h:form>
/* The User Bean - simplified */
#ManagedBean
#ViewScoped
public class UserBean implements Serializable {
private User user;
public User getUser() {
// Contains logic for reading a user from the database or creating a new
// user object
return user;
}
public void setUser(User user) {
this.user = user;
}
}
/* The user Entity - Simplified */
#Entity
#Table(name = "user")
#Inheritance(strategy = InheritanceType.JOINED)
#DiscriminatorColumn(name = "user_type", discriminatorType = DiscriminatorType.STRING)
public class User implements IEntity<String>, Serializable {
private static final long serialVersionUID = 1L;
private String id;
#Id
#Column(name = "username", length = 50)
#NotNull(message = "{userIdMandatory}")
#Size(max = 50)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
}
/* The IEntity interface */
public interface IEntity<ID extends Serializable> {
ID getId();
void setId(final ID pId);
}
So essentially I'm trying to bind a string property of my user entity to a inputText field. As far as I'm concerned there should be no need for a conversion so I shouldn't be getting the error I'm seeing.
Interestingly, if I add the following getter and setter to my entity:
public String getTmpId() {
return this.id;
}
public void setTmpId(String id) {
this.id = id;
}
And then make the necessary changes to my view to bind to tmpId rather than id, everything works as expected.
This seems like a bug to me either to do with the fact that I am binding to a getter/setter defined in an interface, defined in a generic interface or because the getter is marked with the Id attribute. I would appreciate someone else's ideas however.
As an aside, I have inherited this design and don't particularly like it so I may just end up refactoring it to introduce a new username property rather than trying to use the Id.
To the best of my knowledge I believe this is caused by an obscure bug in BeanELResolver which is used to get the type of the property being bound to - rather than returning String it is returning Serializable, for which there is no converter and hence the error I am seeing.
It's not particularly elegant, but I have worked around this by adding a userId property to my userBean and then binding to that in my view instead of the Id property on the user entity. I then use that value to set the Id manually on the entity when I save it:
<!-- My New View -->
<ui:composition template="/template/template_v1.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<!-- Simplified for clarity -->
<h:form>
<div class="block-panel customer-data">
<h:outputLabel for="txtUsername">Username:</h:outputLabel>
<h:inputText id="txtUsername" name="Username"
value="#{userBean.userId}"
styleClass="text" />
<rich:message id="errorUsername" for="txtUsername"/>
</div>
<!-- Other fields omitted for clarity -->
/* The New User Bean - simplified */
#ManagedBean
#ViewScoped
public class UserBean implements Serializable {
private string userId;
private User user;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public User getUser() {
// Contains logic for reading a user from the database or creating a new
// user object
return user;
}
public void setUser(User user) {
this.user = user;
}
public void saveUser() {
if (user.getId() == null) {
user.setId(userId);
}
// Actual saving omitted for brevity
}
}
Related
I've made small jsf app and a bit confused about lifecycle order, i'm getting unexpected NPE on postback even though i'm creating that object on every request. Can someone explain what's happening under the covers. Here is the code:
Entity.java
public class Entity {
private Long id;
private String property;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
}
Bean.java
import javax.enterprise.inject.Model;
#Model
public class Bean {
private Long id;
private Entity entity;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Entity getEntity() {
return entity;
}
public void loadEntity() {
this.entity = new Entity();
}
}
edit.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:o="http://omnifaces.org/ui">
<f:view transient="true">
<f:metadata>
<f:viewParam name="id" value="#{bean.id}"/>
<f:viewAction onPostback="true" action="#{bean.loadEntity()}"/>
</f:metadata>
<h:body>
<o:form useRequestURI="true">
<h:inputText value="#{bean.entity.property}"/>
<h:commandButton value="Save"/>
</o:form>
</h:body>
</f:view>
</html>
Action methods like <f:viewAction action> are invoked during invoke application phase. The model values are updated during update model values phase. So, the entity is created one phase too late and still null when property needs to be set.
Get rid of the <f:viewAction> and make it a #PostConstruct method instead.
#PostConstruct
public void init() {
this.entity = new Entity();
}
I try to use JSF in combination with Bean Validation. Basically, everything works well, the validation works as expected, I get the correct message, but there is an exception on my Glassfish console:
Warnung: EJB5184:A system exception occurred during an invocation on EJB MyEntityFacade, method: public void com.mycompany.testbv.AbstractFacade.create(java.lang.Object)
Warnung: javax.ejb.EJBException
at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:748)
....
....
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)
Caused by: javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'. Please refer to embedded ConstraintViolations for details.
This exception occurs if I use custom constraints as well as predefined constraints.
Here is my sample code.
Sample Entity:
#Entity
#ValidEntity
public class MyEntity implements Serializable {
private static final long serialVersionUID = 3104398374500914142L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Size(min = 2)
private String name;
public MyEntity(String name) {
this.name = name;
}
public MyEntity() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Custom constraint:
#Constraint(validatedBy = MyValidator.class)
#Target({FIELD, METHOD, TYPE})
#Retention(RUNTIME)
public #interface ValidEntity {
String message() default "fail";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Custom validator:
public class MyValidator implements ConstraintValidator<ValidEntity, MyEntity>{
#Override
public void initialize(ValidEntity a) {
}
#Override
public boolean isValid(MyEntity t, ConstraintValidatorContext cvc) {
return false;
}
}
Sample Controller:
#Named
#SessionScoped
public class MyController implements Serializable {
private static final long serialVersionUID = -6739023629679382999L;
#Inject
MyEntityFacade myEntityFacade;
String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public void saveNewEntity() {
try {
myEntityFacade.create(new MyEntity(text));
} catch (Exception e) {
Throwable t = e;
while (t != null) {
if (t instanceof ConstraintViolationException) {
FacesContext context = FacesContext.getCurrentInstance();
Set<ConstraintViolation<?>> constraintViolations = ((ConstraintViolationException) t).getConstraintViolations();
for (ConstraintViolation<?> constraintViolation : constraintViolations) {
FacesMessage facesMessage = new FacesMessage(constraintViolation.getMessage());
facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
context.addMessage(null, facesMessage);
}
}
t = t.getCause();
}
}
}
}
Sample jsf page:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head></h:head>
<h:body>
<h:form>
<h:messages id="messages" />
<h:inputText value="#{myController.text}" />
<h:commandButton value="Save" action="#{myController.saveNewEntity()}" />
</h:form>
</h:body>
</html>
The MyEntityFacade only calls persist from entity manager.
As mentioned before, the application is running fine and the correct messages are shwon, but I want to avoid this exception in the Glassfish console.
Setting the validation mode in persistence.xml to NONE as discussed here is no option, because I want a validation.
I use JSF in version 2.2, the implementation is Mojarra. The version of Bean Validation is 1.1, the implementation is Hibernate Validator.
Application Server is Glassfish 4.0.
Class-level constraints do not work with JSF. Take a look at this answer. When you press the 'Save' button JSF checks only if name has at least 2 chars and does not take into account the ValidEntity constraint. JPA, on the other hand, complains that the bean is not valid and throws an exception.
UPDATE
1) the #Size constraint is on MyEntity.name property while in the facelet you have MyController.text property. In the JSF perspective there is nothing to validate. It has no knowledge of the MyEntity at all.
2) ValidEntity is always invalid, so JPA will always throw the exception (unless you disable validation) even if you properly set the MyEntity.name in the facelet.
I'm trying to create a custom component for displaying an Entity with a certain form. So I've created my #FacesComponent and he's working but only when he is not inside a loop like <ui:repeat>. When I'm using the following code, my component is displaying null values for price and photo but not for name. Do you have an explaination ?
XHTML code :
<ui:define name="content">
<f:view>
<h:form>
<ui:repeat value="#{dataManagedBean.listNewestCocktails}" var="item" varStatus="status">
<h:outputText value="#{item.price}"/> <!--working very well-->
<t:cocktailVignette idPrefix="newCocktails" name="foo" price="#{item.price}" urlPhoto="#{item.photoURI}"/> <!-- not working the getPrice here -->
</ui:repeat>
<!--<t:cocktailVignette idPrefix="allCocktails" name="OSEF" price="20" urlPhoto="osefdelurl" ></t:cocktailVignette> -->
</h:form>
</f:view>
My component code :
package component;
import java.io.IOException;
import javax.faces.context.FacesContext;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.ResponseWriter;
#FacesComponent(value = "CocktailVignette")
public class CocktailVignette extends UIComponentBase {
private String idPrefix;
private String name;
private String price;
private String urlPhoto;
public String getIdPrefix() {
return idPrefix;
}
public void setIdPrefix(String idPrefix) {
this.idPrefix = idPrefix;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getUrlPhoto() {
return urlPhoto;
}
public void setUrlPhoto(String urlPhoto) {
this.urlPhoto = urlPhoto;
}
#Override
public String getFamily() {
return "CocktailVignette";
}
#Override
public void encodeBegin(FacesContext context) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.write("<div id=\""+idPrefix+name+"\" class=\"cocktail-vignette\">");
writer.write("<h2>"+name+"</h2>");
writer.write("<h3>"+price+"</h3>");
writer.write("</div>");
}
}
Thanks a lot :) I'm trying but nothing is working ...
All of component's attributes which are sensitive to changes in state (e.g. the value being dependent on <ui:repeat var>, at least those which is not known during view build time but during view render time only), must delegate the storage of attribute value to the state helper as available by inherited getStateHelper() method.
Kickoff example:
public String getPrice() {
return (String) getStateHelper().eval("price");
}
public void setPrice(String price) {
getStateHelper().put("price", price);
}
Apply the same for all other attributes and get rid of the instance variable declarations. Important note is that the state helper key ("price" in above example) must be exactly the same as attribute name.
See also:
How to save state when extending UIComponentBase
I am working in seam with jsf1.2.
Everything working fine for me, except the search operation. Can anyone please suggest me to go in a right way?
Here, how I am having my form:
<h:form class="input-list" id="searchUser" style="width:100%;"
name="searchUser">
<div class="edit-label">FIRST NAME :</div>
<h:inputText tabindex="1" id="firstName" type="text" class="miniusername clp"
value="#{userListAction.firstName}" required="true">
<f:validateLength minimum="3" maximum="20" />
</h:inputText>
<h:commandButton value="Search" tabindex="2" style="margin-left: 5px"
action="#{userListAction.retrieveName}" class="usersearch">
</h:commandButton>
</h:form>
My interface:
#Local
public interface UserListAction extends Serializable {
public List<User> retrieveCustomers();
public List<User> retrieveEmployees();
public List<User> getUsersList();
public CLRPUser getLoginUser();
public List<User> retrieveName();
#WebRemote
public String deleteById(Integer userId);
#WebRemote
public CLRPUser getUserById(Integer userId);
public UserTypeEnum getUserType();
public void setUserType(UserTypeEnum userType);
public void setFirstName(String firstName);
public String getFirstName();
public CLRPUser getCurrentUser();
public void setCurrentUser(CLRPUser currentUser);
}
Action class which implements the interface:
#Name("userListAction")
#Stateless
#AutoCreate
public class UserListActionImpl implements UserListAction {
#In
protected UserService userService;
#Out(value = "userType", scope = ScopeType.CONVERSATION, required = false)
private UserTypeEnum userType;
#In
private LoggedInUser loggedInUser;
#Out(value = "currentUser", scope = ScopeType.CONVERSATION, required = false)
#In(value = "currentUser", scope = ScopeType.CONVERSATION, required = false)
private CLRPUser currentUser;
#In(value = "firstName", scope = ScopeType.CONVERSATION, required = false)
private String firstName;
/**
*
*/
private static final long serialVersionUID = 8649412602585430272L;
/*
* (non-Javadoc)
*
* #see com.ermms.clrp.user.UserAction#getUsersList()
*/
public List<User> retrieveName() {
System.out.print("FirstName is :" + firstName);
return userService.getAllUsers(firstName);
}
public UserTypeEnum getUserType() {
return this.userType;
}
public void setUserType(UserTypeEnum userType) {
this.userType = userType;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
#Override
public CLRPUser getCurrentUser() {
// TODO Auto-generated method stub
return null;
}
#Override
public void setCurrentUser(CLRPUser currentUser) {
// TODO Auto-generated method stub
}
}
Console says the First name is : null.
I tried more times, but lost my mind in this. Please suggest me.
Why do you inject the firstName in the userListAction-component? Where should this come from?
If there is nothing more, I guess it works as follows:
user enters first name and clicks the command button
the form gets submitted and the entered first name is set into the model using the setter-method
when executing the command button-action Seam-bijection takes place and injects the value for the firstName stored in "some" context. Since it is not defined anywhere, null is injected.
So if you need the injection (for any case), you should put the correct value in the contest at any place. If not, just remove the #In annotation.
I try to bring the property of one ManagedBean (SessionScoped) (loginBean) to another (RequestScoped) ManagedBean (newsBean):
<c:set value="#{loginBean.user.nickname}" target="#{newsBean}" property="author" />
<h3>News verfassen:</h3>
<h:form>
<p:inputText label="Titel" value="#{newsBean.title}"></p:inputText>
<p:inputTextarea label="Inhalt" value="#{newsBean.description}"></p:inputTextarea>
<p:commandLink type="button" value="Absenden" action="#{newsBean.writeMsg}"></p:commandLink>
</h:form>
I thought that I can use the c:set tag like in the case I want to set a attribute "staticly".. :)
Maybe someone can help me?
Domii
----- AFTER ANSWER ------
LoginBean:
#ManagedBean
#SessionScoped
public class LoginBean {
public LoginBean() {
email = "";
pwd = "";
device = "";
user = null;
}
private String email;
private String pwd;
private User user;
private String device;
/**
* #return the user
*/
public User getUser() {
return user;
}
/**
* #param user the user to set
*/
public void setUser(User user) {
this.user = user;
}
And NewsBean:
public class NewsBean {
#ManagedProperty("#{loginBean.user.nickname}")
private String nickname;
private String title;
private String description;
/**
* Creates a new instance of NewsBean
*/
public NewsBean() {
}
And yes i also declare the getter /setter for nickname in user!
Just use #ManagedProperty.
#ManagedBean
#ViewScoped
public class NewsBean {
#ManagedProperty("#{loginBean.user.nickname}")
private String author;
// ...
}