错误信息
HTTP Status 500 - An exception occurred processing JSP page /2.jsp at line 33
type Exception report
message An exception occurred processing JSP page /2.jsp at line 33
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /2.jsp at line 33
30: <!-- 使用请求参数为bean赋值 http://localhost:8080/Day09/2.jsp?name=李四-->
31: <jsp:setProperty name="person" property="name" param="name"/>
32: <jsp:setProperty name="person" property="age" param="age"/><!-- 在获得请求参数的时候类型是字符串,但会自动转型为bean的类型(支持8大基本数据类型) -->
33: <jsp:setProperty name="person" property="birthday" param="birthday" />
34: <%=person.getName() %>
35: <%=person.getAge() %>
36: <%=person.getBirthday() %>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
org.apache.jasper.JasperException: org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to convert string "1980-01-01" to class "java.util.Date" for attribute "birthday": Property Editor not registered with the PropertyEditorManager
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:306)
org.apache.jsp._2_jsp._jspService(_2_jsp.java:163)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to convert string "1980-01-01" to class "java.util.Date" for attribute "birthday": Property Editor not registered with the PropertyEditorManager
org.apache.jasper.runtime.JspRuntimeLibrary.convert(JspRuntimeLibrary.java:239)
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:298)
org.apache.jsp._2_jsp._jspService(_2_jsp.java:163)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
org.apache.jasper.JasperException: Unable to convert string "1980-01-01" to class "java.util.Date" for attribute "birthday": Property Editor not registered with the PropertyEditorManager
org.apache.jasper.runtime.JspRuntimeLibrary.getValueFromPropertyEditorManager(JspRuntimeLibrary.java:805)
org.apache.jasper.runtime.JspRuntimeLibrary.convert(JspRuntimeLibrary.java:235)
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:298)
org.apache.jsp._2_jsp._jspService(_2_jsp.java:163)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.5.9 logs.
Apache Tomcat/8.5.9
源代码
2.jsp源代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP '2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="person" class="top.cheungchingyin.domain.Person" scope="page"/>
<!--手工为bean属性赋值 -->
<jsp:setProperty name="person" property="name" value="xxx"/>
<%=person.getName() %>
<!-- 使用请求参数为bean赋值 http://localhost:8080/Day09/2.jsp?name=李四-->
<jsp:setProperty name="person" property="name" param="name"/>
<jsp:setProperty name="person" property="age" param="age"/><!-- 在获得请求参数的时候类型是字符串,但会自动转型为bean的类型(支持8大基本数据类型) -->
<jsp:setProperty name="person" property="birthday" param="birthday" />
<%=person.getName() %>
<%=person.getAge() %>
<%=person.getBirthday() %>
</body>
</html>
Person.java
package top.cheungchingyin.domain;
import java.util.Date;
public class Person {
private String name = "张三";
private String age;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
原因
由于在JSP中,在获得请求参数的时候是以字符串的形式取出的,但在存向JavaBean时会自动转型为相对应的类型,如连接http://localhost:8080/Day09/2.jsp?name=%E6%9D%8E%E5%9B%9B&birthday=1980-01-01
连接带了两个请求参数:name和birthday。由于jsp的自动转换只支持基本的八大数据类型:
- byte
- short
- int
- long
- float
- double
- char
由于birthday的请求参数类型是Date类型,不在八大基本数据类型中,所以会报错。在报错的提示信息中也说道了这一点
org.apache.jasper.JasperException: org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to convert string “1980-01-01” to class “java.util.Date” for attribute “birthday”: Property Editor not registered with the PropertyEditorManager
解决方法
只能够是在servlet中把值获取出来,然后通过使用SimpleDateFormat类来把字符串转换为Date类型,SimpleDateFormat类的使用请看此链接:https://www.cnblogs.com/huangminwen/p/5994846.html