Struts 2中的客户端验证

时间:2012-01-10 16:09:53

标签: java-ee struts2 client-side-validation

我是Java Web的新手。我在http://viralpatel.net

上关注struts 2的教程

我成功创建了服务器端验证,但客户端无法正常工作。提交时,我注意到没有定义javascript方法。尝试查看源代码,我看到没有生成脚本。 这是生成的HTML源

http://pastebin.com/Lc49jnMs

没有javascript'validifyForm_customer()'方法。

在customer.jsp中,我添加了验证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。我不知道,但新的vesion也不适合我。它建立成功但在调度程序初始化时有一些错误,因此当使用taglib“struts-tags”时,它会抛出错误

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] 

引起的

org.apache.struts2.views.gxp.inject.InjectedObjectContainer
找不到课程了!

在尝试使用验证之前,struts 2.1.6似乎运行良好。

我做错了什么?

谢谢,抱歉我的英语不好

3 个答案:

答案 0 :(得分:1)

首先,我强烈建议您使用最新版本(出于明显的安全原因),目前是2.3.1.1

然后删除struts2-gxp-plugin(看起来你不需要它),然后看看会发生什么

答案 1 :(得分:1)

经过长时间的试用,我发现验证不适用于web.xml文件中设置的欢迎页面,因此必须在欢迎页面内执行一些重定向,并将其内部重定向到下一页。并且用户没有注意到该重定向。以下是我工作的示例代码。

index.jsp

<!--importing jslt library to redirect the page (jar required jstl.jar and standard.jar)  -->
<%@ taglib prefix="j"  uri="http://java.sun.com/jsp/jstl/core" %>
<!--redirection the index page to some action named index see strut.xml file  -->
<j:set var="baseUrl" scope="session" value="http://127.0.0.1:8080/strutsBasic/"/>
<j:redirect url="index" />

struts.xml中

<struts>

<constant name="struts.enable.DynamicMethodInvocation"
    value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources"
    value="ApplicationResources" />

<package name="default" extends="struts-default" namespace="/">
    <!-- at this point redirect is mapped to login page -->
    <action name="index">
    <result>/login.jsp</result>
    </action>

    <action name="login" 
        class="com.pkg.action.LoginAction" method="execute">
        <result name="success">/regform.jsp</result>
        <result name="error">/login.jsp</result>
    </action>

<action name="register" 
        class="com.pkg.action.LoginAction" method="register">
        <result name="success1">success.jsp</result>
        <result name="input">regform.jsp</result>
    </action>   

</package>

其余的代码和验证与@Viral Patel相同

答案 2 :(得分:0)

Struts标记仅在请求通过其servlet过滤器时可用您似乎错过了web.xml中针对您定义的标记的过滤器映射。确保它们在那里。

相关问题