gpt4 book ai didi

java - 无法从 spring mvc portlet 中的 View 检索模型属性

转载 作者:行者123 更新时间:2023-12-02 12:24:18 26 4
gpt4 key购买 nike

这是 spring mvc portlet 的一部分,我们正在尝试从 Controller 中的 View 层检索模型属性,但它显示为 null。我们尝试同时使用 modelAttribute以及 commandName包含 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>

任何有关可能丢失的内容的线索都会有所帮助?请评论以获取所需的任何进一步详细信息。

最佳答案

尝试将您的 Controller 方法修改为

  1. 添加网址@ActionMapping(value="/someUrl")
  2. 尝试使用 BindingResult 打印绑定(bind)错误

    @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 来自此链接 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>

希望这有帮助...

关于java - 无法从 spring mvc portlet 中的 View 检索模型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45559761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com