Reputation: 8487
Possibly I'm using forms incorrectly. The idea is that detail.content displays some HTML, and this portion works fine. The form is supposed to allow multiple (one-to-many notes to be entered and displayed on the right.
While the default note is displayed, more notes are not. How do I link the note bean to the detail bean? I was thinking of a String "id" and passing that from one bean to another.
This is similar to the idea of passing params from one view.xhtml to another, except that it's all on one page. I would like to keep the beans distinct. Ultimately, I would like to do this with EJB's, and so want to keep that option open, while not using EJB's yet.
view, detail.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:ui="http://java.sun.com/jsf/facelets"
template="./complexTemplate.xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core">
<ui:define name="top">
<h:form>
<h:outputLink id="back" value="detail.xhtml">
<f:metadata>
<f:viewParam name="id" value="#{detail.id}" />
</f:metadata>
<f:param name="id" value="#{detail.back()}" />
<h:outputText value="back" />
</h:outputLink>
</h:form>
<h:form>
<h:outputLink id="forward" value="detail.xhtml">
<f:metadata>
<f:viewParam name="id" value="#{detail.id}" />
</f:metadata>
<f:param name="id" value="#{detail.forward()}" />
<h:outputText value="forward" />
</h:outputLink>
</h:form>
</ui:define>
<ui:define name="content">
<h:outputText escape="false" value="#{detail.content}"></h:outputText>
</ui:define>
<ui:define name="right">
<p>#{notes.note.id}</p>
<p>#{notes.note.comment}</p>
<h:form>
<h:inputText value="#{notes.note.comment}" />
<h:commandButton value="add note"
action="#{notes.commentAction()}"/>
</h:form>
</ui:define>
</ui:composition>
bean, Notes.java:
package net.bounceme.dur.nntp;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.Dependent;
import javax.inject.Named;
@Named
@Dependent
public class Notes {
private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger(Notes.class.getName());
private static final Level level = Level.INFO;
private Note note = new Note();
public Notes() {
logger.log(level, "Notes..");
}
public Note getNote() {
return note;
}
private void setNote(Note note) {
this.note = note;
}
public void commentAction() {
logger.log(level, "Notes.newNote.."); note.setId("messageIdGoesHere");
note.setComment("hmmm");
}
}
The other bean, Details, works fine. However, I'm not sure how to integrate two beans onto one page so that the two beans are aware of each other.
Upvotes: 0
Views: 580
Reputation: 1108632
Use @Inject
.
@Named
public class Notes {
@Inject
private Detail detail;
}
It'll be available in the Notes
instance during the lifetime beyond the @PostConstruct
method. The other way round can also. It's not entirely clear what the parent-child relationship is in your particular case.
Unrelated to the concrete problem, you've there some odd view markup. Only one <f:metadata>
in top of definition is sufficient. Also, #{detail.back()}
as a value expression is odd. You should have a getBack()
method and reference it as #{detail.back}
. The same for forward()
. Also, EJBs have nothing to do with this all. Whether you plan to use EJBs or not is irrelevant to this particular issue. I'd suggest learning JSF by a decent book/tutorial, not by cobbling some loose snippets together while not understanding the complete picture.
Upvotes: 4