我在表单验证方面遇到问题,当我按下提交按钮时,没有调用 validate() 方法,但我得到 javax.servlet.ServletException: BeanUtils.populate ,当我按下取消按钮时,事情也一样,这里有一些代码:
<html:form action="/AddNews.do" >
<div class="form-horizontal">
<fieldset>
<div class="control-group">
<label class="control-label" for="newsTitle"> <bean:message
key="addnews.label.title" />
</label>
<html:text property="newsTitle" size="40" maxlength="20" />
</div>
<div class="control-group">
<label class="control-label pull-left" for="newsDate"> <bean:message
key="addnews.label.date" />
</label>
<html:textarea property="newsDate" cols="50" rows="10" />
</div>
<bean:message key="addnews.label.brief" />
<html:textarea property="newsBrief" cols="50" rows="10" />
<bean:message key="addnews.label.content" />
<html:textarea property="newsContent" cols="50" rows="10" />
<bean:message key="addnews.button.submit" />
<html:submit>
<bean:message key="addnews.button.submit" />
</html:submit>
<html:cancel ><bean:message key="addnews.button.cancel"/></html:cancel>
</fieldset>
</div>
<html:errors/>
</html:form>
public class AddNewsAction extends Action{
private static Logger logger = Logger
.getLogger(ListNewsAction.class);
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
NewsDaoImpl dao = new NewsDaoImpl();
AddNewsForm addForm = (AddNewsForm)form;
addForm.validate(mapping, request);
News newsInstance = new News();
try {
BeanUtils.copyProperties(newsInstance, addForm);
dao.addNews(newsInstance, CheckLocale.checkLocale(request));
} catch (IllegalAccessException e) {
logger.error(e);
} catch (InvocationTargetException e) {
logger.error(e);
} catch (DAOLogicException e) {
logger.error(e);
} catch (DAOTechnicException e) {
logger.error(e);
}
if( this.isCancelled( request ) )
{
System.out.println( "About to forward the cancel!" );
return mapping.findForward( "cancel");
}
else{
return mapping.findForward("listNews");
}
}
}
struts-config.xml 中的一些代码:
<form-bean name="AddNewsForm" type="by.blabla.form.AddNewsForm" />
<action path="/AddNewsPage"
type="org.springframework.web.struts.DelegatingActionProxy"
parameter="/pages/addNews.jsp" />
<action path="/AddNews"
type="org.springframework.web.struts.DelegatingActionProxy" name="AddNewsForm"
validate="true" cancellable="true" input="/pages/addNews.jsp">
<forward name="cancel" path="/listNews.do"/>
<forward name="listNews" redirect="true" path="/listNews.do" />
</action>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml" />
</plug-in>
形式:
public class AddNewsForm extends ActionForm{
/**
*
*/
private static final long serialVersionUID = -4359805626315443538L;
private String newsTitle;
private Date newsDate;
private String newsBrief;
private String newsContent;
public String getNewsTitle() {
return newsTitle;
}
public void setNewsTitle(String newsTitle) {
this.newsTitle = newsTitle;
}
public Date getNewsDate() {
return newsDate;
}
public void setNewsDate(Date newsDate) {
this.newsDate = newsDate;
}
public String getNewsBrief() {
return newsBrief;
}
public void setNewsBrief(String newsBrief) {
this.newsBrief = newsBrief;
}
public String getNewsContent() {
return newsContent;
}
public void setNewsContent(String newsContent) {
this.newsContent = newsContent;
}
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if( getNewsTitle() == null || ("".equals(getNewsTitle()))) {
errors.add("error.title",
new ActionMessage("error.required"));
}
if( getNewsDate() == null || ("".equals(getNewsDate()))) {
errors.add("error.date",
new ActionMessage("error.required"));
}
if( getNewsBrief() == null || ("".equals(getNewsBrief()))) {
errors.add("error.brief",
new ActionMessage("error.required"));
}
if( getNewsContent() == null || ("".equals(getNewsContent()))) {
errors.add("error.content",
new ActionMessage("error.required"));
}
return errors;
}
}
我在 web-inf 目录中也有 validation.xml 和 validation-rules.xml 并尝试使用 ActionForm 的 ValidatorForm 但没有任何改变,所以任何建议将不胜感激
编辑:完整的堆栈跟踪:
SEVERE: Servlet.service() for servlet [action] in context with path [/struts-blank-lastweekLATEST] threw exception [javax.servlet.ServletException: BeanUtils.populate] with root cause
org.apache.commons.beanutils.ConversionException: No value specified for 'java.sql.Date'
at org.apache.commons.beanutils.converters.AbstractConverter.handleMissing(AbstractConverter.java:310)
at org.apache.commons.beanutils.converters.DateTimeConverter.convertToType(DateTimeConverter.java:323)
at org.apache.commons.beanutils.converters.AbstractConverter.convert(AbstractConverter.java:156)
at org.apache.commons.beanutils.converters.ConverterFacade.convert(ConverterFacade.java:60)
at org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:470)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1006)
at org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:830)
at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:433)
at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:473)
at org.apache.struts.chain.commands.servlet.PopulateActionForm.populate(PopulateActionForm.java:50)
at org.apache.struts.chain.commands.AbstractPopulateActionForm.execute(AbstractPopulateActionForm.java:60)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)