Spring Webflow中的数据绑定问题

时间:2014-03-14 05:47:35

标签: java spring data-binding spring-webflow autowired

我是webflow的新手,我在这里面临几个问题。基本上我想将变量绑定到视图状态选择选项集并在下一个视图中使用它。为此,我使用了以下代码。

<var name="flowScope.jobIdString" class="java.lang.String" />

<input name="userId" type="java.lang.Long" />

<on-start>
    <set name="userId" value="1234"></set>
</on-start>

<view-state id="startJob" view="/WEB-INF/jsp/startJob.jsp">

    <on-entry>
        <evaluate expression="jobService.getAllJobsForUser(userId)"
            result="flowScope.jobs">
            </evaluate>
    </on-entry>
    <transition on="createNew" to="createNewJob" />
    <transition on="editJob" to="editJob" />
</view-state>

<action-state id="createNewJob">
    <evaluate expression="patientBean" result="flowScope.patient" />
    <transition to="patientinfo" />
</action-state>

<action-state id="editJob">
    <evaluate expression="patientService.getPatientInfo(jobId)"
        result="flowScope.patient" />
    <transition to="patientinfo" />
</action-state>

<view-state id="patientinfo" model="patient"
    view="/WEB-INF/jsp/patientInfo.jsp">
</view-state>

在这里,当我点击任一按钮时,我可以访问第一个视图,但不能进入下一个视图。 这是我的startjob.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>



    <select size="8" name="jobIdString">
        <c:forEach items="${jobs}" var="job">
            <option value="job.id">${job.name}</option>
        </c:forEach>
    </select>

    <br />

    <input type="submit" name="_eventId_createNew" value="createNew">
    <input type="submit" name="_eventId_editJob" value="editJob">

</body>
</html>

这是我的patientInfo.jsp文件

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form:form modelAttribute="patient">
        <form:hidden path="id" />
        <table>
            <tr>
                <td>Name:</td>
                <td><form:input path="name" /></td>
            </tr>
            <tr>
                <td>Age:</td>
                <td><form:input path="age" /></td>
            </tr>
            <tr>
                <td><input type="submit" value="Save and Close" /></td>
                <td><input type="submit" value="Save and Continue" /></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

这里我将患者模型绑定到此视图。

<webflow:flow-registry id="flowRegistry">
    <webflow:flow-location path="/WEB-INF/flow/webflow-flow.xml"
        id="webflow" />
</webflow:flow-registry>
<webflow:flow-executor id="flowExecutor"
    flow-registry="flowRegistry" />
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
    <property name="flowRegistry" ref="flowRegistry" />
    <property name="order" value="0" />
</bean>
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
    <property name="flowExecutor" ref="flowExecutor" />
</bean>

<bean id="jobService" class="com.b.webfolwprototype.service.JobService" />
<bean id="patientService" class="com.brandix.webfolwprototype.service.PatientService"></bean>

<bean id="patientBean" class="com.b.webfolwprototype.model.Patient"
    scope="prototype" />

<bean id="jobBean" class="com.b.webfolwprototype.model.Job"
    scope="prototype" />

这是我的webflow配置文件(上图)。

基本上这不能正常工作。

  1. 所以我也想解决以下这些问题。

  2. 这些型号如何在没有任何接线的情况下正常工作。我没有使用任何注释bean定义在webflow配置文件中(可能这可能没有正确连接)。

  3. 我可以将类似字符串的变量(在本例中为jobId)投标到视图(正如我在startjob.jsp中尝试过的那样)如果这是错误的,怎么办呢。

  4. 这是我的PatientService

    public class PatientService {
    
    public void save(Patient patient) {
        System.out.println("save patinet");
    }
    
    public Patient getPatientInfo(Long jobId){
        Patient patient = new Patient();
        patient.setAge(35);
        patient.setName("test name");
        return patient;
    }
    

    }

    这是我的JobService

    public class JobService {
    
    public List<Job> getAllJobsForUser(Long userId) {
    
        ArrayList<Job> jobList = new ArrayList<Job>();
        Job job1 = new Job();
        job1.setId((long) 1234);
        job1.setName("Test Job Name");
    
        Job job2 = new Job();
        job2.setId((long) 1235);
        job2.setName("Test Job Name 2");
    
        jobList.add(job1);
        jobList.add(job2);
        return jobList;
    }
    

    }

    这些是我的模特

        public class Job implements Serializable {
    
        private static final long serialVersionUID = 4979685043689356058L;
        private Long id;
        private String name;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }
    
    
    public class Patient implements Serializable {
    
        private static final long serialVersionUID = -1541400280884340541L;
        private String name;
        private int age;
        private Long patientId;
    
        public Long getPatientId() {
            return patientId;
        }
    
        public void setPatientId(Long patientId) {
            this.patientId = patientId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int i) {
            this.age = i;
        }
    
    }
    

1 个答案:

答案 0 :(得分:1)

您的流程进入视图startjob.jsp,这是启动点。从这里,当您点击指定的按钮时,swf不知道如何恢复流程。因此,为了恢复流程,请在startjob.jsp中包含以下内容:

     <input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}">