I'm quite new to JEE6 & JSF in specific, so I know I'm lacking some knowledge to get this running. I was trying to get in work for over two days know - it's desperating...
What is my problem: I have a user, skills and level, e.g. "Frank (The Cook)" has skill "Cooking" with level "expert". Therefore I try to create a form where a user can manage his skills. I can manage to display a skill with according level. The level is a selected value of a SelectOneMenu. When I change the value and submit the form it simply does nothing and I don't know why :-(
My code so far:
profile.xhtml
<h5>General skills</h5>
<h:panelGroup>
<h:form>
<h:dataTable value="#{userBean.userskills}" var="userskill">
<h:column>
<f:facet name="header">Skill</f:facet>
<span><h:outputText value="#{userskill.skill.description}" /></span>
</h:column>
<h:column>
<f:facet name="header">Level</f:facet>
<span>
<h:selectOneMenu value="#{userskill.level}" >
<f:selectItem itemValue="#{null}"
itemLabel="#{msg.ChooseOption}"
noSelectionOption="true" />
<f:selectItems value="#{userskill.skill.levels}"
var="level"
itemValue="#{level}"
itemLabel="#{level.description}" />
<f:converter converterId="levelConverter" />
</h:selectOneMenu>
</span>
</h:column>
</h:dataTable>
<h:commandButton value="#{msg.Save}"
action="#{userBean.testAction}" />
</h:form>
</h:panelGroup>
UserBean.java
@ManagedBean
@SessionScoped
public class UserBean implements Serializable {
private static final long serialVersionUID = 1L;
private User user;
@ManagedProperty(value="#{authBean}")
private AuthenticationBean authBean;
@EJB IUserDao userDao;
@EJB ICategoryDao categoryDao;
@EJB IUserSkillLevelDao uskDao;
List< UserSkillLevel > userskills;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void setAuthBean(AuthenticationBean authBean) {
this.authBean = authBean;
}
public List<UserSkillLevel> getUserskills() {
return userskills;
}
public void setUserskills(List<UserSkillLevel> userskills) {
this.userskills = userskills;
}
public String getProfile(Long userId) {
// is called when clicking on the "Show user profile" link on an other page
User auth_user = this.authBean.getUser();
if ( auth_user.getId() == userId ) {
this.user = auth_user;
} else {
this.user = userDao.findById(userId);
}
this.userskills = findUserskills(null, false);
return "/pages/profile?faces-redirect=true";
}
public List< UserSkillLevel > findUserskills(Category category, boolean skillIsWithStandardLevelSet) {
if ( category == null ) {
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("description", "General");
category = categoryDao.findOneByConditions(Category.FIND_BY_DESCRIPTION, parameters);
}
return uskDao.findByUserAndCategory(this.user, category, skillIsWithStandardLevelSet);
}
public String testAction() {
System.out.println("-------- worked --------");
return "";
}
}
It all works fine. The level (from DB) is displayed on startup, the converter is converting vise-versa and the SelectOneMenu contains the level specified for that skill.
I think the commandButton is not working because of the automatically generated SelectOneMenus. Am I right? When debugging my code I found out that userBean.userskills.level changed from initial value to null but I couldn't figure out if it contains the selected value after changing it.
As soon as I remove the SelectOneMenu the commandButton calls testAction as intended.
What am I doing wrong? I'm missing something but I don't know what :-( Is there an easier way to accomplish what I want? In addition: I have to stick with plain JSF, that means no use of Primefaces, Omnifaces, etc.
Greetings, Stefi
-- UPDATE --
I added a h:message to my SelectOneMenu and encountered a "Value not valid"-Error;
trying to figure out why it appears...
-- UPDATE --
IT'S WORKING ;-) Just for those who are desperated like I was: I just forgot to implement a equals method for my entity!!!