您的位置:首页 > 运维架构 > Apache

是用BeanUtils做工程出错org.apache.commons.beanutils.ConversionException: DateConverter does not support def

2012-09-03 17:44 585 查看
使用eclipse新建一动态工程,建好包,

新建了四个文件:ActionServlet.java\ActionForm.java\AddForm.java\FormUtil.java

WebContent:add.jsp\addsucess.jsp

导入jar包:commons-beanutils-1.8.0

建立WEB-INF下配置文件:web.xml\struts-config.xml

============================

package com.lxf.mu;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class AcionServlet extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

// TODO Auto-generated method stub

doPost(req, resp);

}

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// TODO Auto-generated method stub

//

String formfullclassname="com.lxf.mu.AddForm";

String formname="AddForm";

FormUtil.fillform(request, formfullclassname, formname);

//forward to where

request.getRequestDispatcher("/addsuccess.jsp").forward(request, response);

}

}

=============================

package com.lxf.mu;

public class ActionForm {

public void reset()

{

}

public void validate()

{

}

}

====================

package com.lxf.mu;

public class AddForm extends ActionForm {

private String sname=null;

private String major=null;

private float score=0;

private java.util.Date birth=null;

public String getSname() {

return sname;

}

public void setSname(String sname) {

this.sname = sname;

}

public String getMajor() {

return major;

}

public void setMajor(String major) {

this.major = major;

}

public float getScore() {

return score;

}

public void setScore(float score) {

this.score = score;

}

public java.util.Date getBirth() {

return birth;

}

public void setBirth(java.util.Date birth) {

this.birth = birth;

}

public String toSting()

{

String pring="sname="+sname+"\tmajor="+major+"\tbirth="+birth+"\tscore="+score;

return pring;

}

}

=====================

package com.lxf.mu;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;

public class FormUtil {

public static void fillform(HttpServletRequest request,String formfullclassname,String formname)

{

try {

ActionForm form=(ActionForm)Class.forName(formfullclassname).newInstance();

java.util.Enumeration enumr=request.getParameterNames();

Field[] field=form.getClass().getDeclaredFields();

while(enumr.hasMoreElements()){

String enumrvalue=(String)enumr.nextElement();

for(Field fieldtmp:field){

String fieldvalue=fieldtmp.getName();

if(enumrvalue.equals(fieldvalue)){

String value0 = request.getParameter(fieldvalue);

try {

org.apache.commons.beanutils.BeanUtils.setProperty(form, fieldvalue, value0);

} catch (InvocationTargetException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

request.getSession().setAttribute(formname, form);

System.out.println(form);

} catch (InstantiationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

======================

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd" >

<struts-config>

<form-beans>

<form-bean name="add" type="com.lxf.mu.AddForm"></form-bean>

</form-beans>

<action-mappings>

<action path="/add" type="com.lxf.mu.ActionForm" name="add"></action>

<forward name="" path=""></forward>

</action-mappings>

</struts-config>

===========================

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>StrutsMu</display-name>

<servlet>

<servlet-name>cfj</servlet-name>

<servlet-class>com.lxf.mu.AcionServlet</servlet-class>

<init-param>

<param-name>act</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<load-on-startup>0</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>cfj</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

</web-app>

===============

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Add--JSP</title>

</head>

<body>

<form action=" <%=request.getContextPath() %>/add.do " method="post">

sname:<input type="text" name="sname" /><br>

major:<input type="text" name="major" /><br>

birth:<input type="text" name="birth" /><br>

score:<input type="text" name="score" /><br>

<input type="submit" value="add"><br>

</form>

</body>

</html>

=====================

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Add Student Success</title>

</head>

<body>

<center>Add a Student Success!!</center>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐