2010-05-23 34 views
2

我正在学习缝并遵循Dan Allen的一本书Seam In Action。接缝页面参数不按预期方式工作

这是本书的一个例子。

缝2.2.0.GA
JBoss的5.1.0.GA

这里的页面参数roundId是即使经过一轮系列化总是空,这是从来没有过。点击保存到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

+1假设你的问题是:为什么实体不反映行为? – stacker 2010-05-23 22:19:45

+0

好,但** logger.info(“在Setter RoundId是:”+ ID); **输出? – 2010-05-25 19:38:52

+0

@Arthur Ronald F D Garcia,实际上,如果我不传递URL中的roundId参数,setter永远不会被调用。即使在我保存该回合之后,Round details页面显示null roundId。只有当我在URL中传递roundId参数时,该方法才会被调用并具有正确的值。 – rangalo 2010-05-26 06:50:52

回答

0

在Round.xhtml您有:

<div class="actionButtons"> 
    <s:button id="edit" view="/RoundEdit.xhtml" value="Edit" /> 
</div> 

此导航到RoundEdit但它没有通过roundId进行编辑。当您为网址添加一个ID时,您需要手动设置该ID。要设置它的代码,你可以使用取得动作,并传递作为PARAM:

<s:button id="edit" view="/RoundEdit.xhtml" value="Edit" > 
<param name="roundId" value="#{roundHome.id}"/> 
</s:button> 

退房的网址,你会看到被传递帕拉姆。