So basically my application is a 2 page application were on the frontage i will display all users in the users database which is connected through JPA and if you click in on an user you will be redirected to a new page with more information.
To get the users and user information I'm using an Entity class which is connected to my database and an class extending AbstractFacade to handle requests like create, edit, findAll, find and count.
I get two different errors that i would need help solving. And i would appreciate some information that could help me prevent these issues in the future as well.
For my front pages they currently look like this:
Index.xhtml
<h:body>
Hello from Facelets
<h1>#{indexManagedBean.helloMsg()}</h1>
<hr></hr>
<h1>Total # of users: #{userManagedBean.count()}</h1>
<h:dataTable value="#{userManagedBean.getAll()}" var="user">
<h:column>
<f:facet name="header">ID</f:facet>
#{user.id}
</h:column>
<h:column>
<f:facet name="header">Username</f:facet>
#{user.username}
</h:column>
<h:column>
<f:facet name="header">DisplayName</f:facet>
#{user.displayname}
</h:column>
<h:column>
<f:facet name="header">Email</f:facet>
#{user.email}
</h:column>
</h:dataTable>
<hr></hr>
<ul>
<ui:repeat value="#{userManagedBean.getAll()}" var="user">
<li>
<h:link value="View details of #{user.displayname}" outcome="user">
<f:param name="id" value="#{user.id}" />
</h:link>
</li>
</ui:repeat>
</ul>
</h:body>
user.xhtml
<f:metadata>
<f:viewParam name="id" value="#{userManagedBean.userId}" />
<f:viewAction action="#{userManagedBean.init()}" />
</f:metadata>
<h:head>
<title>User Details</title>
</h:head>
<h:body>
<h1># #{userManagedBean.userId}</h1>
<h:messages />
<h:panelGrid columns="2" rendered="#{not empty userManagedBean.user}">
<h:outputText value="ID" />
<h:outputText value="#{userManagedBean.user.id}" />
<h:outputText value="Name" />
<h:outputText value="#{userManagedBean.user.displayname}" />
<h:outputText value="Email" />
<h:outputText value="#{userManagedBean.user.email}" />
</h:panelGrid>
<h:link value="Back to all users" outcome="index" />
</h:body>
They both take their information from methods inside the userManagedBean which u all can guess is a managed bean. I have read about scopes and i have tried them all with this class but i cannot seem to get it to work. Now when using @RequestScoped on my managed bean its inconsistent because sometimes i get errors saying there is no active request. And if i switch to @ViewScoped the information from my Entity Manager class doesn't come up on the page.
Here is the error i get now and then using RequestScoped:
WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
Here is my UserManagedBean class:
@Named(value = "userManagedBean")
@RequestScoped
@ManagedBean
public class UserManagedBean {
@EJB
private UserEntityFacade userFacade;
String username;
String password;
String displayname;
String email;
String bio;
String websiteurl;
int followers;
int follows;
int projects;
UserEntity user;
private int userId;
/**
* Creates a new instance of UserManagedBean
*/
public UserManagedBean() {
}
public void init() {
user = userFacade.find(userId);
if (user == null) {
String message = "Bad request. Unknown user.";
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
}
}
/**
* Uses the UserEntityFacade to return all user records.
* @return
*/
public List<UserEntity> getAll(){
System.out.println("getall");
return userFacade.findAll();
}
/**
* Uses the UserEntityFacade to return the user with specified id.
* @param id - id of target user.
* @return the UserEntity of found user.
*/
public UserEntity getById(int id){
return userFacade.find(id);
}
/**
* Uses the UserEntityFacade to return the amount user records.
* @return
*/
public int count() {
return userFacade.count();
}
/**
* Uses the UserEntityFacade to delete specified user record.
*
* @param u - Target user record to be deleted.
* @return - null
*/
public String delete(UserEntity u) {
userFacade.remove(u);
return null; //Error logging ?
}
/**
* Uses the UserEntityFacade to insert a new user record.
*
* @return - An URL to be redirected to.
*/
public String add() {
UserEntity u = new UserEntity();
//u.setDisplayname(userBean.getDisplayName());
//u.setUsername(userBean.getUsername());
//u.setPassword(userBean.getPassword());
userFacade.create(u);
return "index";//Url ?
}
public void setUser(UserEntity u) {
this.user = u;
}
public UserEntity getUser(){
return this.user;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
}
Also quite often when developing i get this WELD error which i cannot find that much information about:
org.jboss.weld.exceptions.IllegalStateException: WELD-000227: Bean identifier index inconsistency detected - the distributed container probably does not work with identical applications Expected hash: 2084739097 Current index: BeanIdentifierIndex [hash=-907470349, indexed=12]: 0: WELD%AbstractBuiltInBean%com.ibm.jbatch.container.cdi.BatchCDIInjectionExtension%HttpSession 1: WELD%AbstractBuiltInBean%com.sun.faces.flow.FlowDiscoveryCDIExtension%HttpSession 2: WELD%AbstractBuiltInBean%org.glassfish.cdi.transaction.TransactionScopedContextExtension%HttpSession 3: WELD%AbstractBuiltInBean%org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider%HttpSession 4: WELD%AbstractBuiltInBean%org.glassfish.jersey.ext.cdi1x.servlet.internal.CdiExternalRequestScopeExtension%HttpSession 5: WELD%AbstractBuiltInBean%org.glassfish.jersey.ext.cdi1x.transaction.internal.TransactionalExceptionInterceptorProvider%HttpSession 6: WELD%AbstractBuiltInBean%org.glassfish.jms.injection.JMSCDIExtension%HttpSession 7: WELD%AbstractBuiltInBean%org.glassfish.osgicdi.impl.OSGiServiceExtension%HttpSession 8: WELD%AbstractBuiltInBean%org.glassfish.sse.impl.ServerSentEventCdiExtension%HttpSession 9: WELD%AbstractBuiltInBean%org.hibernate.validator.internal.cdi.ValidationExtension%HttpSession 10: WELD%AbstractBuiltInBean%root_web%HttpSession 11: WELD%AbstractBuiltInBean%web%HttpSession
If someone can notice a pattern of what i am doing wrong here please do help me, i haven't found that many good resources about JavaEE and JSF. Also if someone could explain to me what I'm doing wrong when it comes to the Scoped i would appreciate it a lot. Also if there is some more information needed to help me just tell me and i can post it!
EDIT: Holger posted a comment which solved the WELD error of inconsistency.
Try to reduce the annotations to @Named and @RequestScoped @Named and @ManagedBean are possibly conflicting. They are the same functionality, 1st for CDI, 2nd for JSF. Remember to not mix annotations of different technologies.