【问题标题】:Can't access bean properties of model object across Spring webflow views无法跨 Spring webflow 视图访问模型对象的 bean 属性
【发布时间】:2014-04-21 05:49:01
【问题描述】:

我浏览了许多关于堆栈溢出的文章和帖子,但似乎找不到我的解决方案(包括这个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);
    }

POJOS:

@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 中?

【问题讨论】:

    标签: java hibernate spring-mvc spring-webflow


    【解决方案1】:

    您需要在评估表达式中将 flowScope bean 传递为:

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

    【讨论】:

    • 感谢您的回复。能够通过一些返工来解决这个问题。当我有空时会发布。
    • 给你复选标记。感谢您对此的帮助!
    猜你喜欢
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    相关资源
    最近更新 更多