2014-11-01 18 views
0

getResponsesubmit的返回類型應該是什麼?帶有@Named後臺bean的echo參數到facelets模板客戶端

當在firstFormSecondForm中輸入猜測時,我該如何迴應該猜測到同一​​個網頁?

無論使用Ajax,所以沒有重新加載在同一頁

加載了新的一頁,guessResults.xhtml,例如,其中Echo的猜測。

支持豆,NextClient

package dur.beans; 

import dur.jpa.Client; 
import dur.jpa.ClientFacadeLocal; 
import java.util.concurrent.atomic.AtomicInteger; 
import javax.ejb.EJB; 
import javax.enterprise.context.ApplicationScoped; 
import javax.inject.Named; 

@Named("nextClient") 
@ApplicationScoped 
public class NextClient implements NextClientLocal { 

    @EJB 
    private ClientFacadeLocal clientFacade; 
    private AtomicInteger next = new AtomicInteger(1009); 
    private AtomicInteger guess = new AtomicInteger(0); 
    private final boolean correct = true; 

    @Override 
    public String getNext() { 
     next.addAndGet(1); 
     Client client = clientFacade.find(next.intValue()); 
     return client.toString(); 
    } 

    @Override 
    public void setGuess(int guessInt) { 
     guess = new AtomicInteger(guessInt); 
    } 

    @Override 
    public int getGuess() { 
     return guess.intValue(); 
    } 

    //not sure what do with these methods 

    @Override 
    public String getResponse() { 
     return "the guess of " + guess.intValue() + " is " + correct; 
    } 

    @Override 
    public String submit() { 
     return "the guess of " + guess.intValue() + " is " + correct; 
    } 

} 

Facelets模板客戶端,next.xhtml

<!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:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     > 

    <h:head></h:head> 
    <h:body> 
     This and everything before will be ignored 
     <ui:composition template="template.xhtml"> 
      <ui:define name="navigation"> 
       <ui:include src="menu.xhtml"/> 
      </ui:define> 
      <ui:define name="main"> 
       <h1>next bird</h1> 
       <p> 
        #{nextClient.next} 
       </p> 

       <p> 


        <h:panelGroup id="firstPanel"> 
         <h:form id="firstForm"> 
          <h:outputLabel for="input" value="First form input" /> 
          <h:inputText id="input" value="#{nextClient.guess}" required="true" /> 
          <h:commandButton value="Submit form" action="#{nextClient.submit}"> 
           <f:ajax execute="@form" render="@form :secondPanel :secondForm :messages" /> 
          </h:commandButton> 
          <h:message for="input" /> 
         </h:form> 
        </h:panelGroup> 
        <h:panelGroup id="secondPanel"> 
         <h:form id="secondForm"> 
          <h:outputLabel for="input" value="Second form input" /> 
          <h:inputText id="input" value="#{nextClient.guess}" required="true" /> 
          <h:commandButton value="Submit other form" action="#{nextClient.submit}"> 
           <f:ajax execute="@form" render="@form :firstPanel :firstForm :messages" /> 
          </h:commandButton> 
          <h:message for="input" /> 
         </h:form> 
        </h:panelGroup> 
        <h:messages id="messages" globalOnly="true" layout="table" /> 



       </p> 
      </ui:define> 
     </ui:composition> 
     This and everything after will be ignored 
    </h:body> 
</html> 

還看到:

http://balusc.blogspot.ca/2011/09/communication-in-jsf-20.html#AjaxRenderingOfContentWhichContainsAnotherForm

JSF 2.0 commandButton do nothing

https://javaserverfaces.java.net/nonav/docs/2.0/pdldocs/facelets/h/commandButton.html

http://docs.oracle.com/javaee/7/tutorial/doc/jsf-facelets003.htm

我Glassfish上運行的小面,採用CDI,所以我使用@Named@ManagedBean - 一些文件上面是@ManagedBean更多的齒輪,但我不知道有多重要。

目標比「hello world」更好一步,「hello world,你的猜測」會是一個好結果。如果有特定的手冊,我不介意RTFM到那個特定的文檔。 Oracle文檔可能是facelets的最佳選擇?

代碼:

https://github.com/THUFIR/EntAppWeb

回答

0

response.xhtml

<!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:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core"> 

    <h:head>response</h:head> 
    <h:body> 
     This and everything before will be ignored 
     <ui:composition template="template.xhtml"> 
      <ui:define name="navigation"> 
       <ui:include src="menu.xhtml"/> 
      </ui:define> 
      <ui:define name="main"> 
       <h1>submitted value</h1> 
       <p> 
        #{nextClient.guess} 
       </p> 
       <h2>for this bird</h2> 
       <p> 
        #{nextClient.client} 
       </p> 
      </ui:define> 
     </ui:composition> 
     This and everything after will be ignored 
    </h:body> 
</html> 

next.xhtml:使用支持bean NextClient

<!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:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core"> 

    <h:head>next</h:head> 
    <h:body> 
     This and everything before will be ignored 
     <ui:composition template="template.xhtml"> 
      <ui:define name="navigation"> 
       <ui:include src="menu.xhtml"/> 
      </ui:define> 
      <ui:define name="main"> 
       <h1>next bird</h1> 
       <p> 
        #{nextClient.next} 
       </p> 
       <p> 
        <h:panelGroup id="simpleGroup"> 
         <h:form id="simpleForm"> 
          <h:outputLabel for="input" value="First form input" /> 
          <h:inputText id="input" value="#{nextClient.guess}" required="true" /> 
          <h:commandButton value="submit" action="response"> 
          </h:commandButton> 
         </h:form> 
        </h:panelGroup> 

       </p> 
      </ui:define> 
     </ui:composition> 
     This and everything after will be ignored 
    </h:body> 
</html> 

package dur.beans; 

import dur.jpa.Client; 
import dur.jpa.ClientFacadeLocal; 
import java.util.concurrent.atomic.AtomicInteger; 
import javax.ejb.EJB; 
import javax.enterprise.context.ApplicationScoped; 
import javax.inject.Named; 

@Named("nextClient") 
@ApplicationScoped 
public class NextClient implements NextClientLocal { 

    @EJB 
    private ClientFacadeLocal clientFacade; 
    private AtomicInteger next = new AtomicInteger(1009); 
    private AtomicInteger guess = new AtomicInteger(0); 
    private final boolean correct = true; 
    private Client client = new Client(); 

    @Override 
    public String getNext() { 
     next.addAndGet(1); 
     client = clientFacade.find(next.intValue()); 
     return client.toString(); 
    } 

    @Override 
    public void setGuess(int guessInt) { 
     guess = new AtomicInteger(guessInt); 
    } 

    @Override 
    public int getGuess() { 
     return guess.intValue(); 
    } 

    @Override 
    public Client getClient() { 
     return client; 
    } 

    @Override 
    public void setClient(Client client) { 
     this.client = client; 
    } 

} 

將提交的值與鳥一起輸出到響應中。將結果輸出到同一頁面可能更有意義,但這已足夠。