Spring Web Flow - 表单不绑定视图

时间:2015-10-03 16:40:21

标签: spring forms hibernate spring-webflow spring-form

我对Spring和Spring Web Flow很陌生。 我遇到了无法将视图与提交表单绑定的问题: - 以下是我的代码: -

P.s:我不知道模型= {}在流XML中做了什么,但这已经是代码中已有的,并且将其更改为alertForm不会改变行为/

a)JSP

    <form:form name="alertSubmitForm" method="post" id="alertsForm" modelAttribute="alertForm">
            <table class="general">
                <tr>
                    <th>Alert Text
                    </th>
                    <td><form:textarea path="alertText" cols="50" id="alertText" style="padding-top: 11px"></form:textarea>
                    </td>
                    <td><span id="characterCount"></span>/250
                    </td>
                    <td><br/><br/><br/><br/><br/><input id="advancedAlertOptions" type="button" value="More" class="primarybtn" style="float:right;"/></td>
                </tr>
                <tr class="advancedAlertOptions" style="display:none">
                    <th>Active Date
                    </th>
                    <td><form:input path="activeDate" id="activeDate" type="date"/>
                    </td>
                </tr>
                <tr class="advancedAlertOptions"  style="display:none">
                    <th>Expiration Date
                    </th>
                    <td><form:input id="expDate" path="expDate" type="date"/>
                    </td>
                </tr>
                <tr class="advancedAlertOptions" style="display:none">
                    <th>Author
                    </th>
                    <td><form:input id="author" path="author" type="text" disabled="true" value="${author}" />
                    </td>
                </tr>
                <tr class="advancedAlertOptions" style="display:none">
                    <th>Added on
                    </th>
                    <td><form:input id="addedDate" type="date" path="addedDate" disabled="true"/>
                    </td>
                </tr>
            </table>
            <input id="cancelAddAlert" type="button" value="Cancel" class="secondarybtn" style="float:right; margin-left:20px;"/>
            <input id="persistAlert" type="submit" value="Add" class="secondarybtn" style="float:right;" name="_eventId_addAlert"/>
        </form:form>

b)Flow XML

    <view-state id="general" view="editMember/general" model="{}" parent="#abstract-member-view">
        <on-entry>
            <evaluate expression="new com.company.member.alerts.AlertForm()" result="viewScope.alertForm"/>         
        </on-entry>
        <on-render>         
            <evaluate expression="alertManagementService.getAlertsList(partyIdMember)" result="viewScope.alerts" />
        </on-render>        
         <transition on="addAlert" to="general" bind="true" >
            <evaluate expression="alertManagementService.addAlertToMember(alertForm,partyIdMember)" /> 
        </transition>
    </view-state>

c)服务电话

package com.company.member.alerts;

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;

    import org.hibernate.SQLQuery;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import com.company.domain.db.GenericDao;

    @Service("alertManagementService")
    @Transactional(readOnly = true)
    public class AlertManagementServiceImpl implements AlertManagementService {

    @Autowired
    GenericDao dao;
    @Override
    public List<AlertForm> getAlertsList(Long memberId) {
        String a = "SELECT * FROM PARTY_ALERT WHERE PARTY_ID = "+memberId+ " ORDER BY PA_ACTIVE_DATE";
        SQLQuery query = dao.createSQLQuery(a);
        List<Object[]> queryResults = query.list();
        List<AlertForm> results = new ArrayList<AlertForm>();
        for(Object[] arr : queryResults) {
            AlertForm alertForm = new AlertForm();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd H:m:s");
            try {
                alertForm.setAlertId(Long.valueOf(arr[0].toString()));
                alertForm.setMemberId(Long.valueOf(arr[1].toString()));
                alertForm.setAlertText(arr[2].toString());
                Date acD = df.parse(arr[3].toString());
                alertForm.setActiveDate(acD);
                if(arr[4]!=null) {
                    Date expD = df.parse(arr[4].toString());
                    alertForm.setExpDate(expD);
                }
                alertForm.setAuthor(arr[5].toString());
                Date adD = df.parse(arr[6].toString());
                alertForm.setAddedDate(adD);
                alertForm.setAddedDate(adD);
                alertForm.setActiveDate(acD);
            } 
            catch (ParseException e) {
                e.printStackTrace();
            }
            results.add(alertForm);
        } 
        return results;
    }

    @Override
    public void addAlertToMember(AlertForm alert, Long memberID) {
        Date expDate;
        PartyAlerts partyAlertsDB = new PartyAlerts();
        if(alert.getAlertText()!=null) {
            partyAlertsDB.setAlertText(alert.getAlertText());
            partyAlertsDB.setActiveDate(alert.getActiveDate());
            partyAlertsDB.setMemberId(memberID);
            if((expDate = alert.getExpDate())!=null) {
                partyAlertsDB.setInactiveDate(expDate);
            }
        }
        else { //hardcoding into DB
            partyAlertsDB.setActiveDate(new Date());
            partyAlertsDB.setInactiveDate(new Date());
            partyAlertsDB.setAlertText("This is a hardcoded alert");
            partyAlertsDB.setMemberId(memberID);
        }
        dao.save(partyAlertsDB);
    }
}

d)Form Backing Bean

    package com.company.member.alerts;

    import java.io.Serializable;
    import java.util.Date;


    public class AlertForm  implements Serializable{
    private static final long serialVersionUID = 1L;
    private Long alertId;
    private Long memberId;
    private String alertText;
    private Date activeDate;
    private Date expDate;
    private Date addedDate;
    private String author;

    public AlertForm() {
    }

    public Long getAlertId() {
        return alertId;
    }
    public void setAlertId(Long alertId) {
        this.alertId = alertId;
    }
    public Long getMemberId() {
        return memberId;
    }
    public void setMemberId(Long memberId) {
        this.memberId = memberId;
    }
    public String getAlertText() {
        return alertText;
    }
    public void setAlertText(String alertText) {
        this.alertText = alertText;
    }
    public Date getActiveDate() {
        return activeDate;
    }
    public void setActiveDate(Date activeDate) {
        this.activeDate = activeDate;
    }
    public Date getExpDate() {
        return expDate;
    }
    public void setExpDate(Date expDate) {
        this.expDate = expDate;
    }
    public Date getAddedDate() {
        return addedDate;
    }
    public void setAddedDate(Date addedDate) {
        this.addedDate = addedDate;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }


}

问题在于 - 当我提交表单时 - alertForm元素为NULL。获取渲染上所有警报的getter按预期工作。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

如果您不熟悉SWF,为什么要使用流继承等高级功能?包含引用的父流的内容是什么:

... model =“{}”parent =“#abstract-member-view”&gt;

这很可能是您问题的根源。您需要将模型属性显式定义为已初始化的POJO,以便将表单字段正确绑定到它。

因此,为了测试目的,简化在流程中初始化一个新的AlertForm,在视图状态之前和之外的某些地方

....
     <set name="alertFormModel" value="new com.company.member.alerts.AlertForm()"/>
....



 <!-- then when you enter the view-state define it like this: -->


<view-state id="general" view="editMember/general" model="alertFormModel">
....
</view-state>

如果此测试有效,则为AlertForm创建/实现Factory模式,以消除流xml中丑陋的“新”语法