Seam not injecting Stateful EJB dependencies - java

I have not been able to find a solution to this problem and my post to the Seam forum has gone unanswered. I am hope someone here can help.
I am a noob working with Seam, so I am sure I am just doing something stupid. Any help anyone can give me would be greatly appreciated... I have wasted hours and gotten nowhere. I used the jboss tools to generate a seam project in eclipse. The tool took pre-created JPA entities and created a basic webapp. My first step was to add a registration page so that I can create some users and login using database authentication (I set this up, but will test it once the registration page works). I used the seam-booking example to guide me (basically integrating the form stuff plus additional fields into the view of the seam-gen app). When I test the registration page, I get NullPointerExceptions for all of the injected fields. I have looked through the entire seam-booking example, scoured the web looking at examples, and quickly read through some sections of a book and I do not see where there is any additional configuration information needed. What in the world am I doing wrong? Please help!!!
I am using JBoss Server (community edition) 5.1.0GA and Seam 2.2.0GA.
If you need any more information than what I am posting, please let me know. Thanks to all ahead of time for your help!!
Stateful EJB:
#Stateful
#Scope(EVENT)
#Name("register")
public class RegisterAction implements Register {
#In
private User user;
#PersistenceContext
private EntityManager entityManager;
#In
private FacesMessages facesMessages;
private String verify = null;
private boolean registered = false;
public void registerUser() {
if (user.getPassword().equals(verify)) {
List existing = entityManager
.createQuery(
"select u.userName from User u where u.userName=#{user.userName}")
.getResultList();
if (existing.size() == 0) {
entityManager.persist(user);
facesMessages
.add("Successfully registered as #{user.userName}");
registered = true;
} else {
facesMessages.addToControl("userName",
"Username #{user.userName} already exists");
}
} else {
facesMessages.addToControl("verify", "Re-enter your password");
verify = null;
}
}
public void invalid() {
facesMessages.add("Please try again");
}
public boolean isRegistered() {
return registered;
}
public String getVerify() {
return verify;
}
public void setVerify(String verify) {
this.verify = verify;
}
#Remove
#Destroy
public void destroy() {
}
}
EJB local interface:
#Local
public interface Register
{
public void registerUser();
public void invalid();
public String getVerify();
public void setVerify(String verify);
public boolean isRegistered();
public void destroy();
}
XHTML of registratin page:
<ui:define name="body">
<rich:panel>
<f:facet name="header">Register</f:facet>
<h:form id="registration">
<fieldset><s:decorate id="firstNameDecorate"
template="layout/edit.xhtml">
<ui:define name="label">First Name:</ui:define>
<h:inputText id="firstName" value="#{user.firstName}"
required="true">
<a:support id="onblur" event="onblur" reRender="firstNameDecorate" />
</h:inputText>
</s:decorate> <s:decorate id="lastNameDecorate" template="layout/edit.xhtml">
<ui:define name="label">Last Name:</ui:define>
<h:inputText id="lastName" value="#{user.lastName}" required="true">
<a:support id="onblur" event="onblur" reRender="lastNameDecorate" />
</h:inputText>
</s:decorate> <s:decorate id="emailDecorate" template="layout/edit.xhtml">
<ui:define name="label">Email:</ui:define>
<h:inputText id="emailAddress" value="#{user.emailAddress}"
required="true">
<a:support id="onblur" event="onblur" reRender="emailDecorate" />
</h:inputText>
</s:decorate> <s:decorate id="usernameDecorate" template="layout/edit.xhtml">
<ui:define name="label">Username:</ui:define>
<h:inputText id="username" value="#{user.userName}" required="true">
<a:support id="onblur" event="onblur" reRender="usernameDecorate" />
</h:inputText>
</s:decorate> <s:decorate id="passwordDecorate" template="layout/edit.xhtml">
<ui:define name="label">Password:</ui:define>
<h:inputSecret id="password" value="#{user.password}"
required="true" />
</s:decorate> <s:decorate id="verifyDecorate" template="layout/edit.xhtml">
<ui:define name="label">Verify Password:</ui:define>
<h:inputSecret id="verify" value="#{register.verify}"
required="true" />
</s:decorate>
<div class="buttonBox"><h:commandButton id="register"
value="Register" action="#{register.registerUser}" />   <s:button
id="cancel" value="Cancel" view="/index.xhtml" /></div>
</fieldset>
</h:form>
</rich:panel>
</ui:define>
</ui:composition>
Template XHTML (Registration page uses this):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a="http://richfaces.org/a4j"
xmlns:s="http://jboss.com/products/seam/taglib"
contentType="text/html">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>MyApp</title>
<link rel="shortcut icon" href="#{request.contextPath}/favicon.ico"/>
<a:loadStyle src="resource:///stylesheet/theme.xcss"/>
<a:loadStyle src="/stylesheet/theme.css"/>
<ui:insert name="head"/>
</head>
<body>
<ui:include src="menu.xhtml">
<ui:param name="projectName" value="MyApp"/>
</ui:include>
<div class="body">
<h:messages id="messages" globalOnly="true" styleClass="message"
errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"
rendered="#{showGlobalMessages != 'false'}"/>
<ui:insert name="body"/>
</div>
<div class="footer">
<p>Powered by Seam #{org.jboss.seam.version} and RichFaces. Generated by seam-gen.</p>
<s:fragment rendered="#{init.debug}">
<a:log hotkey="D"/>
<p style="margin-top: -0.5em;">
Conversation: id = #{conversation.id}, #{conversation.longRunning ? 'long running' : 'temporary'}#{conversation.nested ? ', nested, parent id = '.concat(conversation.parentId) : ''}
#{' - '}
Ajax4jsf Log (Ctrl+Shift+D)
#{' - '}
<s:link id="debugConsole" view="/debug.xhtml" value="Debug console" target="debugConsole"/>
#{' - '}
<s:link id="resetSession" view="/home.xhtml" action="#{org.jboss.seam.web.session.invalidate}" propagation="none" value="Terminate session"/>
</p>
</s:fragment>
</div>
</body>
</html>
</f:view>
Seam Components.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<components xmlns="http://jboss.com/products/seam/components"
xmlns:core="http://jboss.com/products/seam/core"
xmlns:persistence="http://jboss.com/products/seam/persistence"
xmlns:drools="http://jboss.com/products/seam/drools"
xmlns:bpm="http://jboss.com/products/seam/bpm"
xmlns:security="http://jboss.com/products/seam/security"
xmlns:mail="http://jboss.com/products/seam/mail"
xmlns:web="http://jboss.com/products/seam/web"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd
http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.2.xsd
http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.2.xsd
http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.2.xsd
http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.2.xsd
http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.2.xsd
http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd">
<core:init debug="true" jndi-pattern="#jndiPattern#"/>
<core:manager concurrent-request-timeout="500"
conversation-timeout="120000"
conversation-id-parameter="cid"
parent-conversation-id-parameter="pid"/>
<!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
<web:hot-deploy-filter url-pattern="*.seam"/>
<persistence:managed-persistence-context name="entityManager" auto-create="true"
persistence-unit-jndi-name="java:/MyAppEntityManagerFactory"/>
<drools:rule-base name="securityRules">
<drools:rule-files>
<value>/security.drl</value>
</drools:rule-files>
</drools:rule-base>
<security:rule-based-permission-resolver security-rules="#{securityRules}"/>
<security:identity-manager identity-store="#{jpaIdentityStore}" />
<security:jpa-identity-store
entity-manager="#{entityManager}" user-class="my.app.path.dao.profiles.User"
role-class="my.app.path.dao.profiles.Role" />
<event type="org.jboss.seam.security.notLoggedIn">
<action execute="#{redirect.captureCurrentView}"/>
</event>
<event type="org.jboss.seam.security.loginSuccessful">
<action execute="#{redirect.returnToCapturedView}"/>
</event>
<mail:mail-session host="localhost" port="25"/>
</components>

A quick answer as I'm on the rush:
To address the Seam question first, is "User" a Seam component that will be auto-created or is there a factory method to create one? Annotating a field with #In is just one half of what's required, you still need the other end which supplies the value.
In the bigger picture:
presuming User is an entity, having it as a Seam component is not a good practice (way too much overhead caused by Seam).
your stateful bean is scoped as an EVENT. This is unlikely to be your desire, the EVENT scope in Seam is the same as a request for a servlet.
See if you can get a copy of "Seam in Action", it explains the fundamentals very well.

Related

Partial Page Rendering (primefaces) need two times click before changing

I'm trying to apply this answer to rendering a part of my primefaces page but the problem is that I need to click twice before getting the part changed is there a solution ?
this is my code
index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
</h:head>
<h:body>
<h:panelGroup id="header" layout="block">
<h1>Header</h1>
</h:panelGroup>
<h:panelGroup id="menu" layout="block">
<h:form>
<f:ajax render=":content">
<p:commandButton value="next" action="#{navBean.next}" />
<p:commandButton value="back" action="#{navBean.back}" />
</f:ajax>
</h:form>
</h:panelGroup>
<h:panelGroup id="content" layout="block">
<h:panelGroup rendered="#{navBean.activePage == 'firstAjax'}">
<ui:include src="firstAjax.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{navBean.activePage == 'lastAjax'}">
<ui:include src="lastAjax.xhtml" />
</h:panelGroup>
</h:panelGroup>
</h:body>
</html>
and this is my managedBean
package com.project.beans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean(name = "navBean")
#SessionScoped
public class NavBean {
private String activePage = "firstAjax";
public String getActivePage() {
return activePage;
}
public void setActivePage(String activePage) {
this.activePage = activePage;
}
public void next() {
System.out.println("next in " + activePage);
this.setActivePage("lastAjax");
System.out.println("next out " + activePage);
}
public void back() {
System.out.println("back in " + activePage);
this.setActivePage("firstAjax");
System.out.println("back out " + activePage);
}
}
the firstAjax.xhtml and the lastAjax.xhtml is nearly the same
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:form>
<h2>content first</h2>
</h:form>
</ui:composition>
Your page is not working because PrimeFaces has its own ajax engine. You have to use it when using p:commandButton. You don't need to use p:ajax tag, because p:commandButton has ajax behaviour activated by default, just use the update attribute (equivalent to the render attribute of f:ajax component):
<h:form>
<p:commandButton value="next" action="#{navBean.next}" update=":content"/>
<p:commandButton value="back" action="#{navBean.back}" update=":content"/>
</h:form>
If you want to stick to plain jsf use this instead (it looks much more like the example by BalusC you mentioned):
<h:form>
<f:ajax render=":content" >
<h:commandButton value="next" action="#{navBean.next}" />
<h:commandButton value="back" action="#{navBean.back}" />
</f:ajax>
</h:form>
As a final note, it must be said that p:ajax tag (at least in PrimeFaces 4.0) doesn't support the syntax we are using here, outside the p:commandButton components, because it's not able to determine the event attribute and therefore it throws: <p:ajax> Event attribute could not be determined: null

JSF 1.2 h:commandLink not working, mojarra is not defined

I got a problem with JSF 1.2 on websphere 7.
I'm trying to implement a navigation with JSF.
I'm also using RichFaces.
In the console of the browser I got this error: Uncaught ReferenceError: mojarra is not defined
I have seen other with this problem and they solved it with adding <h:head> but this didn't work for me.
Thank you for helping
My navigation.xhtml
<?xml version="1.0" ?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets">
<body>
<ui:composition>
<h:form>
<!-- Navigation -->
<div id="navigation" class="navigation">
<div class="level0">
<h:commandLink action="#{navigation.toHome}" value="#{messages.elsi_dg_navigation_home}" />
</div>
<div class="level0">
<h:commandLink action="#{navigation.toHistory}" value="#{messages.elsi_dg_navigation_history}" />
</div>
</div>
</h:form>
</ui:composition></body></html>
The NavigationBean.java
#Controller("navigation")
#Scope("session")
#ManagedBean
public class NavigationBean {
private String lastSite = "home";
public String toHome() {
return this.processOutcome("home");
}
public String toHistory() {
return this.processOutcome("history");
}
public String toCurrentPage() {
return this.lastSite;
}
private String processOutcome(String outcome) {
this.lastSite = outcome;
return outcome;
}
}
HTML output:
<div id="navigation" class="navigation">
<form id="j_id19" name="j_id19" method="post" action="/elsi-dg/xhtml/home.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_id19" value="j_id19">
<!-- Navigation -->
<div id="navigation" class="navigation">
<div class="level0">Home</div>
<div class="level0">History</div>
</div><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="3428583661652779493:-8865896015881316410" autocomplete="off">
</form>
</div>
UPDATE
<a4j:commandLink> works without problems..
Maybe the problem is with the tag ui:compostion
<ui:composition template="./template/template.xhtml">
It normally needs a template file.
Are you using one?
If you aren't, try to remove the <ui:composition> tag.

JSF - App design issue. How to get data in backing bean A, after user logs in to backing bean B

I'm trying to load some data after a user logs in to my app. I need the username to load data specific to the user. The problem I'm having is that I have a SessionScoped backing bean that contains the code to log the user in, and then I have a ViewScoped page specific backing bean (which injects the SessionScoped bean) which is to load the data for the page.
Currently this is the code that I have and I get a null pointer when loading the data because the username doesn't exist when the loadData() method is called.
I'm having a hard time coming up with a solution to this problem as I'd like to NOT have to put the login dialog on every page and keep it in the template.xhtml file if possible.
template.xhtml
...
<!-- Login Dialog -->
<p:dialog id="loginDialog" header="Login" widgetVar="loginWidget" modal="true" visible="#{!accessBacking.hasAccess}" closable="false">
<h:form id="loginForm">
<p:messages id="loginFormMessages" severity="error" autoUpdate="true" showDetail="true" />
<h:panelGrid columns="2" cellspacing="10" width="300">
<p:outputLabel for="username" value="Username" />
<p:inputText id="username" value="#{accessBacking.username}" required="true" requiredMessage="Username is Required" />
<p:outputLabel for="password" value="Password" />
<p:password id="password" value="#{accessBacking.password}" required="true" requiredMessage="Password is Required" />
<h:panelGroup></h:panelGroup>
<h:panelGroup>
<p:commandButton value="Login" styleClass="ui-priority-primary" actionListener="#{accessBacking.checkViewAccess}" oncomplete="handleAuthenticationRequest(xhr, status, args)" update="loginFormMessages" />
</h:panelGroup>
</h:panelGrid>
</h:form>
</p:dialog>
...
userGroups.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:body>
<f:metadata>
<f:event type="preRenderView" listener="#{userGroupBacking.setCurrentMenu}" />
</f:metadata>
<ui:composition template="/templates/template.xhtml">
<p:dataTable var="user" value="#{userGroupBacking.users}" editable="true" id="userTable">
...
AccessBacking.java
#ManagedBean(name="accessBacking")
#SessionScoped
public class AccessBacking {
private String username;
private String password;
public boolean checkViewAccess() {
Access access = new Access();
if(access.authenticate(username, password)) {
// user is logged in
}
}
}
UserGroupBacking
#ManagedBean(name="userGroupBacking")
#ViewScoped
public class UserGroupBacking {
#ManagedProperty(value="#{accessBacking}")
private AccessBacking accessBacking;
public void setAccessBacking(AccessBacking accessBacking) {
this.accessBacking = accessBacking;
}
#PostConstruct
public void init() {
loadData();
}
/**
* Loads the data for the page
*/
public void loadData() {
Dao dao = new Dao(ds);
users = dao.findAllUsers(accessBacking.getUsername(), accessBacking.getRoles()); // NULL POINTER BECAUSE ACCESSBACKING.GETUSERNAME() IS NULL SINCE THE USER HASN'T LOGGED IN YET.
}
}
I've figured out a good solution by using ui:param. Basically I set a ui:param with my backing bean value in my page specific xhtml and then reference that ui:param in my template.
Updated code from question with new solution:
template.xhtml
...
<!-- NOTE the 'myBacking' instead of the 'userGroupBacking' -->
<!-- Login Dialog -->
<p:dialog id="loginDialog" header="Login" widgetVar="loginWidget" modal="true" visible="#{!myBacking.hasAccess}" closable="false">
<h:form id="loginForm">
<p:messages id="loginFormMessages" severity="error" autoUpdate="true" showDetail="true" />
<h:panelGrid columns="2" cellspacing="10" width="300">
<p:outputLabel for="username" value="Username" />
<p:inputText id="username" value="#{accessBacking.username}" required="true" requiredMessage="Username is Required" />
<p:outputLabel for="password" value="Password" />
<p:password id="password" value="#{accessBacking.password}" required="true" requiredMessage="Password is Required" />
<h:panelGroup></h:panelGroup>
<h:panelGroup>
<p:commandButton value="Login" styleClass="ui-priority-primary" actionListener="#{myBacking.checkViewAccess}" oncomplete="handleAuthenticationRequest(xhr, status, args)" update="loginFormMessages" />
</h:panelGroup>
</h:panelGrid>
</h:form>
</p:dialog>
...
userGroups.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:body>
<f:metadata>
<f:event type="preRenderView" listener="#{userGroupBacking.setCurrentMenu}" />
</f:metadata>
<!-- THIS IS PART OF THE SOLUTION -->
<ui:param name="myBacking" value="#{userGroupBacking}" />
<ui:composition template="/templates/template.xhtml">
<p:dataTable var="user" value="#{userGroupBacking.users}" editable="true" id="userTable">
...
AccessBacking.java
#ManagedBean(name="accessBacking")
#SessionScoped
public class AccessBacking {
private String username;
private String password;
public boolean checkViewAccess() {
Access access = new Access();
if(access.authenticate(username, password)) {
// user is logged in
}
}
}
UserGroupBacking
#ManagedBean(name="userGroupBacking")
#ViewScoped
public class UserGroupBacking {
#ManagedProperty(value="#{accessBacking}")
private AccessBacking accessBacking;
public void setAccessBacking(AccessBacking accessBacking) {
this.accessBacking = accessBacking;
}
#PostConstruct
public void init() {
loadData();
}
// CHECKS AGAINST ACCESSBACKING
public boolean isHasAccess() {
return accessBacking.isHasAccess();
}
// CHECKS AGAINST ACCESSBACKING
public boolean checkViewAccess() {
return accessBacking.checkViewAccess();
}
/**
* Loads the data for the page
*/
public void loadData() {
Dao dao = new Dao(ds);
users = dao.findAllUsers(accessBacking.getUsername(), accessBacking.getRoles());
}
}

ViewScoped Constructor being called before save method in JSF 2.1.16

Related to my JSF application, I noticed there's a problem with the Mojarra JSF 2.1.16 library. I have a ViewScoped bean which loads a user from a login got as ViewParam. After that loaded user data can be managed and saved. Below is the view code, where I have skiped the main form fields as I have tested there's no problem with them.
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui"
template="/templates/general_template.xhtml">
<ui:define name="metadata">
<f:metadata>
<f:viewParam id="user" name="user"
value="#{manager._ParamUser}" />
<f:event type="preRenderView"
listener="#{manager.initialize}" />
</f:metadata>
</ui:define>
<ui:define name="general_content">
<p:outputPanel autoUpdate="false" id="loggedData" name="loggedData"
layout="block">
<h:form id="SystemUserForm">
<h:panelGrid columns="4" cellspacing="10px" style="border-color:red;">
<h:outputText value="#{msg.LOGIN}:" />
<h:outputText value="#{manager._UserBean._login}" />
</h:panelGrid>
<p:commandButton value="#{msg.UPDATE}" action="#{manager.actionSave}"
ajax="false" />
<p:commandButton value="#{msg.CANCEL}"
action="#{manager.actionCancelSave}" ajax="false" />
</h:form>
</p:outputPanel>
</ui:define>
At the beginning bean is created and User itself is loaded from data base using received param. Problem comes when I call the action method to save it, because the ViewScoped bean called manager is being constructed again. So there's no param and I have a null pointer Exception. That's working properly with Mojarra 2.1.14 and 2.1.15.
Backing bean code:
#ManagedBean
#ViewScoped
public class Manager extends UserData {
public static final String PARAM_USER = "ParamUser";
private String _ParamUser;
public String get_ParamUser() {
return this._ParamUser;
}
public void set_ParamUser(String _ParamUser) {
this._ParamUser = _ParamUser;
}
public Manager() {
super();
}
#Override
public void initialize(ComponentSystemEvent event) {
if (!FacesContext.getCurrentInstance().isPostback()) {
loadUserBean(this._ParamUser);
if (this._UserBean == null) {
redirectTo404();
}
}
}
#Override
public String actionSave() {
super.actionSave();
return NavigationResults.USER_LIST;
}
UserData is, of course, an abstract class. When actionSave() is called bean is constructed again and there is no _ParamUser attribute, because this is get by viewParam. That constructor recall is only happening with Mojarra 2.1.6.
This issue has been solved in Mojarra JSF 2.1.17, tried and tested. Could be a problem with Mojarra JSF 2.1.16 and Tomcat 6. However, I haven't found any known issues for that version.

Wired component null in seam EntityHome action

I have a custom EntityHome class. I wire the dependent entity in the wire method, but when I call the action (persist) the wired component is always null.
What could be the reason, similar code generated by seam gen is apparently working.
Here is the entity class.
The round.teeSet component is always null when persist is called, although it is shown in the jsf page.
Note: Seam version: 2.2.0.GA Jboss:
jboss-5.1.0.GA
I have overrden persist method to log the value of the wired element.
#Name("roundHome")
#Scope(ScopeType.CONVERSATION)
public class RoundHome extends EntityHome<Round>{
#In(required = false)
private Golfer currentGolfer;
#In(create = true)
private TeeSetHome teeSetHome;
#Override
public String persist() {
logger.info("Persist called");
if (null != getInstance().getTeeSet() ) {
logger.info("teeSet not null in persist");
} else {
logger.info("teeSet null in persist");
// wire();
}
String retVal = super.persist(); //To change body of overridden methods use File | Settings | File Templates.
return retVal;
}
#Logger
private Log logger;
public void wire() {
logger.info("wire called");
TeeSet teeSet = teeSetHome.getDefinedInstance();
if (null != teeSet) {
getInstance().setTeeSet(teeSet);
logger.info("Successfully wired the teeSet instance with color: " + teeSet.getColor());
}
}
public boolean isWired() {
logger.info("is wired called");
if(null == getInstance().getTeeSet()) {
logger.info("wired teeSet instance is null, the button will be disabled !");
return false;
}
else {
logger.info("wired teeSet instance is NOT null, the button will be enabled !");
logger.info("teeSet color: "+getInstance().getTeeSet().getColor());
return true;
}
}
#RequestParameter
public void setRoundId(Long id) {
super.setId(id);
}
#Override
protected Round createInstance() {
Round round = super.createInstance();
round.setGolfer(currentGolfer);
round.setDate(new java.sql.Date(System.currentTimeMillis()));
return round;
}
}
Here the xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
template="layout/template.xhtml">
<ui:define name="body">
<h:form id="roundform">
<rich:panel>
<f:facet name="header>">
#{roundHome.managed ? 'Edit' : 'Add' } Round
</f:facet>
<s:decorate id="dateField" template="layout/edit.xhtml">
<ui:define name="label">Date:</ui:define>
<rich:calendar id="date" datePattern="dd/MM/yyyy" value="#{round.date}"/>
</s:decorate>
<s:decorate id="notesField" template="layout/edit.xhtml">
<ui:define name="label">Notes:</ui:define>
<h:inputTextarea id="notes" cols="80" rows="3" value="#{round.notes}" />
</s:decorate>
<s:decorate id="totalScoreField" template="layout/edit.xhtml">
<ui:define name="label">Total Score:</ui:define>
<h:inputText id="totalScore" value="#{round.totalScore}" />
</s:decorate>
<s:decorate id="weatherField" template="layout/edit.xhtml">
<ui:define name="label">Weather:</ui:define>
<h:selectOneMenu id="weather" value="#{round.weather}">
<s:selectItems var="_weather" value="#{weatherCategories}" label="#{_weather.label}"
noSelectionLabel=" Select " />
<s:convertEnum/>
</h:selectOneMenu>
</s:decorate>
<div style="clear: both;">
<span class="required">*</span> required fields
</div>
</rich:panel>
<div class="actionButtons">
<h:commandButton id="save" value="Save"
action="#{roundHome.persist}"
rendered="#{!roundHome.managed}" />
<!-- disabled="#{!roundHome.wired}" /> -->
<h:commandButton id="update" value="Update" action="#{roundHome.update}"
rendered="#{roundHome.managed}" />
<h:commandButton id="delete" value="Delete" action="#{roundHome.remove}"
rendered="#{roundHome.managed}" />
<s:button id="discard" value="Discard changes" propagation="end"
view="/Round.xhtml" rendered="#{roundHome.managed}" />
<s:button id="cancel" value="Cancel" propagation="end"
view="/#{empty roundFrom ? 'RoundList' : roundFrom}.xhtml"
rendered="#{!roundHome.managed}" />
</div>
<rich:tabPanel>
<rich:tab label="Tee Set">
<div class="association">
<h:outputText value="Tee set not selected" rendered="#{round.teeSet == null}" />
<rich:dataTable var="_teeSet" value="#{round.teeSet}" rendered="#{round.teeSet != null}">
<h:column>
<f:facet name="header">Course</f:facet>#{_teeSet.course.name}
</h:column>
<h:column>
<f:facet name="header">Color</f:facet>#{_teeSet.color}
</h:column>
<h:column>
<f:facet name="header">Position</f:facet>#{_teeSet.pos}
</h:column>
</rich:dataTable>
</div>
</rich:tab>
</rich:tabPanel>
</h:form>
</ui:define>
</ui:composition>
I suppose You have a single ScopeType.CONVERSATION (RoundHome) instance without any long-running conversation enabled. So you may have a Temporary conversation which survive until The render response phase.
When you submit your form, because you have a Temporary conversation, You just populate what you submit. It explains why your TeeSet is null. (Your submitted form does not contain any reference to a Tee instance)
If you really want your TeeSet survive until The next submit, you must enable a long-running conversation before showing and disable after submitting your form.
Just an advice: Each Home instance is by default ScopeType.CONVERSATION - inherited from Home. So you can set up your RoundHome as follows
#Name("roundHome")
public class RoundHome extends EntityHome<Round> {
...
}

Categories