2009-12-20 86 views
1

我正在使用Struts 2和REST插件。 Struts 2中的驗證是ClassName-actionAlias-validation.xml。但是,使用REST插件時,操作別名始終爲/。例如OrdersController - >/orders - > OrdersController -order-validation.xml。 如何根據REST方法獲得不同的驗證?主要是,我想要一種類型的update()方法的驗證,另一種是create()方法。Struts 2 REST和驗證

回答

0

啊,我不知道你可以使用基於特定方法的基於註解的驗證。我還沒有使用過Struts。謝謝!

但是,我確實必須修改我的struts.xml以允許對不同方法進行不同的驗證(validateAnnotatedMethodOnly必須爲true)。下面是它的樣子:

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
    "http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
    <constant name="struts.convention.action.suffix" value="Controller" /> 
    <constant name="struts.convention.action.mapAllMatches" value="true" /> 
    <!-- Set to "default" instead of "rest-default" --> 
    <constant name="struts.convention.default.parent.package" 
     value="default" /> 

    <constant name="struts.convention.package.locators" value="rest" /> 

    <package name="default" extends="rest-default"> 
     <interceptors> 
      <interceptor-stack name="restDefaultStack"> 
       <interceptor-ref name="exception" /> 
       <interceptor-ref name="alias" /> 
       <interceptor-ref name="servletConfig" /> 
       <interceptor-ref name="messages"> 
        <param name="operationMode">AUTOMATIC</param> 
       </interceptor-ref> 
       <interceptor-ref name="prepare" /> 
       <interceptor-ref name="i18n" /> 
       <interceptor-ref name="chain" /> 
       <interceptor-ref name="debugging" /> 
       <interceptor-ref name="profiling" /> 
       <interceptor-ref name="actionMappingParams" /> 
       <interceptor-ref name="scopedModelDriven" /> 
       <interceptor-ref name="modelDriven"> 
        <param name="refreshModelBeforeResult">true</param> 
       </interceptor-ref> 
       <interceptor-ref name="fileUpload" /> 
       <interceptor-ref name="checkbox" /> 
       <interceptor-ref name="staticParams" /> 
       <interceptor-ref name="params"> 
        <param name="excludeParams">dojo\..*</param> 
       </interceptor-ref> 
       <interceptor-ref name="rest" /> 
       <interceptor-ref name="conversionError" /> 
       <interceptor-ref name="validation"> 
        <param name="excludeMethods">input,back,cancel,browse,index</param> 
        <!-- Modified! --> 
        <param name="validateAnnotatedMethodOnly">true</param> 
       </interceptor-ref> 
       <interceptor-ref name="restWorkflow"> 
        <param name="excludeMethods">input,back,cancel,browse,index</param> 
       </interceptor-ref> 
      </interceptor-stack> 

     </interceptors> 
    </package> 
</struts>