从 jsp 提交数据时在 Spring MVC 控制器中获取空值

Getting null values in Spring MVC controller when submitting data from the jsp

我有一个带有输入框的 jsp 表单、一个带有 get/set 方法的域对象和一个控制器。当我提交表单时,我在控制器中得到空值。当我提交表单但对象本身被调用时,"set" 方法从未在域对象中被调用。

Order.jsp

<portlet:defineObjects />

<portlet:actionURL portletMode="view" var="createNewOrderURL">
<portlet:param name="action" value="createNewOrder" />
</portlet:actionURL>

<div>
<form:form name="form" method="post" commandName="refOrder" action="${createNewOrderURL}" id="createOrder">
    TestName : ${refOrder.name}<br/> <!-- here I get the correct value to display -->

    <form:input  path="referenceNum" />

    <input type="submit" value="Submit" />

</form:form>
</div>

Order.java

public class Order {
private String name = "Name1";
private String referenceNum;

public Order(){
    System.out.println("Inside Order.java");
    System.out.println(getReferenceNum());
}



 public Order(String name, String referenceNum) {
    this.name = name;
    this.referenceNum = referenceNum;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getReferenceNum() {
    return referenceNum;
}

public void setReferenceNum(String referenceNum) {
    this.referenceNum = referenceNum;
}

SalesOrderController.java

@RenderMapping 
public String defaultRender(RenderRequest request, RenderResponse response, Model model) throws SQLException, NamingException{

    model.addAttribute("refOrder",new Order());
    return "SalesOrderEntry";
}


@ActionMapping(params={"action=createNewOrder"})
public void addNewOrder(ActionRequest request, ActionResponse response, @ModelAttribute("refOrder") Order order)throws NamingException, SQLException{
    System.out.println("Inside addNewOrder method");
    System.out.println("New Order is --> "+order.toString());
    System.out.println("RefNum - "+order.getReferenceNum());
    System.out.println("request.getParameter is "+request.getParameter("referenceNum"));
}

控制器中的所有打印语句都为空。这两天一直在尝试解决这个问题,但我找不到问题所在。如果有人可以帮助我让它工作,我将不胜感激。

您的 src/main/webapp/WEB-INF/liferay-portlet.xml 描述符中是否包含以下内容?

<requires-namespaced-parameters>false</requires-namespaced-parameters>

此外,您可能想看看 PortletMVC4Spring project, which is the successor to Spring Portlet MVC. The GitHub project contains archetypes that work in Apache Pluto and Liferay Portal. The requires-namespaced-parameters config option is conveniently set in the archetypes