I am struggling to add a feature to my Spring Webflow app in which one of the stages has a form and a table. The form is bound to a model and on one transition (action) I save the model to the list of scopeModel. The thing is that I dont know how to achieve this using a proper technique.
<?xml version="1.0" encoding="UTF-8"?>
<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-2.0.xsd">
<!--ASSIGNING MODELS-->
<var name="personalInfo" class="com.example.demo.alloystry.models.ApplicantPersonalInfoMain" />
<var name="employment" class="com.example.demo.alloystry.models.ApplicantEmployment" />
<var name="experience" class="com.example.demo.alloystry.models.ApplicantExperienceInfo" />
<var name="experienceList" class="com.example.demo.alloystry.models.ApplicantExperienceList" />
<var name="education" class="com.example.demo.alloystry.models.step4.Education" />
<var name="additional" class="com.example.demo.alloystry.models.additionalinfo.ApplicantAdditionalInfo" />
<!-- returning and adding inside flow registerModel instance -->
<on-start>
<evaluate expression="applicantHandler.init()"
result="flowScope.registerModel" />
</on-start>
<view-state id="personal" view="flows/register/personal-info" model="personalInfo">
<transition on="employment" to="validatePersonal" />
<on-exit>
<evaluate expression="applicantHandler.addPersonalInfo(flowScope.registerModel, personalInfo)"></evaluate>
</on-exit>
</view-state>
<action-state id="validatePersonal">
<evaluate expression="applicantHandler.validatePersonalInfo(personalInfo, messageContext)" />
<transition on="success" to="employment" />
<transition on="failure" to="personal" />
</action-state>
<view-state id="employment" view="flows/register/employment" model="employment">
<transition on="personal" to="personal" />
<transition on="experience" to="experience" />
<on-exit>
<evaluate expression="applicantHandler.addEmploymentInfo(flowScope.registerModel, employment)"></evaluate>
</on-exit>
</view-state>
<!--<view-state id="experience" view="flows/register/experience" model="experience">-->
<!--<transition on="employment" to="employment" />-->
<!--<transition on="education" to="education" />-->
<!--<on-exit>-->
<!--<evaluate expression="applicantHandler.addExperienceInfo(flowScope.registerModel, experience)"></evaluate>-->
<!--</on-exit>-->
<!--</view-state>-->
<view-state id="experience" view="flows/register/experience" model="experience">
<on-entry>
<set name="experience.workPeriod" value="''"/>
<set name="experience.nameOfOrg" value="''"/>
<set name="experience.titleOfyourPost" value="''"/>
</on-entry>
<transition on="addExperience">
<evaluate expression="applicantHandler.addExperienceInfo(flowScope.registerModel, experience)" />
</transition>
<transition on="employment" to="employment" />
<transition on="education" to="education" />
<!--<on-exit>-->
<!--<evaluate expression="applicantHandler.setExperienceList(flowScope.registerModel.experienceInfoList, experienceList)"></evaluate>-->
<!--</on-exit>-->
</view-state>
<view-state id="education" view="flows/register/education" model="education">
<transition on="experience" to="experience" />
<transition on="additional" to="additional" />
<on-exit>
<evaluate expression="applicantHandler.addEducationInfo(flowScope.registerModel, education)"></evaluate>
</on-exit>
</view-state>
<view-state id="additional" view="flows/register/education" model="additional">
<transition on="education" to="education" />
<transition on="confirm" to="confirm" />
<on-exit>
<evaluate expression="applicantHandler.addAdditionalInfo(flowScope.registerModel, additional)"></evaluate>
</on-exit>
</view-state>
<view-state id="confirm" view="flows/register/applicant-confirm" model="flowScope.registerModel">
<transition on="submit" to="submit" />
</view-state>
<action-state id="submit">
<evaluate expression="registerHandler.saveAll(flowScope.registerModel, messageContext)" />
<transition on="success" to="success" />
<transition on="failure" to="confirm" />
</action-state>
<!-- end state -->
<end-state id="success" view="flows/register/signup-success" />
<!--<end-state id="home" view="externalRedirect:contextRelative:/" />-->
<!--<!– Global Transition –>-->
<!--<global-transitions>-->
<!--<transition on="home" to="home" validate="false" />-->
<!--</global-transitions>-->
</flow>
Model package com.example.demo.alloystry.models;
import com.example.demo.alloystry.models.additionalinfo.ApplicantAdditionalInfo;
import com.example.demo.alloystry.models.step4.Education;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Data
public class RegisterModel implements Serializable {
private static final long serialVersionUID = 1L;
private ApplicantPersonalInfoMain personalInfo;
private ApplicantEmployment employment;
// private ApplicantExperienceInfo experience;
private List<ApplicantExperienceInfo> experienceInfoList = new ArrayList<ApplicantExperienceInfo>();
private Education education;
private ApplicantAdditionalInfo additional; //TODO should be configured
}
Handler
package com.example.demo.alloystry.handlers;
import com.example.demo.alloystry.models.*;
import com.example.demo.alloystry.models.additionalinfo.ApplicantAdditionalInfo;
import com.example.demo.alloystry.models.step4.Education;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.stereotype.Component;
@SuppressWarnings("ALL")
@Component
public class ApplicantHandler {
public RegisterModel init() {
return new RegisterModel();
}
public ApplicantExperienceInfo giveNewExperience() {
return new ApplicantExperienceInfo();
}
public void addPersonalInfo(RegisterModel registerModel, ApplicantPersonalInfoMain personalInfo) {
registerModel.setPersonalInfo(personalInfo);
}
public void addEmploymentInfo(RegisterModel registerModel, ApplicantEmployment employment) {
registerModel.setEmployment(employment);
}
// public void setExperienceList(RegisterModel registerModel, ApplicantExperienceList experiences) {
// registerModel.setExperiences(experiences);
// }
public void addExperienceInfo(RegisterModel registerModel, ApplicantExperienceInfo exp) {
registerModel.getExperienceInfoList().add(exp);
}
public void addEducationInfo(RegisterModel registerModel, Education education){
registerModel.setEducation(education);
}
public void addAdditionalInfo(RegisterModel registerModel, ApplicantAdditionalInfo additional){
registerModel.setAdditional(additional);
}
public String saveAll(RegisterModel registerModel, MessageContext error) {
String transitionValue = "success";
// XXX Save model in database or somewhere else...
error.addMessage(new MessageBuilder(). //
error() //
.source("registration") //
.defaultText( //
String.format("Couldn't register user with username: %s!",
registerModel.getPersonalInfo().getFirstName())) //
.build());
transitionValue = "failure";
return transitionValue;
}
public String validatePersonalInfo(ApplicantPersonalInfoMain personalInfo, MessageContext error) {
String transitionValue = "success";
// Checking that username is not equal to 'Vakho' :d XXX do whatever you want!
if (personalInfo.getFirstName().equalsIgnoreCase("Vakho")) {
error.addMessage(new MessageBuilder(). //
error() //
.source("username") //
.defaultText("You are not allowed to use Vakho as the username!") //
.build());
transitionValue = "failure";
}
// // Checking if password matched the confirm password
// if (!personalInfo.getPassword().equals(personalInfo.getConfirmPassword())) {
// error.addMessage(new MessageBuilder(). //
// error() //
// .source("confirmPassword") //
// .defaultText("Password doesn't match up the confirm password!") //
// .build());
//
// transitionValue = "failure";
// }
return transitionValue;
}
}
Whenever I submit the form, model is saving to the list no problems here. But after saving the form is not being refreshed. The saved data is being shown on field. I have tried multiple ways to reset the form but no luck. The problem is following: I need to save multiple models to a list and show on table ON THIS SPECIFIC PAGE. Question How can i achieve this in a correct and easy way! Thanks in advance
you could add a method in your ApplicationHandler that returns a new ApplicantPersonalInfoMain "createNewPersonalInfo"
then use:
<set name="personalInfo" value="applicantHandler.createNewPersonalInfo()" />
in order to recreate your model object and that should reset the form