我正在尝试开发一个spring应用程序来显示表中的员工和选择单选按钮,过滤后的结果应该显示在同一个表中(相同的jsp)。我试过下面,但得到NotReadablePropertyException。请帮忙。
EmployeeController.java
@Controller
public class EmployeeController {
/** Service object to handle web request */
@Autowired
protected EmployeeService empService;
/**
* This method will form the list of employees and return the string to be
* used for spring to fetch the appropriate JSP
*
* @param model
* - Spring Model class object
* @return String - to be used to help spring to fetch the appropriate JSP
*/
@RequestMapping(value = "/employees", method = RequestMethod.GET)
public String getEmployees(Model model) {
List<EmployeeDetails> empList = empService.getEmployeeList();
model.addAttribute("employees", empList);
return "employees";
}
/**
*
*/
@RequestMapping(value = "/filteremployees", method = RequestMethod.POST)
public ModelAndView getFilteredData(
@ModelAttribute("salaryFilterBean") SalaryFilterBean salaryFilterBean) {
System.out.println("Inside controller to filter data");
List<EmployeeDetails> filteredEmpList = empService
.getFilteredEmployeeListBySalary(salaryFilterBean
.getFilteremployees());
System.out.println("filtered data of employee ::: "
+ filteredEmpList.get(0).getEmpSalary());
ModelAndView model = new ModelAndView("employees");
model.addObject("employees", filteredEmpList);
model.addObject("salaryFilterBean", new SalaryFilterBean());
return model;
}
}
SalaryFilterBean.java
public class SalaryFilterBean {
private String filteremployees = null;
/**
* @return the filteremployees
*/
public String getFilteremployees() {
return filteremployees;
}
/**
* @param filteremployees
* the filteremployees to set
*/
public void setFilteremployees(String filteremployees) {
this.filteremployees = filteremployees;
}
}
employees.jsp
<!DOCtype html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=utf8"
pageEncoding="utf8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="English" />
<link rel="stylesheet" media="all"
href="<c:url value="/WEB_INF/css/site.css"/>">
<title>Employee Details</title>
</head>
<script>
function submitForm() {
document.getElementById("filterForm").submit();
}
</script>
<body>
<form:form id="filterForm" modelAttribute="salaryFilterBean"
action="/filteremployees" method="post" commandName="employees">
<form:radiobutton path="filteremployees" value="< 10000"
onclick="submitForm()" /> Salary < 10000
<form:radiobutton path="filteremployees" value="< 20000"
onclick="submitForm()" /> Salary < 20000
<form:radiobutton path="filteremployees" value="> 40000"
onclick="submitForm()" /> Salary < 20000
</form:form>
<BR>
<table border="0" style="border: 1px #000000 solid">
<tr>
<th border="0" style="border: 1px #000000 solid">Id</th>
<th border="0" style="border: 1px #000000 solid">Name</th>
<th border="0" style="border: 1px #000000 solid">Date of birth</th>
<th border="0" style="border: 1px #000000 solid">Experience
(Years)</th>
<th border="0" style="border: 1px #000000 solid">Salary</th>
</tr>
<c:forEach items="${employees}" var="employee">
<tr>
<td border="0" style="border: 1px #000000 solid">${employee.empId}</td>
<td border="0" style="border: 1px #000000 solid">${employee.empName}</td>
<td border="0" style="border: 1px #000000 solid">${employee.empDob}</td>
<td border="0" style="border: 1px #000000 solid">${employee.empYoe}</td>
<td border="0" style="border: 1px #000000 solid">${employee.empSalary}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
堆栈跟踪:
org.springframework.beans.NotReadablePropertyException: Invalid property 'filteremployees' of bean class [java.util.ArrayList]: Bean property 'filteremployees' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:707)
org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:699)
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:147)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:179)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:165)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:152)
org.springframework.web.servlet.tags.form.AbstractCheckedElementTag.autogenerateId(AbstractCheckedElementTag.java:81)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:143)
org.springframework.web.servlet.tags.form.AbstractSingleCheckedElementTag.writeTagContent(AbstractSingleCheckedElementTag.java:83)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:103)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
org.apache.jsp.WEB_002dINF.pages.employees_jsp._jspx_meth_form_005fradiobutton_005f0(employees_jsp.java:212)
org.apache.jsp.WEB_002dINF.pages.employees_jsp._jspx_meth_form_005fform_005f0(employees_jsp.java:166)
org.apache.jsp.WEB_002dINF.pages.employees_jsp._jspService(employees_jsp.java:93)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
我在哪里错了?
答案 0 :(得分:2)
commandName="employees"
您的commandName设置为
的员工 model.addObject("employees", filteredEmpList);
是
List<EmployeeDetails> filteredEmpList
并且您正在尝试绑定
<form:radiobutton path="filteremployees" value="< 10000"
onclick="submitForm()" /> Salary < 10000
到它
因此无法在setFilteremployees()
filteredEmpList
方法
答案 1 :(得分:0)
解决了这个问题。
在form标签的commandName中,值应为bean类名。 Spring将使用项目的bean类映射此commandName值。
<强>修正:强>
<form:form id="filterForm" modelAttribute="salaryFilterBean"
action="/filteremployees" method="post" commandName="salaryFilterBean">
<form:radiobutton path="filteremployees" value="< 10000"
onclick="submitForm()" /> Salary < 10000
<form:radiobutton path="filteremployees" value="< 20000"
onclick="submitForm()" /> Salary < 20000
<form:radiobutton path="filteremployees" value="> 40000"
onclick="submitForm()" /> Salary < 20000
</form:form>