Struts 2 中的客户端验证

发布于 2024-12-25 17:16:54 字数 1622 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两人的回忆 2025-01-01 17:16:54

首先,我强烈建议您使用最新版本(出于明显的安全原因),目前为 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

两相知 2025-01-01 17:16:54

经过长时间的试验,我发现验证在 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 相同

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

<!--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>

and the rest of the code and validation are same as of @Viral Patel

等往事风中吹 2025-01-01 17:16:54

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文