通过Spring将复选框值赋值给varchar

时间:2013-06-18 14:28:09

标签: java forms spring

我正在尝试从数据库中获取varchar消息,以显示jsp中复选框字段的选定值,以显示患者药物的剂量频率。可能的值将保存在varchar中以逗号分隔的字符串中。对于大多数表单字段,只有一个表单值与一个数据库字段比率,但在这种情况下,我需要将作为字符串[]的值合并到逗号分隔的字符串中,然后在检索该记录时该患者的药物,显示从selectableDosageFrequencyList中选择的逗号分隔字符串中的选定值。

您对此非常感谢,因为我不确定我在这里缺少什么。

在应用程序上下文中,我创建了可能值的列表,作为ServiceBean的一部分。

    <property name="selectableDosageFrequencyList">
        <set>
            <value>On an empty stomach</value>
            <value>Every other day</value>
            <value>4 times daily</value> 
            <value>3 times daily</value>
            <value>Twice daily</value>
            <value>At bedtime</value> 
            <value>With meal</value>
            <value>As needed</value>
            <value>Once daily</value>               
        </set>
    </property>

这是作为requestscope在流程中设置的。

<view-state id="addEditMedication" model="medication">

    <on-render>
        <set name="requestScope.selectableDosageFrequencyList" value="memberService.buildSelectableDosageFrequencyList(patient)" />     
    </on-render>
...
    <transition on="next" to="assessment" >
        <evaluate expression="memberService.updateMedication(patient, medication)" />
    </transition>
</view-state>

我在memberService中有helper方法,需要在表单为init时执行,然后在表单完成时执行。

    //get the form fields selected and build the new string for db
public String setSelectedDosageFrequency(String [] dosageFrequencies){
    String frequencies = null;
    if (dosageFrequencies != null){
        for (String s : dosageFrequencies){
            frequencies = frequencies + "," + s;
        }
    }
    return frequencies;
}
//get value from database and build selected Set
public LinkedHashSet<String> getSelectedDosageFrequencyList(String dosageFrequency){
    String copyOfDosages =dosageFrequency;//may not need to do this
    LinkedHashSet<String> setofSelectedDosageFrequency = new LinkedHashSet<String> ();
    while (copyOfDosages!= null && copyOfDosages.length()>0){
        for (String aFrequency: selectableDosageFrequencyList){
        if (copyOfDosages.contains(aFrequency)){
            setofSelectedDosageFrequency.add(aFrequency);
            if (!copyOfDosages.equals(aFrequency) && copyOfDosages.endsWith(aFrequency)){
                copyOfDosages.replaceAll(","+aFrequency, "");       
            }else if (!copyOfDosages.equals(aFrequency) && copyOfDosages.contains(aFrequency=",")){
                copyOfDosages.replaceAll(aFrequency+",", "");
            }else
                copyOfDosages.replaceAll(aFrequency, "");
            copyOfDosages.trim();
        }
        }
    }   
    return setofSelectedDosageFrequency;        
}

支持表单的Medication类将有一个剂量 - 频率变量作为字符串。

private String dosageFrequency;

我目前正在做这个jsp。

        <div class="formField">
            <form:label path="dosageFrequency">Dosage Frequency</form:label>
            <ul class="multi-column double" style="width: 550px;">
                <form:checkboxes path="dosageFrequency" items="${selectableDosageFrequencyList}" itemLabel="${selectableDosageFrequencyList}" element="li" />
            </ul>
        </div>  

1 个答案:

答案 0 :(得分:0)

在看了这个之后,我有一个解决方案,希望这可以帮助其他有类似问题的人。哦,并将分隔符更改为空格'',而不是:

webflow和app上下文没有改变,但是因为view-state id =“addEditMedication”...使用initMedication(patient)方法转换到视图状态,并且视图状态的转换使用memberService .updateMedication(病人,医学)。我用:

更新了memberServiceImpl
@Override
public Medication initMedication(PatientInformation patient, int index) {
    ServiceUtils.assertNotNull("A patient is required", patient);
    Medication medication = patient.getMedicationList().get(index);
    medication.setDosageFrequencySet(
                  convertSelectedDosageFrequencyList(medication.getDosageFrequency()));
    return medication;
}

@Override
public void updateMedication(PatientInformation patient, Medication medication) {
    if (!patient.getMedicationList().contains(medication)){
        medication.setDosageFrequency(
               convertSelectedDosageFrequency(medication.getDosageFrequencySet()));
        patient.getMedicationList().add(medication);
    }
}

private String convertSelectedDosageFrequency(LinkedHashSet<String> dosageFrequencies){
    String frequencies = StringUtils.join(dosageFrequencies," ");
    return frequencies;
}

private  LinkedHashSet<String> convertSelectedDosageFrequencyList(String dosageFrequency){
    String copyOfDosages =dosageFrequency;
    LinkedHashSet<String> setofSelectedDosageFrequency = new LinkedHashSet<String> ();
    if  (copyOfDosages!= null && copyOfDosages.length()>0){
        for (String aFrequency: selectableDosageFrequencyList){
        if (copyOfDosages.contains(aFrequency)){
            setofSelectedDosageFrequency.add(aFrequency);
            copyOfDosages = copyOfDosages.replaceAll(aFrequency, "");
            copyOfDosages = copyOfDosages.trim();
        }
        }
    }   
    return setofSelectedDosageFrequency;        
}

Medication类更新为:

private String dosageFrequency;
private LinkedHashSet<String> dosageFrequencySet;
//hibernate mapped
public String getDosageFrequency() {
    return dosageFrequency;
}
public void setDosageFrequency(String dosageFrequency) {
    this.dosageFrequency = dosageFrequency;
}
public LinkedHashSet<String> getDosageFrequencySet() {
    return dosageFrequencySet;
}
public void setDosageFrequencySet(LinkedHashSet<String> dosageFrequencySet) {
    this.dosageFrequencySet = dosageFrequencySet;
}

使用

更新jsp
        <div class="formField">
            <form:label path="dosageFrequency">Dosage Frequency</form:label>
            <ul class="multi-column double" style="width: 550px;">
                <form:checkboxes id="dosageFrequencySet" path="dosageFrequencySet" items="${selectableDosageFrequencyList}" element="li" />
            </ul>
        </div>