簡體   English   中英

如何避免Struts2驗證

[英]How to avoid Struts2 validation

我有一個index.jsp ,我從中調用一個動作類TestAction (點擊超鏈接),它有方法(顯示)從數據庫加載組合框的值和execute方法,以顯示在頁面test.jsp

test.jsp ,我有一些輸入字段和組合框。點擊test.jsp上的按鈕我想驗證輸入字段,但問題是當我來自index.jsp時,那個時候只有驗證發生了test.jsp打開時顯示錯誤消息。

我嘗試使用<ActionName>-validator.xml進行客戶端驗證以及通過覆蓋validate方法使用服務器端驗證。如何在index.jsp上點擊超鏈接時避免驗證,並且可以在按鈕上單擊test.jsp進行驗證。

這是我的代碼:

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>

如果您不想驗證某些操作,只需在操作方法上添加@SkipValidation注釋即可。 這個答案顯示了如何使用這個注釋 ,這個答案顯示了另一種方法

我嘗試使用-validator.xml客戶端驗證,並通過覆蓋驗證方法使用服務器端驗證。

它們都是服務器端驗證,都由Validation Interceptor執行,並且兩者都可以調整為僅針對動作別名運行

然后用你的struts.xml:

<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結果,這是可疑的:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM