现象描述:ActionForm中要求的是java.sql.Date类型。但是用户在jsp中输入:asasdfasdfdas等字符,点提交。
这个时候ActionServlet已经抛出SqlDateConverter.convert(SqlDateConverter.java:112)异常了。。
那么ActionForm的setDate()方法 根本就不会执行。更别说在validate()方法了问题是:如何 使 用户在jsp中输入:asasdfasdfdas等不能转换成java.sql.Date类型的字符是,跳转到一个页面,而不是抛出500异常。
补充:ActionForm的setDate()方法 根本就不会执行。更别说在validate()方法了所以 struts-config.xml中action的input属性根本就不会发生作用,也既不会跳转到 inupt="inputError.jsp"页面的。。所以,请不要说这个方法。。
这个时候ActionServlet已经抛出SqlDateConverter.convert(SqlDateConverter.java:112)异常了。。
那么ActionForm的setDate()方法 根本就不会执行。更别说在validate()方法了问题是:如何 使 用户在jsp中输入:asasdfasdfdas等不能转换成java.sql.Date类型的字符是,跳转到一个页面,而不是抛出500异常。
补充:ActionForm的setDate()方法 根本就不会执行。更别说在validate()方法了所以 struts-config.xml中action的input属性根本就不会发生作用,也既不会跳转到 inupt="inputError.jsp"页面的。。所以,请不要说这个方法。。
淘汰了,所以给你提提,希望能给你提示或帮助。
struts中没有直接把string类型转换成Date类型的转换器。要自己写一个转换器类,
把string转换成Date 。转换器类继承PropertyEditorSupport,复写setAsText方法。举个例子吧,有关spring的IoC,步骤如下:
1.写一个bean类有Date属性,提供get/set方法。2.转换器类
public class ChangeDate extends PropertyEditorSupport {
private String format = "yyyy-mm-dd";
public void setAsTest(String text) throws IllegalArgumentException{
SimpleDateFormat sdf = new SimpleDateFormat(format);
try{
Date date = sdf.parse(text);
this.setValue(date);
}catch(ParseException e) {
e.printStackTrace();
}
}
}3.配置文件application.xml
<bean id="c" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditor">
<map>
<entry key="java.util.Date">
<bean class="com.linqing.ChangeDate"/>
</entry>
</map>
</property>
<property name="date">
<value>2009-11-26</value>
</property?
</bean>
4.测试类
public class Test extends TestCase {
private BeanFactory factory;
protected void setUp() throw Exception {
factory = new ClassPathXmlApplicationContext("application.xml");
}
public void test() {
System.out.println("changedate="+bean.getDateValue());
}
}
希望能给你提示或帮助。
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
...
<init-param>
<description>java基础类型转化失败时,默认null而不是抛Exception</description>
<param-name>convertNull</param-name>
<param-value>true</param-value>
</init-param>
...
</servlet>
参见ActionServlet的API
我很感兴趣这个方法:想深问一个问题,关于struts如何知道调用那个setter方法的:
formBean 中有字段:public java.util.Date date;
方法:public void setDate(String date),等多个setter方法。
这个时候struts会根据jsp中的字段名字找到 "setFiledName",但是并没有得到某一个方法的参数的信息。而反射想要得到一个Method,是要"方法名字"和“参数类型.class”这两个才可以得到一个Method实例的。
struts如何得到一个 这个setDate(Strng date)方法的Method实例的呢????