开发者

jsf render component type?

I am creating an online survey application using JSF with IceFaces. The survey can have any number of questions. Each question can be any one of the types checkbox, radio button, textbox, etc., with开发者_运维知识库 multiple options.

Now I need to show the question one per page in jsf. I am fetching The question type, question, option list(opt1, opt2, opt3...) data from the db.

When user click on prev button, I need to go back to previous question. When user click on next button, I need to go to next question. At the end, when user click on Submit button, need to store the survey data in the db.

How can I render the components based on the question type? (If question type is checkbox means, I need to show checkbox. If question type is radio button means, I need to show radio button..)

How can I do this? Can anyone please help me on this?

If any example on this type please share me the link.


Use the rendered attribute. If the EL condition in the attiribute evaluates true, then the component will render HTML to the output, otherwise not.

So, assuming that you've a

public class Question {

    public enum Type { 
        SINGLE, MULTIPLE;
    }

    private Long id;
    private Type type;
    private String text;
    private List<Answer> answers;

    // ...
}

then you can use it as follows:

<h:dataTable value="#{bean.questions}" var="question">
    <h:column>
        <h:selectOneRadio value="#{bean.answers[question.id]}" rendered="#{question.type == 'SINGLE'}">
            <f:selectItems value="#{question.answers}" />
        </h:selectOneRadio>
        <h:selectManyCheckbox value="#{bean.answers[question.id]}" rendered="#{question.type == 'MULTIPLE'}">
            <f:selectItems value="#{question.answers}" />
        </h:selectManyCheckbox>
    </h:column>
</h:dataTable>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜