简体   繁体   English

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

[英]Can't access bean properties of model object across Spring webflow views

I've looked through numerous articles and posts on stack overflow, but can't seem to find my solution to this (including this post ). 我已经通过堆栈溢出大量的文章和帖子看了看,但似乎无法找到我解决这个(包括本岗位 )。 I'm new to Spring (and webflow) but not to Java. 我是Spring(和Webflow)的新手,但对Java却不是。

I have form pages using Spring Webflow 2.3.2, the input data for which I simply want to persist using Hibernate3 to MySQL5. 我有使用Spring Webflow 2.3.2的表单页面,我只是想使用Hibernate3到MySQL5保留输入数据。 The problem is, I don't see values from the model (rentalApplication) persisting between pages. 问题是,我看不到模型(rentalApplication)的值在页面之间仍然存在。 I will post the relevant code but if there's more detail needed, I can update accordingly. 我将发布相关代码,但是如果需要更多详细信息,我可以进行相应更新。

flow.xml: 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>

... ...

Controller: 控制器:

@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);
    }
}

Config: 配置:

  <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 implementation: 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: 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: 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: 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 -->

The SQL output using log4jdbc shows that the inserts are happening. 使用log4jdbc的SQL输出显示插入正在发生。 Perhaps the error is in the flow.xml? 也许错误在flow.xml中?

You need to pass the flowScope bean in evaluate expression as: 您需要按以下方式在评估表达式中传递flowScope bean:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM