javaspring-mvcmodelliferay-6spring-portlet-mvc

Unable to retrieve model attribute from view in spring mvc portlet


This is part of spring mvc portlet and we are trying to retrieve a model attribute from the view layer in the controller,but it is showing as null. We tried using both modelAttribute as well as commandName with bean name and form input fields with path names corresponding to bean attributes.

View jsp

<form:form id="empForm" modelAttribute="empBean"  action="${createEmpURL}" method="POST" class="form-horizontal">

<form:hidden path="empId"></form:hidden>
<div class="control-group">
    <label class="control-label" for="empName">Employee Name</label>
    <div class="controls">
        <form:input type="text" id="empName" path="empName"/>
    </div>
</div>
<div class="control-group">
    <label class="control-label" for="grade">Grade</label>
    <div class="controls">
        <form:input type="text" id="grade" path="grade"/>
    </div>
</div>
<div class="control-group">
    <div class="controls">
            <input id="validateNewEmployeeButton" class="btn btn-primary" type="submit" value="Create"/>

    </div>
</div>

Controller class

    @ActionMapping  
    public void createEmp(@ModelAttribute("empBean") Employee emp, BindingResult bindingResult,ActionRequest req,ActionResponse res)
{
    System.out.println("------------->"+emp);

}

Bean class

public class Employee implements Comparable<Employee>,Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private long empId;
private String empName;
private String grade;

public long getEmpId() {
    return empId;
}

public Employee() {
    super();
    // TODO Auto-generated constructor stub
}

public void setEmpId(long empId) {
    this.empId = empId;
}

public String getEmpName() {
    return empName;
}

public void setEmpName(String empName) {
    this.empName = empName;
}

public String getGrade() {
    return grade;
}

public void setGrade(String grade) {
    this.grade = grade;
}


@Override
    public int compareTo(Employee o) {

        return (int) (this.getEmpId() - o.getEmpId());

    }
 }

Update

Used this URL for action URL:<portlet:actionURL var="createEmpURL" escapeXml="false"> </portlet:actionURL>

Any clues on what could be possibly missing would be helpful?Please comment for any further details needed.


Solution

  • Try to modify your controller method as

    1. Add url @ActionMapping(value="/someUrl")
    2. Try to print binding errors using BindingResult

      @ActionMapping(value="/someUrl")
      public void createEmp(@ModelAttribute("empBean") Employee emp, BindingResult bindingResult,ActionRequest req,ActionResponse res)
      {
          for( FieldError fieldError : bindingResult.getFieldErrors() )
              System.out.println(fieldError.getField() +" : "+fieldError.getDefaultMessage());
          System.out.println("------------->"+emp);
      }
      

    Updated:- try to create URL as

    <portlet:actionURL var="createEmpURL" name="createEmp" escapeXml="false">
    </portlet:actionURL>
    

    update 2 from this link https://web.liferay.com/community/forums/-/message_boards/message/32472440#_19_message_32690931

    Hi
    I have used following tag in liferay-portlet.xml file its working successfully..

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

    liferay-portlet.xml as follows

    <portlet>
        <portlet-name>welcome</portlet-name>
        <requires-namespaced-parameters>false</requires-namespaced-parameters>
    </portlet>
    

    Hope this help...