struts1的转换器
//MyActionServlet的代码
package com.actions;
import java.util.Date;
import javax.servlet.ServletException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.struts.action.ActionServlet;
import com.convert.DateConvert;
public class MyActionServlet extends ActionServlet {
@Override
public void init() throws ServletException {
//这是注册这个转换器,因为ConvertUtils里面有很多转换器
super.init();
//这句话必须放在Init的后面,因为读actionServlet的源代码会发现,如果你设置了convertNUll = true,那么首先会把所有的转换器全部删掉,
//然后再注册这些默认的转换器,如果线面代码放在init的前面,那么也会把这个转换器删掉,那么还是会出错。因为formBean的java.util.Date还是不能转换。
ConvertUtils.register(new DateConvert(), Date.class);
}
}
//action的代码
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.forms.UserForm;
public class UserAction extends DispatchAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;
return new ActionForward("/user.jsp");
}
}
//convert的代码
package com.convert;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.Converter;
//这个类是将页面传过来的字符窜转换成某个类型
public class DateConvert implements Converter {
//其实我经过测试,struts1是不能进行双向转换器的
public Object convert(Class clazz, Object arg1) {
//如果桌面的日期的书写形式是yyyy/MM/dd,那么我们这里的格式化形式就是这种类型,如果是其他的形式就是其他的形式
SimpleDateFormat 易做图DateFormat = new SimpleDateFormat("yyyy-MM-dd");
// if(clazz == String.class)
// {
String des = (String)arg1;
java.util.Date date = null;
try {
//这是将String类型转换成Date类型
date = 易做图DateFormat.parse(des);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
// }
// else if(clazz == Date.class)
// {
// Date des = (Date)arg1;
// java.lang.String date = null;
// try {
// //这是将日期转换成string类型
// date = 易做图DateFormat.format(des);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// return date;
// }
// else
// return null;
}
}
//form的代码
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.forms;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class UserForm extends ActionForm {
private Date date;
private String name;
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//web.xml的代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.actions.MyActionServlet</servlet-class>
<init-param>
<!-- 设置这个,那么在页面的所有的界面都会设置为空,假如页面有一个身高(如果在formBean中用 int height对应),那么它的出事值会是0,这样不好看,但是如果设置了这个convertNULL,那么就会出现为空 -->
<param-name>convertNull</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
//struts-config的代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="userFo
补充:Web开发 , 其他 ,