Spring MVC Portlet:表单未正确保存数据
Spring MVC Portlet : form not saving data correctly
我正在做这个教程:
http://proliferay.com/form-submit-in-spring-mvc-portlet/
除最后一部分外一切正常。
在我的电脑上,客户表格正确显示。但是如果我将客户的详细信息设置到表单中,当我验证表单时,客户不会被保存。
因此,下一个 jsp (success.jsp) 找不到已保存的 Customer 客户,因此会显示一个包含空数据的新客户。
我的版本和教程之间的唯一区别是我必须从 Maven 构建项目,但这应该不会改变什么??
监控确认数据在 form.jsp 中是正确的,因为我可以获得正确的答案:
System.out.println("\n客户:" + request.getAttribute("customer") + "\n");
这里是class.jsp:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%System.out.println("\ncustomer : " + request.getAttribute("customer") + "\n"); %>
<portlet:actionURL var="submitFormURL" name="handleCustomer"/>
<form:form name="customer" method="post" modelAttribute="customer" action="<%=submitFormURL.toString() %>"> <%-- onSubmit="displayParams"> --%>
<br/>
<table style="margin-left:80px">
<tbody>
<tr>
<td><form:label path="firstName">First Name</form:label></td>
<td><form:input path="firstName"></form:input></td>
</tr>
<tr>
<td><form:label path="middleName">Middle Name</form:label></td>
<td><form:input path="middleName"></form:input></td>
</tr>
<tr>
<td><form:label path="lastName">Last Name</form:label></td>
<td><form:input path="lastName"></form:input></td>
</tr>
<tr>
<td><form:label path="age">Age</form:label></td>
<td><form:input path="age"></form:input></td>
</tr>
<tr>
<td><form:label path="address">Address</form:label></td>
<td><form:input path="address"></form:input></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit Form">
</td>
</tr>
</tbody>
</table>
</form:form>
以及对应的控制器方法:
@ActionMapping(value = "handleCustomer")
public void getCustomerData(
@ModelAttribute("customer") Customer customer,
ActionRequest actionRequest, ActionResponse actionResponse,
Model model) {
log.info("#############Calling getCustomerData : post form validation##########");
System.out.println("\nModel : " + model);
//displays : "Model : {customer=null null,............."
System.out.println(customer.getFirstName());
System.out.println(customer.getLastName());
System.out.println(customer.getAddress());
actionResponse.setRenderParameter("action", "success");
model.addAttribute("successModel", customer);
}
知道什么会阻止它工作吗?
提前致谢
我想到了一些您可以尝试的事情:
改变这个
model.addAttribute("successModel", customer);
进入这个
model.addAttribute("customer", customer);
您可以使用 commandName="customer"
而不是使用 name
和 modelAttribute
也许 <%@
之后缺少的 space 可能是问题所在?
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
您可能正在使用 Liferay 6.2+,如果是这样您应该设置(Spring 不是命名空间参数 - 参见 SPR-11176)
<requires-namespaced-parameters>false</requires-namespaced-parameters>
到liferay-portlet.xml
Element : requires-namespaced-parameters
Set the requires-namespaced-parameters value to true if the
portlet will only process namespaced parameters. The default
value is true.
描述来自http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd
我正在做这个教程: http://proliferay.com/form-submit-in-spring-mvc-portlet/
除最后一部分外一切正常。 在我的电脑上,客户表格正确显示。但是如果我将客户的详细信息设置到表单中,当我验证表单时,客户不会被保存。 因此,下一个 jsp (success.jsp) 找不到已保存的 Customer 客户,因此会显示一个包含空数据的新客户。
我的版本和教程之间的唯一区别是我必须从 Maven 构建项目,但这应该不会改变什么??
监控确认数据在 form.jsp 中是正确的,因为我可以获得正确的答案: System.out.println("\n客户:" + request.getAttribute("customer") + "\n");
这里是class.jsp:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%System.out.println("\ncustomer : " + request.getAttribute("customer") + "\n"); %>
<portlet:actionURL var="submitFormURL" name="handleCustomer"/>
<form:form name="customer" method="post" modelAttribute="customer" action="<%=submitFormURL.toString() %>"> <%-- onSubmit="displayParams"> --%>
<br/>
<table style="margin-left:80px">
<tbody>
<tr>
<td><form:label path="firstName">First Name</form:label></td>
<td><form:input path="firstName"></form:input></td>
</tr>
<tr>
<td><form:label path="middleName">Middle Name</form:label></td>
<td><form:input path="middleName"></form:input></td>
</tr>
<tr>
<td><form:label path="lastName">Last Name</form:label></td>
<td><form:input path="lastName"></form:input></td>
</tr>
<tr>
<td><form:label path="age">Age</form:label></td>
<td><form:input path="age"></form:input></td>
</tr>
<tr>
<td><form:label path="address">Address</form:label></td>
<td><form:input path="address"></form:input></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit Form">
</td>
</tr>
</tbody>
</table>
</form:form>
以及对应的控制器方法:
@ActionMapping(value = "handleCustomer")
public void getCustomerData(
@ModelAttribute("customer") Customer customer,
ActionRequest actionRequest, ActionResponse actionResponse,
Model model) {
log.info("#############Calling getCustomerData : post form validation##########");
System.out.println("\nModel : " + model);
//displays : "Model : {customer=null null,............."
System.out.println(customer.getFirstName());
System.out.println(customer.getLastName());
System.out.println(customer.getAddress());
actionResponse.setRenderParameter("action", "success");
model.addAttribute("successModel", customer);
}
知道什么会阻止它工作吗? 提前致谢
我想到了一些您可以尝试的事情:
改变这个
model.addAttribute("successModel", customer);
进入这个
model.addAttribute("customer", customer);
您可以使用 commandName="customer"
name
和 modelAttribute
也许 <%@
之后缺少的 space 可能是问题所在?
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
您可能正在使用 Liferay 6.2+,如果是这样您应该设置(Spring 不是命名空间参数 - 参见 SPR-11176)
<requires-namespaced-parameters>false</requires-namespaced-parameters>
到liferay-portlet.xml
Element : requires-namespaced-parameters
Set the requires-namespaced-parameters value to true if the
portlet will only process namespaced parameters. The default
value is true.
描述来自http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd