java - 向导组件 PrimeFaces 更新其他变量

标签 java jsf primefaces updates wizard

我有两个标签。 使用向导组件,当我按“下一步”按钮(从第一个选项卡)时,我需要填充一个列表(使用 SelectItem 对象),然后在第二个选项卡的 SelectOneMenu 标记中显示这些 SelectItem。当我很快完成在第一个选项卡上输入数据,然后按“下一步”按钮时,此 SelectOneMenu 标签(在第二个选项卡上)中没有任何内容。

基本上,我需要更新下一个选项卡以及当前选项卡的处理。无论如何要这样做吗?

提前致谢。

enter image description here enter image description here

这是我的代码:

<p:wizard widgetVar="wiz" showNavBar="true" flowListener="#{detentionForm.onFlowProcess}">
                <p:tab id="typeOfLeader" title="Leader Selection">
                    <p:panel header="Leader Selection">
                        <p:messages showSummary="true" showDetail="false"/>
                        <p:panelGrid columns="2">
                            #{msgs.typeOfLeaderPunishment}
                            <p:selectOneMenu value="#{detentionForm.typeOfLeaderSelectedID}" style="width:400px" panelStyle="width:150px" effect="fade">
                                <f:selectItems value="#{detentionForm.teacherTypes}" var="type" itemLabel="#{type.label}" itemValue="#{type.value}" />
                            </p:selectOneMenu>
                        </p:panelGrid> 
                    </p:panel>
                </p:tab>
                <p:tab id="typeOfPunishment" title="Punishment Type">
                    <p:panel header="Type of Punishment">
                        <p:panelGrid columns="2">
                            #{msgs.typeOfDetention}
                            <p:selectOneMenu value="#{detentionForm.typeOfPunishment}" style="width:400px" panelStyle="width:150px" effect="fade" >
                                <f:selectItems value="#{detentionForm.detentionTypes}" var="type" itemLabel="#{type.label}" itemValue="#{type.value}" />
                            </p:selectOneMenu> 
                        </p:panelGrid>

                    </p:panel>
                </p:tab>

            </p:wizard>

#{detentionForm.detentionTypes} 是一个数组列表,当我按下第二个选项卡的“下一步”按钮时,需要填充该数组列表。


这是我的支持 bean:

@ViewScoped
@Named("detentionForm")
public class DetentionFormBean implements Serializable{
    @Resource(name="jdbc/DetentionCentre")
    private DataSource ds;

    private Details details = (Details) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userDetails");



    private String typeOfPunishment;
    private String typeOfLeader = details.getTypeOfLeader(); //Can change if user is House Leader. Eg. They can become a teacher when making a punishment
    private int typeOfLeaderID; //ID in the database of the teacher's type of leadership
    private int typeOfLeaderSelectedID = 1; //Set default to teacher as House leader can be teacher and normal teacher will be a teacher!
    private ArrayList<SelectItem> detentionTypes = new ArrayList<SelectItem>(); //SelectItem objects that shows what punishments each teacher can do
    private ArrayList<SelectItem> teacherTypes = new ArrayList<SelectItem>(); //SelectItem objects that shows what type of leadership a teacher can be (Teacher or House Leader)


    //gets all the data necessary from the database to show on the page
    @PostConstruct
    public void initialize(){
        this.setTypeOfLeaderID(details.getUserName()); //gets the ID from the database to find out the default type of leadership
        this.findTeacherTypes(this.typeOfLeader); 
    } 

    public String onFlowProcess(FlowEvent event) {  //change later for backing as well >>>>>>>>>>>>>>
            String stepToGo = event.getNewStep();
            if(stepToGo.equals("typeOfPunishment")){
                this.findDetentionTypes();

            }
        return stepToGo;

    }

    //populates teacherType Arraylist depending the user's teacher type. House Leader can be a Teacher or a House Leader.
    private void findTeacherTypes(String type) {
        if(type.equals("House Leadership Team")){
            this.teacherTypes.add(new SelectItem(Integer.toString(this.typeOfLeaderID), type)); //House leader is 2
            this.teacherTypes.add(new SelectItem(Integer.toString(this.typeOfLeaderID - 1), "Teacher" )); //Teacher is 1
        }else{ //type is Teacher
            this.teacherTypes.add(new SelectItem(Integer.toString(this.typeOfLeaderID), type)); //Teacher is 1
        }
    }


    //populates detentiontypes depending on type of leader
    private void findDetentionTypes() {
        System.out.println(">>>>Inside findDetentionTypes()!!!");
        PreparedStatement ps;
        Connection con;
        String sqlInitialData = "SELECT r.punishment_type, p.type FROM detentioncentredb.tbl_teacher_roles_allowed_punishments r, detentioncentredb.tbl_punishment_types p WHERE r.teacher_roles = ? AND p.ID = r.punishment_type";
        ResultSet rs;

        try {
            con = ds.getConnection();
            ps = con.prepareStatement(sqlInitialData);
            ps.setString(1, Integer.toString(this.typeOfLeaderSelectedID)); 
            rs = ps.executeQuery();
            while(rs.next()){
                SelectItem s = new SelectItem(rs.getString("punishment_type"), rs.getString("type"));
                this.detentionTypes.add(s);
            }
            rs.close();
            ps.close();
            con.close();

        } catch (SQLException ex) {
            Logger.getLogger(DetentionFormBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //getter and setter methods

    public String getTypeOfPunishment() {
        return typeOfPunishment;
    }

    public void setTypeOfPunishment(String typeOfPunishment) {
        this.typeOfPunishment = typeOfPunishment;
    }

    public String getTypeOfLeader() {
        return typeOfLeader;
    }

    public ArrayList<SelectItem> getDetentionTypes() {
        return detentionTypes;
    }

    public ArrayList<SelectItem> getTeacherTypes() {
        return teacherTypes;
    }

    public int getTypeOfLeaderID() {
        return typeOfLeaderID;
    }

    //gets the typeOfLeader the current user is from the database. This retrieves the ID of the teacher's type of leadership. NOT WHAT THE TEACHER SELECTED.
    private void setTypeOfLeaderID(int userName){
        PreparedStatement ps;
        Connection con;
        String sqlTypeOfLeaderID = "SELECT TypeOfLeader FROM detentioncentredb.tbl_teachers WHERE RegNumber = ?";
        ResultSet rs;
        try {
            con = ds.getConnection();
            ps = con.prepareStatement(sqlTypeOfLeaderID);
            ps.setInt(1, userName);
            rs = ps.executeQuery();
            while(rs.next()){
                this.typeOfLeaderID = rs.getInt("TypeOfLeader");
            }
            rs.close();
            ps.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(DetentionFormBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getTypeOfLeaderSelectedID() {
        return typeOfLeaderSelectedID;
    }

    public void setTypeOfLeaderSelectedID(int typeOfLeaderSelectedID) {
        this.typeOfLeaderSelectedID = typeOfLeaderSelectedID;
    }


}

最佳答案

您可以使用ajax来通知服务器端您正在进入向导。来自 PF documentation :

如果您希望在向导尝试后退或前进时在服务器端收到通知,请定义一个flowListener

<p:wizard flowListener="#{userWizard.handleFlow}">
...
</p:wizard>

public String handleFlow(FlowEvent event) {
    String currentStepId = event.getCurrentStep();
    String stepToGo = event.getNextStep();
    if(skip)
        return "confirm";
    else
        return event.getNextStep();
    }

在您的情况下,您需要在 stepToGo 达到 typeOfPunishment 时加载 detentionTypes 数组,具体取决于用户在第一步中选择的内容向导:

public String handleFlow(FlowEvent event) {
    String stepToGo = event.getNextStep();
    if(stepToGo.equals("typeOfPunishment")){
        detentionTypes = service.loadDetentionTypes(this.TypeOfLeaderSelectedID);
    }
}

关于java - 向导组件 PrimeFaces 更新其他变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15741090/

相关文章:

ajax - <f :ajax render ="someId"> does not update target component, 但 <f:ajax render ="@form"> 工作正常

java - JSF2 ApplicationScope bean实例化时间?

JSF只读inputText问题

java - 在java中逐位读取数字字符串的最佳方法

java - gwtupload 缺少工具

java - 具有多个属性的对象的域模型问题。我应该如何构建它?

java - jsf中的后退命令按钮

jsf - 在 JSF2/PrimeFaces 中命名容器

validation - primefaces tabView 在选项卡更改时跳过表单验证

java - 为什么正则表达式的\z 对我不起作用?