2013-06-02 3 views
1

xhtml의 작동 방식에 대해 묻고 싶습니다.xhtml + jsf 컨트롤러 클래스에서 값 가져 오기

이 내 tablero.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:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:c="http://java.sun.com/jsp/jstl/core" 
     xmlns:p="http://primefaces.org/ui"> 

    <ui:composition template="/template.xhtml"> 
     <ui:define name="title"> 
      <h:outputText value="Dak's Hangman"></h:outputText> 
     </ui:define> 
     <ui:define name="body"> 
      <h:panelGroup id="messagePanel" layout="block"> 
       <h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/> 
      </h:panelGroup> 
      <h:head id="demo" >Opportunities: </h:head>    
      <h:outputText value="#{partidaController.getRemainingOp()}"/>  
      <br/> 
      <h:outputText value="#{partidaController.getNombreJugador()}"/>     
      <br/> 
      <c:forEach var="x" items="#{partidaController.getLetrasColocadas()}" > 
       <h:inputText disabled="true" size="1" value="${x}"/> 
      </c:forEach> 
      <br/> 
      <h:panelGrid> 
       <h:form> 
        <p:commandButton id="b1" style="height: 30px; width: 30px;" value="a" action="#{turnoController.createTurno('a')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b2" style="height: 30px; width: 30px;" value="b" action="#{turnoController.createTurno('b')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b3" style="height: 30px; width: 30px;" value="c" action="#{turnoController.createTurno('c')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b4" style="height: 30px; width: 30px;" value="d" action="#{turnoController.createTurno('d')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b5" style="height: 30px; width: 30px;" value="e" action="#{turnoController.createTurno('e')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b6" style="height: 30px; width: 30px;" value="f" action="#{turnoController.createTurno('f')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b7" style="height: 30px; width: 30px;" value="g" action="#{turnoController.createTurno('g')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b8" style="height: 30px; width: 30px;" value="h" action="#{turnoController.createTurno('h')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b9" style="height: 30px; width: 30px;" value="i" action="#{turnoController.createTurno('i')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b10" style="height: 30px; width: 30px;" value="j" action="#{turnoController.createTurno('j')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b11" style="height: 30px; width: 30px;" value="k" action="#{turnoController.createTurno('k')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b12" style="height: 30px; width: 30px;" value="l" action="#{turnoController.createTurno('l')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b13" style="height: 30px; width: 30px;" value="m" action="#{turnoController.createTurno('m')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b14" style="height: 30px; width: 30px;" value="n" action="#{turnoController.createTurno('n')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b15" style="height: 30px; width: 30px;" value="o" action="#{turnoController.createTurno('o')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b16" style="height: 30px; width: 30px;" value="p" action="#{turnoController.createTurno('p')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b17" style="height: 30px; width: 30px;" value="q" action="#{turnoController.createTurno('q')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b18" style="height: 30px; width: 30px;" value="r" action="#{turnoController.createTurno('r')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b19" style="height: 30px; width: 30px;" value="s" action="#{turnoController.createTurno('s')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b20" style="height: 30px; width: 30px;" value="t" action="#{turnoController.createTurno('t')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b21" style="height: 30px; width: 30px;" value="u" action="#{turnoController.createTurno('u')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b22" style="height: 30px; width: 30px;" value="v" action="#{turnoController.createTurno('v')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b23" style="height: 30px; width: 30px;" value="w" action="#{turnoController.createTurno('w')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b24" style="height: 30px; width: 30px;" value="x" action="#{turnoController.createTurno('x')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b25" style="height: 30px; width: 30px;" value="y" action="#{turnoController.createTurno('y')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <p:commandButton id="b26" style="height: 30px; width: 30px;" value="z" action="#{turnoController.createTurno('z')}" onclick="document.getElementById(this.id).disabled=true;"/> 
        <br /> 
       </h:form> 
      <h:graphicImage value="../resources/images/hangman.jpg" width="480" height="400" /> 
      <h:link outcome="/index" value="#{bundle.CreateJugadorIndexLink}"/>   
     </h:panelGrid> 
     </ui:define> 
    </ui:composition> 
</html> 

이 내가 내가 buttonCommand 클릭 할 때마다에게 전화 빈입니다 :

public String createTurno(String s) { 
    try { 
     Map<String, Object> sesionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap(); 
     Integer id = (Integer) sesionMap.get("id_partida"); 

     Partida p = new Partida(); 
     p.setIdPartida(id); 
     current= new Turno(); 
     current.setIdPartida(p); 
     current.setLetraTurno(s); 
     ejbFacade.create(current); 
     return "tablero"; 
    } catch (Exception e) { 
     JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured")); 
     return null; 
    } 
} 

를이 내가에 통화 할 방법

public List getLetrasColocadas() 
{ 
    String lc = getFacade().letrasColocadasById(current.getIdPartida()); 

    ArrayList<String> lista = new ArrayList<String>(); 
    int i; 
    for (i=0; i<lc.length(); i++){ 
     String h = "" + lc.charAt(i) + ""; 
     lista.add(h); 
    } 
    return lista; 
} 

partidaController.getLetrasColocadas()가 잘 작동 ... 항목을 업데이트 (나는 그것을 디버깅 한 그리고 그것은 내가 원하는 것을 해). 문제는 inputText 값이 일단 표시되면,이 xhtml 파일의 이름을 가진 문자열을 반환하는 bean을 호출하는 명령 버튼을 클릭하면 다시 업데이트되지 않는다는 것입니다. 항상 동일하게 유지됩니다.

내가 xhtml에 대해 알아야 할 마술 ???

+0

우리가 도울 수 있도록 모델/관리 빈을 보여줘야합니다. – skuntsel

답변

2

현재 코드에서 몇 가지 문제가 있습니다

  1. 은 보기에 <h:inputText> 구성 요소를 추가하는 데 <c:forEach>을 사용하고 있습니다. 나중에 Ajax 조작을 사용하여 이러한 구성 요소를 갱신하면 문제점이 발생할 수 있습니다. 이 문제를 해결하려면 대신 <ui:repeat>을 사용하십시오. 자세한 내용은 JSTL in JSF2 Facelets... makes sense?을 참조하십시오.

  2. 귀하의 <p:commandButton>은 서버에 제출 된 후에 비활성화되었지만 다시 활성화되지 않으므로 한 번 실행하면 작동을 멈 춥니 다. ajax 호출을 완료 한 후 oncomplete="document.getElementById(this.id).disabled=false;"을 추가하여 활성화하십시오. 기본적으로 <p:commandButton>ajax 속성이 true으로 설정되어 있습니다.

  3. 아마도 눈치 채지 못했지만 getLetrasColocadas 메서드가 페이지에서 두 번 이상 호출되고 있습니다. 이 동작은 여기에서 잘 설명됩니다 : Why JSF calls getters multiple times. 어떻게 해결할 수 있을까요? 이 메소드의 비즈니스 로직을 loadLetrasColocadas 메소드로 이동하고 createTurno 끝에이 메소드를 호출하면 getter를 단순한 return lista (이 변수가 관리 Bean의 필드가된다고 가정 할 때)으로 활용할 수 있습니다. 이제

, 현재 디자인에 대한 몇 가지 경고

  1. 당신의 <h:form>은 서버에 양식 데이터를 제출할 때 당신이 UIInput 구성 요소, 예를 들어의 아무것도를 보낼 것입니다 따라서, 귀하의 <p:commandButton> 요소를 포함 위에서 언급 한 <h:inputText>의 정보는 절대로 서버로 전송되지 않습니다.

  2. 당신은 항상 당신의 createTurno 방법에 "tablero"를 반환하고 있기 때문에, 당신은 결코 아약스 호출 및 조용히 뒤에서 응답은 따라서을받지 동일한 뷰에 전달이 (를 다시로드 전체 페이지를 실행하지 브라우저에서 전체 리디렉션의 효과). createTurnoString에서 void으로 변경하는 것이 좋습니다. 또한

, 당신이 아약스를 사용하는거야 이후는 적어도 @ViewScoped에 관리 빈을 변경하는 것이 더 나은 것, 단지 호출합니다. 관리 빈 범위에 대한 추가 정보 : BalusC (JSF 및 Java EE 전문가)의 Communication in JSF 2: Managed Bean Scopes.

1

은 음 ... 나는 명령 단추의 OnClick 속성 ... 항목의 값을 업데이트와 호환되지 않는 것을 발견

관련 문제