HTTP 状态 500 - javax.el.PropertyNotFoundException:在 java.lang.String 类型上找不到属性“first_name”
Posted
技术标签:
【中文标题】HTTP 状态 500 - javax.el.PropertyNotFoundException:在 java.lang.String 类型上找不到属性“first_name”【英文标题】:HTTP Status 500 - javax.el.PropertyNotFoundException: Property 'first_name' not found on type java.lang.String 【发布时间】:2015-03-14 15:15:59 【问题描述】:我不知道我的代码有什么问题,我试图在满足特定条件时从 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>
学生.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;
学生道
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)
【问题讨论】:
将first_name
重命名为 firstName
和其他此类属性并更改其访问器
你需要注意,这段代码容易受到SQL注入攻击。
出现新错误 org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/StudentForm.jsp at line 52 49: 50: 您可以通过多种方式解决此问题:-
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";
或
2) 第二种选择是按此返回 ModelAndView
What is Model in ModelAndView from Spring MVC?
【讨论】:
我尝试这样做并将所有 first_name 替换为 firstName,将 last_name 替换为 lastName,并将 cp_num 替换为 cpNum,但仍然是相同的错误,但现在它说的是 firstName 删除 JSP 的最后一部分<c:forEach items="$studentList" var="student" >
中的$studentList
不可用。如果你做了<tr><td>$studentList</td></tr>
,你可能会发现值是什么并帮助你调试。以上是关于HTTP 状态 500 - javax.el.PropertyNotFoundException:在 java.lang.String 类型上找不到属性“first_name”的主要内容,如果未能解决你的问题,请参考以下文章
验证 HTTP 200 和 500 状态码的 Alamofire 响应