无法从 spring mvc portlet 中的视图检索模型属性

Unable to retrieve model attribute from view in spring mvc portlet

这是 spring mvc portlet 的一部分,我们正在尝试从控制器的视图层检索模型属性,但它显示为空。 我们尝试将 modelAttributecommandName 与 bean 名称一起使用,并在表单输入字段中使用与 bean 属性相对应的路径名。

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

将此 URL 用于操作 URL:<portlet:actionURL var="createEmpURL" escapeXml="false"> </portlet:actionURL>

关于可能遗漏什么的任何线索都会有所帮助?请发表评论以获取所需的任何进一步详细信息。

尝试将您的控制器方法修改为

  1. 添加url@ActionMapping(value="/someUrl")
  2. 尝试使用 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);
    }
    

更新:- 尝试将 URL 创建为

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

更新 2 从此 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如下

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

希望这对您有所帮助...