【问题标题】:Spring web flow model bindingSpring Web 流模型绑定
【发布时间】:2015-02-06 16:36:20
【问题描述】:

我在 Spring Web Flow 中遇到了一些绑定模型属性的问题。当我使用<form:input path="${propertyName}" cssClass="form-control" /> 时,生成的HTML 标记显示为<input class="form-control" type="text" value=""/>,并且完全省略了HTML 名称属性。此外,如果我尝试初始化模型中的属性,我会得到一个运行时属性未找到异常。我正在使用 Spring 4.1.2 和 Spring Web Flow 2.4.1。

奇怪的是我在这个模型上有两个选择字段被正确绑定。但是对于标量或嵌套对象属性的文本属性,则会出现此问题。

有谁知道为什么即使我指定了路径属性,spring form:input 标记也没有生成 HTML 名称属性?谢谢

这已经让我困惑了一个多星期。非常感谢任何帮助。不知道为什么我没有任何绑定工作。我什至创建了一个精简的项目,但绑定仍然无法正常工作。这是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
         id="WebApp_ID" version="3.0">
  <display-name>Test</display-name>  
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:DispatcherServlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

这是我的 DispatcherServlet-context.xml:

<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
                            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.test.project" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

HomeController.java:

package com.test.project.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.test.project.Customer;

@Controller
public class HomeController {

    @RequestMapping("/")
    public String welcome(Model model) {        
        Customer customer = new Customer();
        model.addAttribute("customer", customer);       
        return "welcome";
    }

    @RequestMapping(value = "/checkbindings", method = RequestMethod.POST)
    public String checkcusts(

        @ModelAttribute("customer") Customer customer, BindingResult result, Map<String, Object> model) {

        System.out.println("CheckCusts: " + customer.toString());
        return "redirect:/";
    }

}

客户.java:

package com.test.project;

public class Customer {

    private String customerId;
    private String firstName = "First";
    private String lastName = "Second";

    public String getCustomerId() {
        return customerId;
    }
    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String toString() {
        return String.format("Customer[ID=%s, firstName=%s, lastName=%s]", customerId, firstName, lastName);
    }

}

welcome.jsp:

<%@ include file="/WEB-INF/jsp/include.jsp"%>

<!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>Welcome</title>
</head>
<body>

        <form:form modelAttribute="customer" action="checkbindings">        
            <div>
                ID: <form:input path="${customerId}" />
            </div>
            <div>
                FName: <form:input path="${firstName}" />
            </div>          
            <div>
                LName: <form:input path="${lastName}" />
            </div>

            <div><form:button type="submit">SUBMIT</form:button></div>
        </form:form>


</body>
</html>

当我发布表单时,这是我看到的提交值的打印输出:

CheckCusts: Customer[ID=null, firstName=First, lastName=Second]

这种行为真的很奇怪。首先,我没有得到我在表格中输入的值。以前,当我尝试像现在一样初始化值时,我曾经得到错误“First”不是有效属性,这导致我认为框架以某种方式将初始化值解释为 bean 属性。我不再收到那个错误了。为什么绑定不起作用?为什么我看不到我在表格中输入的值?我错过了什么吗?任何帮助是极大的赞赏。谢谢

更新

这里发生了严重错误。如果我将welcome.jsp 的标记更改为以下内容:

<form:form modelAttribute="customer" action="checkbindings">    
    <div>
        ID: <form:input path="${customerIdX}" />
    </div>
    <div>
        FName: <form:input path="${firstNameX}" />
    </div>          
    <div>
        LName: <form:input path="${lastNameX}" />
    </div>

    <div><form:button type="submit">SUBMIT</form:button></div>
</form:form>

注意最后带有 X 的错误属性名称,表单仍然呈现没有错误。调用绑定时,我希望这里出现错误。 HTML 源代码如下:

<!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>Welcome</title>
</head>
<body>  
        <form id="customer" action="checkbindings" method="post">   
            <div>
                ID: <input type="text" value=""/>
            </div>
            <div>
                FName: <input type="text" value=""/>
            </div>          
            <div>
                LName: <input type="text" value=""/>
            </div>

            <div><button type="submit" type="submit" value="Submit">SUBMIT</button></div>
        </form>


</body>
</html>

请注意 form:input 标记不会生成等效的 HTML 名称属性,我猜这就是没有发生绑定的原因。然而,即使使用正确的属性名称也会发生相同的行为。这是一个春天的错误??现在真是令人沮丧。

【问题讨论】:

    标签: spring forms spring-mvc binding


    【解决方案1】:

    path 不使用${propertyName},您只使用propertyName。那就是:

    <form:input path="customerId" />
    

    ${customerId} 正在尝试使用名为“customerId”的 JSP EL 变量 作为 bean 的字段名称。由于您尚未设置任何名为“customerId”的 EL 变量,因此其值为空白。

    【讨论】:

      猜你喜欢
      • 2017-07-11
      • 2016-01-23
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 2012-08-28
      • 1970-01-01
      相关资源
      最近更新 更多