Struts 2 中的客户端验证
我是 Java Web 新手。我遵循有关 struts 2 的教程: http:// Viralpatel.net
我成功创建了服务器端验证,但客户端不起作用。提交时,我注意到未定义 javascript 方法。尝试查看源代码,我发现没有生成任何脚本。 这是生成的 HTML 源代码
http://pastebin.com/Lc49jnMs
没有 javascript 'validateForm_customer()' 方法。
在 customer.jsp 中,我添加了 validate attrubute:
<s:form action="customer.action" method="post" theme="xhtml" validate="true">
并添加了:
<s:actionerror/>
<s:fielderror />
和
<s:head/>
在 struts.xml:
<action name="customer" class="mypackage.CustomerAction">
<result name="success">/success.jsp</result>
<result name="error">/customer.jsp</result>
</action>
在代码中,我扩展了 ActionSupport,并且有 CustomerAction-validation.xml 文件。 只有客户端验证不起作用,服务器端验证可以完美工作。
我使用的是struts 2.1.6。我不知道,但新版本也不适合我。 而导致的错误
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
它构建成功,但在调度程序初始化时出现一些错误,因此当使用taglib“struts-tags”时,它会抛出因
org.apache.struts2.views.gxp.inject.InjectedObjectContainer
找不到类
!在我尝试使用验证之前,struts 2.1.6 似乎工作得很好。
我做错了什么?
感谢并抱歉我的英语不好
I am new at Java Web. I follow a tutorial about struts 2 at: http://viralpatel.net
I success at create server-side validation, but the client-side do not work. When submit, I notice that a javascript method is not defined. Try viewing source, I see no script is generated.
This is generated HTML source
http://pastebin.com/Lc49jnMs
There is no javascript 'validateForm_customer()' method.
In customer.jsp, I have added validate attrubute:
<s:form action="customer.action" method="post" theme="xhtml" validate="true">
and also added:
<s:actionerror/>
<s:fielderror />
and
<s:head/>
In struts.xml:
<action name="customer" class="mypackage.CustomerAction">
<result name="success">/success.jsp</result>
<result name="error">/customer.jsp</result>
</action>
In code, i have extended ActionSupport, and I have CustomerAction-validation.xml file.
Only client-side validation do not work, the server-side validation do work perfectly.
I am using struts 2.1.6. I don't know but the newer vesion also do not work with me. It build success but have some error in dispatcher initialize, so when using taglib "struts-tags", it throw an error
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
caused by
org.apache.struts2.views.gxp.inject.InjectedObjectContainer
class not found!
The struts 2.1.6 seems work good until i try using validation.
What did I do wrong?
Thanks and sorry for my bad English
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我强烈建议您使用最新版本(出于明显的安全原因),目前为 2.3.1.1
然后删除struts2-gxp-plugin(看起来不需要),然后看看会发生什么
Firstly, I strongly recommend you to use the latest version (for obvious security reasons,), which currently is 2.3.1.1
Then remove struts2-gxp-plugin (it looks like you don't need it), after Then see what happens
经过长时间的试验,我发现验证在 web.xml 文件中设置的欢迎页面上不起作用,因此必须在欢迎页面内执行一些重定向,并将其内部重定向到下一页。并且这个重定向不会被用户注意到。下面是我的工作示例代码。
index.jsp
struts.xml
和其余代码和验证与@Viral Patel 相同
After a long trial i figured out that the validation doesn't works on welcome page set in the web.xml file, so some redirect must be carried out inside welcome page and redirect it internally to next page. And this redirect is not noticed by the user. Below is a sample code of my workings.
index.jsp
struts.xml
and the rest of the code and validation are same as of @Viral Patel
Struts 标签仅在请求通过其 servlet 过滤器时才可用 看来您错过了 web.xml 中您定义的标签的过滤器映射。确保他们在那里。
Struts tags are only usable when the request has passed through its servlet filter It seems you missed filter mappings in web.xml for the tags you defined. Make sure they are there.