【问题标题】:how to clear the form on transition (action) Spring Webflow如何在转换(动作)Spring Webflow上清除表单
【发布时间】:2019-12-08 21:53:46
【问题描述】:

我正在努力向我的 Spring Webflow 应用程序添加一项功能,其中一个阶段有一个表单和一个表格。表单绑定到模型,并且在一次转换(操作)上,我将模型保存到 scopeModel 列表。问题是我不知道如何使用适当的技术来实现这一点。

    <?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:/" />-->

    <!--&lt;!&ndash; Global Transition &ndash;&gt;-->
    <!--<global-transitions>-->
        <!--<transition on="home" to="home" validate="false" />-->
    <!--</global-transitions>-->






</flow>

型号 包 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

}

处理程序

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

每当我提交表单时,模型都会保存到列表中,这里没有问题。但是保存表单后没有被刷新。保存的数据显示在现场。我尝试了多种方法来重置表单,但没有运气。问题如下:我需要将多个模型保存到列表中并在此特定页面上的表格中显示。问题我怎样才能以正确和简单的方式实现这一目标!提前致谢

【问题讨论】:

    标签: spring spring-boot spring-webflow


    【解决方案1】:

    您可以在 ApplicationHandler 中添加一个方法,该方法返回一个新的 ApplyPersonalInfoMain "createNewPersonalInfo"

    然后使用:

    <set name="personalInfo" value="applicantHandler.createNewPersonalInfo()" />
    

    为了重新创建您的模型对象并且应该重置表单

    【讨论】:

    • 我已经尝试了上述解决方案,但没有按预期工作!相反,我创建了两个视图,一个用于显示列表,另一个用于创建对象。我认为它正在工作,还不错,但不是最佳解决方案。不过还是谢谢@rptmat57
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 2012-07-28
    相关资源
    最近更新 更多