Question

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.

I have form pages using Spring Webflow 2.3.2, the input data for which I simply want to persist using Hibernate3 to MySQL5. The problem is, I don't see values from the model (rentalApplication) persisting between pages. I will post the relevant code but if there's more detail needed, I can update accordingly.

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:

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

The SQL output using log4jdbc shows that the inserts are happening. Perhaps the error is in the flow.xml?

Was it helpful?

Solution

You need to pass the flowScope bean in evaluate expression as:

    <evaluate expression="rentalApplicationController.update(flowScope.rentalApplication)"
        result="flowScope.rentalApplication"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top