我们正在使用JSF Primefaces 3.2。 我有一个模块,用户将使用自己的选择创建一些动态字段,如textfield,textarea,selectbox,checkbox,单选按钮,然后将根据这些选定的字段生成一个表单。 我已经成功创建了第一部分,用户正在创建表单并根据他的选择生成表单。但现在我面临的问题是如何将此表单保存到数据库中。 在这里,我附上了我的模块的一些代码,以便您很容易理解并快速找到解决方案。
这是我用动态生成所有字段的JSF表单
<h:form id="formObjectiveLibrary">
<h:panelGrid columns="3" styleClass="form-table" columnClasses="column_1, column_2, column_3">
<h:outputText value="Choose from Templates" />
<p:selectOneListbox id="objectiveTemplateID" value="#{objectiveTemplateAction.objectiveTemplateID}" style="height: 120px; width: 200px">
<f:selectItems value="#{objectiveTemplateAction.objectiveTemplateList}" var="template" itemLabel="#{template.title}" itemValue="#{template.id}"></f:selectItems>
<p:ajax event="change" listener="#{objectiveTemplateAction.loadTemplateQuestionsByID()}" update="libraryQuestionContainer"/>
</p:selectOneListbox>
<p:message for="objectiveTemplateID"/>
</h:panelGrid>
<p:outputPanel id="libraryQuestionContainer">
<p:outputPanel rendered="#{objectiveTemplateAction.questionList.size() > 0}">
<div class="tabbable tabs-left" style="margin-top: 20px; text-align: left;"> <!-- Only required for left/right tabs -->
<ul class="nav nav-tabs">
<li class="active"><a href="#inspection" data-toggle="tab">Inspection</a></li>
<li><a href="#observation" data-toggle="tab">Observation</a></li>
<li><a href="#rangeOfMotion" data-toggle="tab">Range of Motion</a></li>
<li><a href="#strength" data-toggle="tab">Strength</a></li>
<li><a href="#neuroVascular" data-toggle="tab">Neuro-Vascular</a></li>
<li><a href="#specialTests" data-toggle="tab">Special Tests</a></li>
<li><a href="#palpation" data-toggle="tab">Palpation</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="inspection">
<ui:repeat var="q" varStatus="current" value="#{objectiveTemplateAction.questionList}">
<p:outputPanel rendered="#{q.questionContainerName eq 'INSPECTION'}">
<h:panelGrid>
<p:outputPanel>
#{current.index + 1}. <h:outputText value="#{q.questionTitle}" />
</p:outputPanel>
<p:outputPanel rendered="#{q.questionType.questionTypeVariant eq 'textfield'}">
<h:inputText id="txtField" value=""/>
</p:outputPanel>
<p:outputPanel rendered="#{q.questionType.questionTypeVariant eq 'comment'}">
<h:inputTextarea id="comment" cols="100" rows="5" value=""/>
</p:outputPanel>
<p:outputPanel rendered="#{q.questionType.questionTypeVariant eq 'description_text'}">
</p:outputPanel>
<p:outputPanel rendered="#{q.questionType.questionTypeVariant eq 'multi_textbox'}">
<ui:repeat var="txt" value="#{q.questionOptionsList}">
<h:panelGrid columns="2" styleClass="form-table" columnClasses="column1, column2">
<h:outputText value="#{txt.optionName}" />
<h:inputText id="multiText" value=""/>
</h:panelGrid>
</ui:repeat>
</p:outputPanel>
<p:outputPanel rendered="#{q.questionType.questionTypeVariant eq 'multi_numeric_textbox'}">
<ui:repeat var="numeric" value="#{q.questionOptionsList}">
<h:panelGrid columns="2" styleClass="form-table" columnClasses="column1, column2">
<h:outputText value="#{numeric.optionName}" />
<pe:inputNumber id="numericTxt" value=""/>
</h:panelGrid>
</ui:repeat>
</p:outputPanel>
<p:outputPanel rendered="#{q.questionType.questionTypeVariant eq 'checkbox'}">
<p:selectManyCheckbox id="check" value="#{q.questionOptionsList}" layout="pageDirection">
<f:selectItems value="#{q.questionOptionsList}" var="chk" itemLabel="#{chk.optionName}" itemValue="#{chk.id}"/>
</p:selectManyCheckbox>
</p:outputPanel>
<p:outputPanel rendered="#{q.questionType.questionTypeVariant eq 'radio'}">
<p:selectOneRadio id="radio" value="#{objectiveTemplateAction.optionValue.optionValue}" layout="pageDirection">
<f:selectItems value="#{q.questionOptionsList}" var="rd" itemLabel="#{rd.optionName}" itemValue="#{rd.id}"/>
</p:selectOneRadio>
</p:outputPanel>
<p:outputPanel rendered="#{q.questionType.questionTypeVariant eq 'matrix_radio'}">
<table class="table table-bordered">
<tbody>
<ui:repeat var="mrow" value="#{q.questionOptionsList}">
<p:outputPanel rendered="#{mrow.optionPosition eq 'ROW'}">
<tr>
<td>#{mrow.optionName}</td>
<td>
<p:selectOneButton id="mradio" value="">
<f:selectItems value="#{objectiveTemplateAction.getColumnOptionsList(q.questionOptionsList)}" var="mr" itemLabel="#{mr.optionName}" itemValue="#{mr.id}"/>
</p:selectOneButton>
</td>
</tr>
</p:outputPanel>
</ui:repeat>
</tbody>
</table>
</p:outputPanel>
<p:outputPanel rendered="#{q.questionType.questionTypeVariant eq 'matrix_checkbox'}">
<table class="table table-bordered">
<tbody>
<ui:repeat var="checkrow" value="#{q.questionOptionsList}">
<p:outputPanel rendered="#{checkrow.optionPosition eq 'ROW'}">
<tr>
<td>#{checkrow.optionName}</td>
<td>
<p:selectManyButton id="mcheck" value="#{q.questionOptionsList}">
<f:selectItems value="#{objectiveTemplateAction.getColumnOptionsList(q.questionOptionsList)}" var="mc" itemLabel="#{mc.optionName}" itemValue="#{mc.id}"/>
</p:selectManyButton>
</td>
</tr>
</p:outputPanel>
</ui:repeat>
</tbody>
</table>
</p:outputPanel>
<p:outputPanel rendered="#{q.questionType.questionTypeVariant eq 'matrix_select'}">
<table class="table table-bordered">
<p:outputPanel rendered="#{q.questionOptionMenuList.size() gt 0}">
<thead>
<tr>
<th></th>
<ui:repeat value="#{q.questionOptionMenuList}" var="m">
<th>#{m.menuTitle}</th>
</ui:repeat>
</tr>
</thead>
</p:outputPanel>
<tbody>
<ui:repeat var="selectrow" value="#{q.questionOptionsList}">
<p:outputPanel rendered="#{selectrow.optionPosition eq 'ROW'}">
<tr>
<td>#{selectrow.optionName}</td>
<ui:repeat value="#{q.questionOptionMenuList}" var="m">
<td>
<h:selectOneMenu value="#{m.questionOptionMenuChoiceList}" style="width: auto;">
<f:selectItems value="#{m.questionOptionMenuChoiceList}" var="choice" itemLabel="#{choice.choiceName}" itemValue="#{choice.id}"/>
</h:selectOneMenu>
</td>
</ui:repeat>
</tr>
</p:outputPanel>
</ui:repeat>
</tbody>
</table>
</p:outputPanel>
<p:outputPanel rendered="#{q.questionType.questionTypeVariant eq 'rating'}">
<table class="table table-bordered">
<tbody>
<ui:repeat var="orow" value="#{q.questionOptionsList}">
<tr>
<td>#{orow.optionName}</td>
<td>
<p:selectOneButton id="rating" value="">
<f:selectItems value="#{q.questionOptionRatingList}" var="rating" itemLabel="#{rating.ratingLabel}" itemValue="#{rating.id}"/>
</p:selectOneButton>
</td>
</tr>
</ui:repeat>
</tbody>
</table>
</p:outputPanel>
</h:panelGrid>
</p:outputPanel>
</ui:repeat>
</div>
<div class="tab-pane" id="observation">
<p>A</p>
</div>
<div class="tab-pane" id="rangeOfMotion">
<p>sanjay</p>
</div>
<div class="tab-pane" id="strength">
<p>A</p>
</div>
<div class="tab-pane" id="neuroVascular">
<p>B</p>
</div>
<div class="tab-pane" id="specialTests">
<p>C</p>
</div>
<div class="tab-pane" id="palpation">
<p>D</p>
</div>
</div>
</div>
<h:panelGrid columns="3" styleClass="form-table" columnClasses="column_1, column_2, column_3">
<h:outputText value="" />
<p:commandButton style="text-align: center" actionListener="#{objectiveTemplateAction.fillAndSaveTemplate}"
styleClass="btn" value="Save" update="@form :mainErrorMessages" process="@form"/>
<h:outputText value="" />
</h:panelGrid>
</p:outputPanel>
<p:outputPanel rendered="#{objectiveTemplateAction.questionList ne null and objectiveTemplateAction.questionList.size() eq 0}">
<p class="lead">This template is under construction. We will launch this template very soon.</p>
</p:outputPanel>
</p:outputPanel>
</h:form>
</ui:composition>
这是我将在用户请求中使用的Java Action类
@Named("objectiveTemplateAction")
@Scope(ScopeType.VIEW)
public class EMRObjectiveTemplateAction implements Serializable {
private static final Logger log = LoggerFactory.getLogger(EMRObjectiveTemplateAction.class);
@Inject
private EMRObjectiveTemplateService emrObjectiveTemplateService;
@Inject
private UserSession userSession;
private EMRObjectiveTemplate objectiveTemplate = new EMRObjectiveTemplate();
private EMRObjectiveTemplateQuestion question = new EMRObjectiveTemplateQuestion();
private List<EMRObjectiveTemplateQuestion> questionList = null;
private List<EMRObjectiveTemplate> objectiveTemplateList = new ArrayList<EMRObjectiveTemplate>();
private List<EMRObjectiveQuestionType> questionTypeList = new ArrayList<EMRObjectiveQuestionType>();
private List<EMRObjectiveTemplateQuestionOption> questionOptionList = new ArrayList<EMRObjectiveTemplateQuestionOption>();
private List<EMRObjectiveTemplateQuestionOption> questionOptionColumnList = new ArrayList<EMRObjectiveTemplateQuestionOption>();
private List<Long> optionMenuCountList = new ArrayList<Long>();
private List<Long> optionRatingCountList = new ArrayList<Long>();
private List<EMRObjectiveTemplateQuestionOptionMenu> optionMenuList = new ArrayList<EMRObjectiveTemplateQuestionOptionMenu>();
private List<EMRObjectiveTemplateQuestionOptionRating> optionRatingList = new ArrayList<EMRObjectiveTemplateQuestionOptionRating>();
private EMRObjectiveTemplateQuestionOptionValue optionValue = new EMRObjectiveTemplateQuestionOptionValue();
private Long objectiveTemplateID;
private Long questionTypeID;
private String title;
private String description;
private String questionContainerName;
private boolean showQuestionTabContainer = false;
private String questionOptionName;
private long optionMenuCount;
private long optionRatingCount;
private String menuOptionName;
@PostConstruct
public void init() {
this.objectiveTemplateList = this.emrObjectiveTemplateService.getAllTemplates();
this.questionTypeList = this.emrObjectiveTemplateService.getAllQuestionType();
// prefill optionMenuCountList with menuCount
this.optionMenuCountList.add(1L);
this.optionMenuCountList.add(2L);
this.optionMenuCountList.add(3L);
this.optionMenuCountList.add(4L);
this.optionMenuCountList.add(5L);
this.optionMenuCountList.add(6L);
this.optionMenuCountList.add(7L);
this.optionMenuCountList.add(8L);
this.optionMenuCountList.add(9L);
// prefill optionRatingCountList with ratingCount
this.optionRatingCountList.add(2L);
this.optionRatingCountList.add(3L);
this.optionRatingCountList.add(4L);
this.optionRatingCountList.add(5L);
this.optionRatingCountList.add(6L);
this.optionRatingCountList.add(7L);
this.optionRatingCountList.add(8L);
this.optionRatingCountList.add(9L);
this.optionRatingCountList.add(10L);
this.optionRatingCountList.add(11L);
this.optionRatingCountList.add(12L);
this.optionRatingCountList.add(13L);
this.optionRatingCountList.add(14L);
this.optionRatingCountList.add(15L);
this.optionRatingCountList.add(16L);
}
/**
* Recharge optionMenuList with requested optionMenuCount
*/
public void rowNumbers() {
this.optionMenuList = new ArrayList<EMRObjectiveTemplateQuestionOptionMenu>();
for(int i = 0; i < this.optionMenuCount; i++){
this.optionMenuList.add(new EMRObjectiveTemplateQuestionOptionMenu());
}
}
/**
* Recharge optionRatingList with requested optionRatingCount
*/
public void ratingNumbers() {
this.optionRatingList = new ArrayList<EMRObjectiveTemplateQuestionOptionRating>();
for(int i = 0; i < this.optionRatingCount; i++){
this.optionRatingList.add(new EMRObjectiveTemplateQuestionOptionRating());
}
}
/**
* Fetch all questions registered under this objectiveTemplateID
*/
public void loadTemplateQuestionsByID(){
System.out.println("Current Template ID: " + this.objectiveTemplateID);
this.questionList = this.emrObjectiveTemplateService.loadTemplateQuestionsByID(this.objectiveTemplateID);
System.out.println("Questions size: " + this.questionList.size());
for(EMRObjectiveTemplateQuestion q : this.questionList){
if(q.getQuestionType().getQuestionTypeVariant().equalsIgnoreCase("radio")){
this.optionValue = new EMRObjectiveTemplateQuestionOptionValue();
optionValue.setQuestion(q);
}
}
}
/**
* Save filled template into database
*/
public void fillAndSaveTemplate(){
for(EMRObjectiveTemplateQuestion q : this.questionList){
System.out.println("Option Value for Question " + q.getQuestionTitle() + " is: " + this.optionValue.getOptionValue());
// System.out.println("Question ANswer: " + q.getQuestionAnswer().getQuestionAnswer());
}
}
/**
* Filter Options according to position from this optionList
* @param optionsList
* @return
*/
public List<EMRObjectiveTemplateQuestionOption> getColumnOptionsList(List<EMRObjectiveTemplateQuestionOption> optionsList) {
List<EMRObjectiveTemplateQuestionOption> columnOptionsList = new ArrayList<EMRObjectiveTemplateQuestionOption>();
for(EMRObjectiveTemplateQuestionOption option : optionsList){
if(option.getOptionPosition().equalsIgnoreCase("COLUMN")){
columnOptionsList.add(option);
}
}
return columnOptionsList;
}
// Getter - Setter
public EMRObjectiveTemplate getObjectiveTemplate() {
return objectiveTemplate;
}
public void setObjectiveTemplate(EMRObjectiveTemplate objectiveTemplate) {
this.objectiveTemplate = objectiveTemplate;
}
public List<EMRObjectiveTemplate> getObjectiveTemplateList() {
return objectiveTemplateList;
}
public void setEmrObjectiveTemplateList(List<EMRObjectiveTemplate> objectiveTemplateList) {
this.objectiveTemplateList = objectiveTemplateList;
}
public Long getObjectiveTemplateID() {
return objectiveTemplateID;
}
public void setObjectiveTemplateID(Long objectiveTemplateID) {
this.objectiveTemplateID = objectiveTemplateID;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<EMRObjectiveQuestionType> getQuestionTypeList() {
return questionTypeList;
}
public void setQuestionTypeList(List<EMRObjectiveQuestionType> questionTypeList) {
this.questionTypeList = questionTypeList;
}
public Long getQuestionTypeID() {
return questionTypeID;
}
public void setQuestionTypeID(Long questionTypeID) {
this.questionTypeID = questionTypeID;
}
public String getQuestionContainerName() {
return questionContainerName;
}
public void setQuestionContainerName(String questionContainerName) {
this.questionContainerName = questionContainerName;
}
public boolean isShowQuestionTabContainer() {
return showQuestionTabContainer;
}
public void setShowQuestionTabContainer(boolean showQuestionTabContainer) {
this.showQuestionTabContainer = showQuestionTabContainer;
}
public List<EMRObjectiveTemplateQuestionOption> getQuestionOptionList() {
return questionOptionList;
}
public void setQuestionOptionList(List<EMRObjectiveTemplateQuestionOption> questionOptionList) {
this.questionOptionList = questionOptionList;
}
public List<EMRObjectiveTemplateQuestionOption> getQuestionOptionColumnList() {
return questionOptionColumnList;
}
public void setQuestionOptionColumnList(List<EMRObjectiveTemplateQuestionOption> questionOptionColumnList) {
this.questionOptionColumnList = questionOptionColumnList;
}
public String getQuestionOptionName() {
return questionOptionName;
}
public void setQuestionOptionName(String questionOptionName) {
this.questionOptionName = questionOptionName;
}
public EMRObjectiveTemplateQuestion getQuestion() {
return question;
}
public void setQuestion(EMRObjectiveTemplateQuestion question) {
this.question = question;
}
public List<EMRObjectiveTemplateQuestionOptionMenu> getOptionMenuList() {
return optionMenuList;
}
public void setOptionMenuList(List<EMRObjectiveTemplateQuestionOptionMenu> optionMenuList) {
this.optionMenuList = optionMenuList;
}
public String getMenuOptionName() {
return menuOptionName;
}
public void setMenuOptionName(String menuOptionName) {
this.menuOptionName = menuOptionName;
}
public long getOptionMenuCount() {
return optionMenuCount;
}
public void setOptionMenuCount(long optionMenuCount) {
this.optionMenuCount = optionMenuCount;
}
public List<Long> getOptionMenuCountList() {
return optionMenuCountList;
}
public void setOptionMenuCountList(List<Long> optionMenuCountList) {
this.optionMenuCountList = optionMenuCountList;
}
public long getOptionRatingCount() {
return optionRatingCount;
}
public void setOptionRatingCount(long optionRatingCount) {
this.optionRatingCount = optionRatingCount;
}
public List<Long> getOptionRatingCountList() {
return optionRatingCountList;
}
public void setOptionRatingCountList(List<Long> optionRatingCountList) {
this.optionRatingCountList = optionRatingCountList;
}
public List<EMRObjectiveTemplateQuestionOptionRating> getOptionRatingList() {
return optionRatingList;
}
public void setOptionRatingList(List<EMRObjectiveTemplateQuestionOptionRating> optionRatingList) {
this.optionRatingList = optionRatingList;
}
public List<EMRObjectiveTemplateQuestion> getQuestionList() {
return questionList;
}
public void setQuestionList(List<EMRObjectiveTemplateQuestion> questionList) {
this.questionList = questionList;
}
public EMRObjectiveTemplateQuestionOptionValue getOptionValue() {
return optionValue;
}
public void setOptionValue(EMRObjectiveTemplateQuestionOptionValue optionValue) {
this.optionValue = optionValue;
}
}