我正在学习接缝并跟随Dan Allen的一本书“Seam In Action”。
这是本书的一个例子。
Seam 2.2.0.GA
JBoss 5.1.0.GA
这里的页面参数roundId总是为null,即使在循环序列化之后,也永远不会传递。在RoundEdit.xhtml上单击保存后,既没有Roud.xhtml也没有RoundEdit.xhtml。该实体始终保持不受管理状态。
RoundEdit.page.xml
<?xml version="1.0" encoding="UTF-8"?>
<page xmlns="http://jboss.com/products/seam/pages"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.2.xsd"
login-required="true">
<begin-conversation join="true" />
<param name="roundId" value="#{roundHome.id}" converterId="javax.faces.Long"/>
<param name="teeSetId" value="#{teeSetHome.teeSetId}" />
<param name="roundFrom" />
<action execute="#{roundHome.wire}" />
<navigation from-action="#{roundHome.persist}">
<rule if-outcome="persisted">
<end-conversation/>
<redirect view-id="#{null != roundFrom ? roundFrom : '/Round.xhtml'}" />
</rule>
</navigation>
</page>
RoundEdit.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>
<h:messages/>
<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>
Round.page.xml
<?xml version="1.0" encoding="UTF-8"?>
<page xmlns="http://jboss.com/products/seam/pages"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.2.xsd">
<param name="roundId" value="#{roundHome.id}" converterId="javax.faces.Long"/>
</page>
Round.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>">Round</f:facet>
<s:decorate id="id" template="layout/display.xhtml">
<ui:define name="label">Id:</ui:define>
<h:outputText value="#{null == roundHome.id ? 'null' : roundHome.id}">
<s:convertDateTime type="date" />
</h:outputText>
</s:decorate>
<s:decorate id="date" template="layout/display.xhtml">
<ui:define name="label">Date:</ui:define>
<h:outputText value="#{roundHome.instance.date}">
<s:convertDateTime type="date" />
</h:outputText>
</s:decorate>
<s:decorate id="golfer" template="layout/display.xhtml">
<ui:define name="label">Golfer:</ui:define>
#{roundHome.instance.golfer.name}
</s:decorate>
<s:decorate id="totalScore" template="layout/display.xhtml">
<ui:define name="label">Total Score:</ui:define>
#{roundHome.instance.totalScore}
</s:decorate>
<s:decorate id="weather" template="layout/display.xhtml">
<ui:define name="label">Weather:</ui:define>
#{roundHome.instance.weather}
</s:decorate>
<s:decorate id="notes" template="layout/display.xhtml">
<ui:define name="label">Notes:</ui:define>
#{roundHome.instance.notes}
</s:decorate>
<div style="clear:both"/>
</rich:panel>
<div class="actionButtons">
<s:button id="edit" view="/RoundEdit.xhtml" value="Edit" />
</div>
<rich:tabPanel>
<rich:tab label="Tee Set">
<div class="association">
<h:outputText value="Tee set not selected" rendered="#{roundHome.instance.teeSet == null}" />
<rich:dataTable var="_teeSet" value="#{roundHome.instance.teeSet}" rendered="#{roundHome.instance.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>
entityHome RoundHome.java
@Name("roundHome")
public class RoundHome extends EntityHome<Round>{
@In(required = false)
private Golfer currentGolfer;
@In(create = true)
private TeeSetHome teeSetHome;
@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) {
logger.info("in Setter RoundId is: " + id);
super.setId(id);
}
public Long getRoundId() {
Long id = (Long) getId();
logger.info("Setting RoundId : " + id);
return id;
}
@Override
protected Round createInstance() {
Round round = super.createInstance();
round.setGolfer(currentGolfer);
round.setDate(new java.sql.Date(System.currentTimeMillis()));
logger.info("Created a Round with roundId: " + round.getId());
return round;
}
@Override
protected Round loadInstance() {
logger.info("loadInstance for id: " + getId());
return (Round) getEntityManager().createQuery(
"select r from Round r " +
"join fetch r.golfer g " +
"join fetch r.teeSet ts " +
"join fetch ts.course c " +
"where r.id = :id ")
.setParameter("id",getId())
.getSingleResult();
}
}
答案 0 :(得分:0)
在Round.xhtml中你有:
<div class="actionButtons">
<s:button id="edit" view="/RoundEdit.xhtml" value="Edit" />
</div>
导航到RoundEdit但它没有传递要编辑的roundId。当您向网址添加ID时,您手动设置该ID。要在代码中设置它,您可以使用get动作并将其作为参数传递:
<s:button id="edit" view="/RoundEdit.xhtml" value="Edit" >
<param name="roundId" value="#{roundHome.id}"/>
</s:button>
查看网址,您会看到传递的参数。