这是spring mvc portlet的一部分,我们正在尝试从控制器中的视图层检索模型属性,但它显示为null。
我们尝试将modelAttribute
和commandName
与bean名称一起使用,并使用与bean属性对应的路径名形式输入字段。
查看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>
控制器类
@ActionMapping
public void createEmp(@ModelAttribute("empBean") Employee emp, BindingResult bindingResult,ActionRequest req,ActionResponse res)
{
System.out.println("------------->"+emp);
}
Bean类
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
将此网址用于操作网址:<portlet:actionURL var="createEmpURL" escapeXml="false">
</portlet:actionURL>
任何可能遗漏的线索都会有所帮助?请评论所需的任何进一步细节。
答案 0 :(得分:1)
尝试将控制器方法修改为
@ActionMapping(value="/someUrl")
尝试使用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 来自此链接https://web.liferay.com/community/forums/-/message_boards/message/32472440#_19_message_32690931
您好
我在liferay-portlet.xml文件中使用了以下标记,它的工作成功..
<requires-namespaced-parameters>false</requires-namespaced-parameters>
liferay-portlet.xml如下
<portlet>
<portlet-name>welcome</portlet-name>
<requires-namespaced-parameters>false</requires-namespaced-parameters>
</portlet>
希望这有帮助...