今天抽出点时间复习了一下,李刚<<struts2权威指南>>,其中有一章,关于客户端验证的,按照书上步骤,最后报了一个freemarker.template.TemplateModelException: Method public java.util.List错误,
问题:在使用struts做客户端校验时,form中增加validate="true"属性,login.jsp代码如下:
<s:form action="userAddAction" method="post" validate="true">
<s:textfield name="username" key="username"></s:textfield>
<s:textfield name="firstname" key="firstname"></s:textfield>
<s:submit key="login"></s:submit>
</s:form>
截取部分错误代码
------------------------------------------------------
异常代码:
2011-8-9 17:14:33 freemarker.log.JDK14LoggerFactory$JDK14Logger error
严重:
Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@dd89f9
The problematic instruction:
----------
==> list tag.getValidators("${tagName}") as validator [on line 46, column 9 in template/xhtml/form-close-validate.ftl]
in include "/${parameters.templateDir}/xhtml/form-close-validate.ftl" [on line 25, column 1 in template/xhtml/form-close.ftl]
----------
Java backtrace for programmers:
----------
freemarker.template.TemplateModelException: Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@dd89f9
at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130)
………
------------------------------------------------------
本着内事不决问度娘,外事不决问谷歌的态度,发现有人说了
解决办法1:
去掉表单中的validate="true"属性,即取消了Struts2框架的客户端验证,改用它的服务端验证。这不是解决问题的办法,呵呵!
解决办法2:给action补充后缀.action即可解决这个问题,修改后的login.jsp代码如下:
<s:form action="userAddAction.action" method="post" validate="true">
<s:textfield name="username" key="username"></s:textfield>
<s:textfield name="firstname" key="firstname"></s:textfield>
<s:submit key="login"></s:submit>
</s:form>
但是方法2会报另一个错误
2015-3-10 11:48:29 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: No configuration found for the specified action: 'userAddAction.action' in namespace: '/login'. Form action defaulting to 'action' attribute's literal value.
2015-3-10 11:48:29 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: No configuration found for the specified action: 'userAddAction.action' in namespace: '/login'. Form action defaulting to 'action' attribute's literal value.
解决办法3:在struts.xml中填加一个
<action name="loginInit" class="com.tungkong.action.RegistAction">
<result name="input">/login.jsp</result>
<result name="success">/success.jsp</result>
</action>
login.jsp代码如下:
<s:form action="userAddAction" method="post" validate="true">
<s:textfield name="username" key="username"></s:textfield>
<s:textfield name="firstname" key="firstname"></s:textfield>
<s:submit key="login"></s:submit>
</s:form>
访问的时候写:http://localhost:8080/test/loginInit,这样就不会报错了,如果有更好的解决方式,欢迎大家讨论!