如何避免Struts2验证 [英] How to avoid Struts2 validation

查看:55
本文介绍了如何避免Struts2验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个index.jsp,我从中调用操作类TestAction(单击超链接时),该类具有方法(显示)从数据库中加载组合框的值以及execute方法,以显示在页面.

test.jsp上,我有一些输入框以及组合框.单击test.jsp上的按钮后,我想验证输入字段,但是问题是当我来自index.jsp时,仅验证正在发生,并且test.jsp打开并显示错误消息.

我尝试使用<ActionName>-validator.xml进行客户端验证以及通过覆盖validate方法使用服务器端验证.如何避免在单击index.jsp的超链接时进行验证,以及如何在单击test.jsp的按钮时进行验证. /p>

这是我的代码:

index.jsp:

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Data</title>

</head>
<body>
<s:form>

   <a href="<s:url action="displayAction.action"/>" >Display Data</a><br>

</s:form>
</body>
</html>
 

test.jsp:

      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
           pageEncoding="ISO-8859-1"%>
        <%@ taglib prefix="s" uri="/struts-tags"%>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>Data</title>
     <script type="text/javascript">


           function openPopUp(){
                document.getElementById('testForm').setAttribute('target', '_blank');
                document.getElementById('testForm').setAttribute('action', 'testAction.action');
            }
            </script>
        <s:head theme="ajax" debug="true"/>
        </head>
        <body bgcolor="white">

        <s:form validate="true" id="testForm">
        <div>
      <s:actionerror/>
   </div>
        <table>

        //Mapping of data from database

        </table>
        <s:submit id="submitButton" value="Display Chart" align="center" onclick="openPopUp();"/>
        </s:form>
        </body>
        </html>
 

struts.xml

 <action name="testAction" 
        class="testAction" 
        method="execute">
             <result name="success" type="chart">
                <param name="value">chart</param>
                <param name="type">jpeg</param>
                <param name="width">600</param>
                <param name="height">400</param>
            </result> 
</action>

<action name="displayAction" 
        class="testAction" 
        method="display">
            <result name="success">test.jsp</result>
</action>
 

解决方案

我尝试使用-validator.xml进行客户端验证以及通过覆盖validate方法来使用服务器端验证.

它们都是服务器端验证,均由验证拦截器执行,并且都是 <action name="testAction" class="testAction" method="execute"> ... </action> <action name="displayAction" class="testAction" method="display"> ... </action>

并假设您只想验证第一个操作,则可以执行以下操作:

  1. 使用testAction-testAction-validation.xml(动作名称+动作别名),而不只是testAction-validation.xml;

  2. 使用validateTestAction(){}方法而不只是validate().

看看整个过程是如何工作的,因为您尚未定义任何INPUT结果,而这是可疑:)

I have a index.jsp from which I am calling an action class TestAction (on click of hyperlink) which have method(display) to load values for combobox from database along with execute method,to display on page test.jsp.

On test.jsp ,I have some input fields along with combo boxes.On click of button on test.jsp I want to validate input fields,but problem is that when i am coming from index.jsp ,that time only validation is happening and test.jsp opens with error messages.

I tried both client side validation using <ActionName>-validator.xml as well as used server side validation by overriding validate method.How can avoid validation on click of hyperlink on index.jsp and can do it on button click on test.jsp.

Here is my code :

index.jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Data</title>

</head>
<body>
<s:form>

   <a href="<s:url action="displayAction.action"/>" >Display Data</a><br>

</s:form>
</body>
</html>

test.jsp :

     <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
           pageEncoding="ISO-8859-1"%>
        <%@ taglib prefix="s" uri="/struts-tags"%>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>Data</title>
     <script type="text/javascript">


           function openPopUp(){
                document.getElementById('testForm').setAttribute('target', '_blank');
                document.getElementById('testForm').setAttribute('action', 'testAction.action');
            }
            </script>
        <s:head theme="ajax" debug="true"/>
        </head>
        <body bgcolor="white">

        <s:form validate="true" id="testForm">
        <div>
      <s:actionerror/>
   </div>
        <table>

        //Mapping of data from database

        </table>
        <s:submit id="submitButton" value="Display Chart" align="center" onclick="openPopUp();"/>
        </s:form>
        </body>
        </html>

struts.xml

<action name="testAction" 
        class="testAction" 
        method="execute">
             <result name="success" type="chart">
                <param name="value">chart</param>
                <param name="type">jpeg</param>
                <param name="width">600</param>
                <param name="height">400</param>
            </result> 
</action>

<action name="displayAction" 
        class="testAction" 
        method="display">
            <result name="success">test.jsp</result>
</action>

解决方案

I tried both client side validation using -validator.xml as well as used server side validation by overriding validate method.

They're both server-side validations, both performed by the Validation Interceptor, and both can be tweaked to be run for an action alias only.

Then with your struts.xml:

<action name="testAction" 
       class="testAction" 
      method="execute">
...
</action>

<action name="displayAction" 
       class="testAction" 
      method="display">
...
</action>

And assuming that you want to validate only the first action, you can do the following:

  1. Use a testAction-testAction-validation.xml (action name + action alias), instead of just testAction-validation.xml;

  2. Use a validateTestAction(){} method instead of just validate().

Also take a look at how the whole thing works, because you have not defined any INPUT result, and this is suspicious :)

这篇关于如何避免Struts2验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆