无法跨Spring Webflow视图访问模型对象的bean属性

时间:2014-03-15 22:09:58

标签: java hibernate spring-mvc spring-webflow

我已经查看过有关堆栈溢出的大量文章和帖子,但似乎无法找到我的解决方案(包括此post)。我是Spring(和webflow)的新手,但不是Java。

我有使用Spring Webflow 2.3.2的表单页面,我只想使用Hibernate3继承到MySQL5的输入数据。问题是,我没有看到模型(rentalApplication)中的值在页面之间持续存在。我将发布相关代码,但如果需要更多详细信息,我可以相应更新。

flow.xml:

<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/webflow 
    http://www.springframework.org/schema/webflow/spring-webflow.xsd">

  <on-start>
    <evaluate expression="rentalApplicationController.newForm()" result="flowScope.rentalApplication" />
  </on-start>

  <view-state id="rentalapp1" view="embeddedFlow/rentalapp1" model="rentalApplication">
    <transition on="next" to="rentalapp2">
      <evaluate
      expression="rentalApplicationController.update(rentalApplication)"
      result="flowScope.rentalApplication"/>
    </transition>
    <transition on="cancel" to="cancel" bind="false" history="discard"/>
    <transition on="save" to="save">
      <evaluate
      expression="rentalApplicationController.update(rentalApplication)"
      result="flowScope.rentalApplication"/>
    </transition>
  </view-state>

  <view-state id="rentalapp2" view="embeddedFlow/rentalapp2" model="rentalApplication">
    <transition on="previous" to="rentalapp1" validate="false"/>
    <transition on="next" to="rentalapp3">
      <evaluate
      expression="rentalApplicationController.update(rentalApplication)"
      result="flowScope.rentalApplication"/>
     </transition>
    <transition on="save" to="save">
      <evaluate
      expression="rentalApplicationController.update(rentalApplication)"
      result="flowScope.rentalApplication"/>
    </transition>

      ...

控制器:

@Service(value="rentalApplicationController")
public class RentalApplicationController extends FormAction {

    public RentalApplication newForm() {
    return rentalApplicationDao.create();
    }

    public RentalApplication update(RentalApplication rentalApp) {
    rentalApplicationDao.update(rentalApp);

    //not the best way to do this, but making sure we get
    //the updated data from the db
    int id = rentalApp.getId();
    return rentalApplicationDao.retrieve(id);
    }
}

配置:

  <bean id="formAction"
    class="com.foo.bar.RentalApplicationController">
  </bean>
  <bean id="primaryApplicant"
    class="com.foo.bar.account.Applicant"
    scope="session">
  </bean>
  <bean id="coApplicant"
    class="com.foo.bar.account.Applicant"
    scope="session">
  </bean>
  <bean id="rentalApplication"
    class="com.foo.bar.property.RentalApplication"
    scope="session">
  </bean>
<bean name="formAction" class="com.foo.bar.RentalApplicationController">
    <property name="formObjectName"><value>rentalApplication</value></property>
    <property name="formObjectClass"><value>com.foo.bar.RentalApplication</value>     </property>
    <property name="validator">
      <bean class="com.foo.bar.RentalApplicationValidator"/>
    </property>
</bean>

DAO实施:

@Repository
@Transactional
@Service("rentalApplicationService")

public class RentalApplicationDaoImpl extends HibernateDaoSupport implements RentalApplicationDao {

    public RentalApplication create() {
    return new RentalApplication();
    }

    public void update(RentalApplication rentalApplication) {
        this.getHibernateTemplate().saveOrUpdate(rentalApplication);
    }

    public RentalApplication retrieve(int id) {
        return (RentalApplication)   getHibernateTemplate().getSessionFactory().getCurrentSession().get(RentalApplication.class, id);
    }

POJO。:

@Entity 
@Table(name="RENTAL_APPLICATION")
public class RentalApplication implements Serializable {

    public RentalApplication() {
       this.setDateCreated(Calendar.getInstance().getTime());
       this.setPrimaryApplicant(new Applicant());
       this.setCoApplicant(new Applicant());
    }
}

@Entity
@DiscriminatorValue("APPLICANT")
public class Applicant implements Serializable {
    //getters and setters including the one for email
}

rentalapp1 JSP:

<form:form id="rentalapp1" commandName="rentalApplication" action="${flowExecutionUrl}" method="POST">
<form:input path="primaryApplicant.email" type="text" class="form-control" placeholder="Primary Applicant Email" data-type="email"/><br/>
</form:form>

rentalapp2 JSP:

Date Created: <c:out value="${rentalApplication.dateCreated}"></c:out><br/>  <!--PRINTS DATE-->
Primary ID: <c:out value="${rentalApplication.primaryApplicant.id}"></c:out><br/> <!--PRINTS 0-->
Primary email: <c:out value="${rentalApplication.primaryApplicant.email}"></c:out><br/> <!--PRINTS EMPTY STRING EVEN IF SUPPLIED -->

使用log4jdbc的SQL输出显示插入正在发生。也许错误发生在flow.xml中?

1 个答案:

答案 0 :(得分:0)

您需要将evaluate表达式中的flowScope bean传递为:

    <evaluate expression="rentalApplicationController.update(flowScope.rentalApplication)"
        result="flowScope.rentalApplication"/>