JSP+JavaBean不能使用jsp:setProperty name="BeanName" property="*"/>来自动提交日期表单

解决问题点:JSP+JavaBean不能使用jsp:setProperty name="BeanName" property="*"/>来自动提交日期表单

 

SQL Server 2000 + Tomcat 5.0

1. DB script

create table basic_employee(id varchar(10) not null primary key, birthday datetime not null)

 

2. JavaBean

(1)EmployeeInfo.java

import java.util.Date;

public class EmployeeInfo {
 String id;
 Date birthday;
 
 public Date getBirthday() {
  return birthday;
 }
 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
}

 

(2)EmployeeRegist.java


import java.sql.Connection;
import java.sql.Date;
import java.sql.Timestamp;
import java.sql.PreparedStatement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import com.easysoft.db.*;
public class EmployeeRegist {
 private EmployeeInfo employeeInfo;
 private DBConnect dbConnect;
 private Connection conn;
 private int i = 0;
 
 public void setEmployeeInfo(EmployeeInfo employee) {
  this.employeeInfo = employee;
 }
 
 public final static java.sql.Timestamp string2Time(String dateString)
   throws java.text.ParseException {
    DateFormat dateFormat;
   dateFormat = new SimpleDateFormat("yyyy-MM-dd");
   dateFormat.setLenient(false);
   java.util.Date timeDate = dateFormat.parse(dateString);
   java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());
   return dateTime;
  }

 public void regist() throws Exception {
  String reg = "insert into basic_employee(id,birthday) " +
  "values(?, ?)";
  
  try {
   conn = dbConnect.getConnection();
   PreparedStatement pstmt = conn.prepareStatement(reg);
   pstmt.setString(++i, employeeInfo.getId());
   System.out.println(employeeInfo.getBirthday()); 
   pstmt.setTimestamp(++i, new Timestamp(employeeInfo.getBirthday().getTime()));
   //Execute update
   pstmt.executeUpdate();
  } catch (Exception e) {
   e.printStackTrace();
   throw e;
  }
 }
}

3. JSP

(1)register.jsp

 <%@ page language="java" contentType="text/html;charset=gb2312"%>

 
    Create new Employee information
 
 
 
   


   
   
   
   
   
   
   
   
   
   
   
   
   
   
*ID
*Birthday

   

 

(2)newEmployee.jsp
<%@ page language="java" contentType="text/html;charset=gb2312" import="com.easysoft.employee.*" %>
<%
String idStr = request.getParameter("id");
String birthday = request.getParameter("birthday");
EmployeeInfo employeeInfo = new EmployeeInfo();
EmployeeRegist regist = new EmployeeRegist();
employeeInfo.setId(idStr);
employeeInfo.setBirthday(regist.string2Time(birthday));
regist.setEmployeeInfo(employeeInfo);
regist.regist();
out.println("Create Employee info success.");
%>

 

如果把newEmployee.jsp,

<%@ page language="java" contentType="text/html;charset=gb2312" import="com.easysoft.employee.*" %>





<%
regist.setEmployeeInfo(employeeInfo);
regist.regist();
out.println("Create Employee info success.");
%>

 

会抛出异常:

org.apache.jasper.JasperException: jsp.error.beans.property.conversion
org.apache.jasper.runtime.JspRuntimeLibrary.getValueFromPropertyEditorManager(JspRuntimeLibrary.java:931)
org.apache.jasper.runtime.JspRuntimeLibrary.convert(JspRuntimeLibrary.java:313)
org.apache.jasper.runtime.JspRuntimeLibrary.internalIntrospecthelper(JspRuntimeLibrary.java:399)
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:352)
org.apache.jasper.runtime.JspRuntimeLibrary.introspect(JspRuntimeLibrary.java:330)
org.apache.jsp.managestudent_jsp._jspService(managestudent_jsp.java:84)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

 

 

 原因是:jsp:setProperty name="employeeInfo" property="*"/>无法自动转换为日期类型。

 

property="*" -->只能自动转换Boolean, Byte,String, int, float 等等类型,不包含Date类型,所以在提交的时候必须自己转换。不知道这样的理解是否正确。

 


 

你可能感兴趣的:(JSP)