我不知道我的代码有什么问题,我正在尝试在满足某些条件时从mysql数据库中检索值,它将显示在表中。这是我的代码
StudentForm.jsp
<c:url var="actionUrl" value="process" />
<form id="student-form" method="post" action="${actionUrl }">
<select name="column">
<option>*</option>
<option>last_name</option>
<option>first_name</option>
<option>cp_num</option>
<option>birthday</option>
<option>grade</option>
</select> 
<select name="condition">
<option>=</option>
<option><</option>
<option>></option>
<option><=</option>
<option>>=</option>
<option>!=</option>
</select> 
<select id="gradeID" name="grades">//grades have a dropdown list of 50-100
</select> 
<button id=submit>Submit</button><br/><br/>
</form>
<table border="1" id="studentTable">
<thead>
<tr>
<th align=center>First Name</th>
<th align=center>Last Name</th>
<th align=center>Cellphone Number</th>
<th align=center>Birthday</th>
<th align=center>Grade</th>
</tr>
</thead>
<tbody>
<c:forEach items="${studentList}" var="student" >
<tr>
<td align=center><c:out value="${student.first_name}" /></td>
<td align=center><c:out value="${student.last_name}" /></td>
<td align=center><c:out value="${student.cp_num}" /></td>
<td align=center><c:out value="${student.birthday}" /></td>
<td align=center><c:out value="${student.grade}" /></td>
</tr>
</c:forEach>
</tbody>
Student.java
@Entity
@Table(name = "student")
public class Student
{
private Long sid;
private String last_name;
private String first_name;
private String cp_num;
private String birthday;
private int grade;
@Id
public Long getSid()
{
return sid;
}
public void setSid(Long sid)
{
this.sid = sid;
}
@Column
public String getLast_name()
{
return last_name;
}
public void setLast_name(String last_name)
{
this.last_name = last_name;
}
@Column
public String getFirst_name()
{
return first_name;
}
public void setFirst_name(String first_name)
{
this.first_name = first_name;
}
@Column
public String getCp_num()
{
return cp_num;
}
public void setCp_num(String cp_num)
{
this.cp_num = cp_num;
}
@Column
public String getBirthday()
{
return birthday;
}
public void setBirthday(String birthday)
{
this.birthday = birthday;
}
@Column
public int getGrade()
{
return grade;
}
public void setGrade(int grade)
{
this.grade = grade;
}
}
控制器
@Controller
@RequestMapping("/StudentInformation")
public class StudentController
{
@Autowired
private StudentDao studentDao;
@RequestMapping(value = "/StudentForm")
public String showForm()
{
return "StudentForm";
}
@RequestMapping(value = "/process", method = RequestMethod.POST)
public String process(@RequestParam(value="column", defaultValue="") String column,
@RequestParam(value="condition", defaultValue="") String condition,
@RequestParam(value="grades", defaultValue="") int grades, Map<String, Object> map)
{
map.put("studentList", studentDao.findData(column, condition, grades));
return "StudentForm";
}
}
StudentDaoImpl
@Repository
public class StudentDaoImpl implements StudentDao
{
@Autowired
private SessionFactory sessionFactory;
@Autowired
private Student student;
private SessionFactory getSessionFactory()
{
return sessionFactory;
}
@SuppressWarnings("unchecked")
public List<Student> findData(String column, String condition, int grades)
{
String hql = "SELECT x."+ column +" FROM Student x WHERE x.grade"+ condition +":grades";
Query query = getSessionFactory().openSession().createQuery(hql);
query.setParameter("grades", grades);
List<Student> result = query.list();
return (List<Student>)result;
}
}
StudentDao
public interface StudentDao
{
public List<Student> findData(String column, String condition, int grades);
}
错误代码
HTTP Status 500 - javax.el.PropertyNotFoundException: Property 'first_name' not found on type java.lang.String
type Exception report
message javax.el.PropertyNotFoundException: Property 'first_name' not found on type java.lang.String
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: Property 'first_name' not found on type java.lang.String
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:172)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
javax.el.PropertyNotFoundException: Property 'first_name' not found on type java.lang.String
javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:266)
javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:243)
javax.el.BeanELResolver.property(BeanELResolver.java:353)
javax.el.BeanELResolver.getValue(BeanELResolver.java:97)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
org.apache.el.parser.AstValue.getValue(AstValue.java:183)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:967)
org.apache.jsp.WEB_002dINF.views.StudentForm_jsp._jspx_meth_c_005fout_005f0(StudentForm_jsp.java:290)
org.apache.jsp.WEB_002dINF.views.StudentForm_jsp._jspx_meth_c_005fforEach_005f0(StudentForm_jsp.java:241)
org.apache.jsp.WEB_002dINF.views.StudentForm_jsp._jspService(StudentForm_jsp.java:135)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:172)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
答案 0 :(得分:1)
您可以通过各种方式解决此问题: -
1)将最后一个参数从Map<String,Object>
更改为ModelMap model
@RequestMapping(value = "/process", method = RequestMethod.POST)
public ModelAndView process(@RequestParam(value="column", defaultValue="") String column,
@RequestParam(value="condition", defaultValue="") String condition,
@RequestParam(value="grades", defaultValue="") int grades,
ModelMap model)
{
model.addAttribute("studentList", studentDao.findData(column, condition, grades));
return "StudentForm";
}
OR
2)第二个选项是按此返回ModelAndView
What is Model in ModelAndView from Spring MVC?