【问题标题】:Update the input text field on click of a button单击按钮更新输入文本字段
【发布时间】:2014-09-04 23:14:58
【问题描述】:

我有一个 Facelets 页面,其中有一个 p:inputText 字段用于学生 ID。我在此输入文本字段旁边还有一个p:commandButton,我希望当我单击此按钮时,会生成一个随机学生 ID 并显示在此文本字段中。我的 Facelets 文件代码是:

<td width="15%"><p:inputText id="Student ID" value="#{StudentData.id}"></p:inputText></td>
<td width="13%"><p:commandButton value="Generate ID" action="#{StudentID.id}" ajax="false" >  </p:commandButton></td>

StudentID.id 包含生成随机数的代码,如下所示:

public class StudentID implements Serializable {

    private int id;
    public StudentID() {

    }


     public int getId() {  
            return id;  
        }  

     @PostConstruct
       public void init() {

        Random r = new Random();
        int Low = 10;
        int High = 10000;
         id = r.nextInt(High-Low) + Low;
    }

}

代码会生成随机 ID,但是如何使用该随机 ID 更新 Facelets 页面上的输入文本字段?我基本上希望一旦我单击“生成 ID”按钮,它旁边的输入文本字段应该使用生成的随机数进行更新。

【问题讨论】:

    标签: jsf primefaces


    【解决方案1】:
    <h:form id="RandomIDGenerationForm">
    
    <h:panelGroup id="StudentIDGeneration">
        <h:inputText id="Student ID" value="#{StudentData.id}"></p:inputText>
    </h:panelGroup>
    <p:commandButton value="Generate ID" action="#{StudentData.generateRandomID()}" update="StudentIDGeneration">  </p:commandButton>
    
    </form>
    

    托管 bean:

    public class StudentData implements Serializable {
            
        private int id;
        public StudentData() {
            //default contructor
        }
         
        public int getId() {  
            return id;  
        }  
        
        public void generateRandomID() {
            Random r = new Random();
            int Low = 10;
            int High = 10000;
            id = r.nextInt(High-Low) + Low;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多