我在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名称属性不是由弹簧形式生成的:输入标签即使我指定了路径属性?谢谢
这一直困扰着我一个多星期了。真的很感激任何帮助。不知道为什么我没有得到任何有约束力的工作。我甚至创建了一个剥离项目,绑定仍然无法正常工作。这是我的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:/";
}
}
Customer.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>
注意表单:input标签不会生成等效的HTML名称属性,我猜这就是没有绑定发生的原因。即使使用正确的属性名称,也会发生相同的行为。这是一个Spring bug吗?这真的令人沮丧。
答案 0 :(得分:1)
${propertyName}
不使用path
,而只使用propertyName
。那就是:
<form:input path="customerId" />
$ {customerId}正在尝试使用名为“customerId”的JSP EL 变量的值作为bean的字段名称。由于您尚未设置任何名为“customerId”的EL变量,因此其值为空。