spring-mvcliferayjsp-tagsspring-formspring-portlet-mvc

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


I have a jsp form with an input box, a domain object with get/set methods, and a controller. When I submit the form I get null values in the controller. The "set" method is never being called in the domain object when i submit the form but the object itself is being called.

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"));
}

I get null for all the print statements in the controller. Have been trying to fix this for two days now and I can't find what's wrong. Would really appreciate if someone can help me get this to work.


Solution

  • Do you have the following in your src/main/webapp/WEB-INF/liferay-portlet.xml descriptor?

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

    Also, you might want to take a look at the 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.