views:

723

answers:

2

I'm using Struts 2 and the REST plugin. Validation in Struts 2 is ClassName-actionAlias-validation.xml. However, using the REST plugin the action alias is always /. For example OrdersController -> /orders -> OrdersController-orders-validation.xml. How can I get different validations depending on the REST method? Mainly, I want one type of validation for the update() method and another for the create() method.

A: 

Have you considered using annotations for the validations? That makes it simpler to tie them to the correct method.

MattMcKnight
A: 

Ahh, I didn't know that you could use annotation based validation on a specific method. I haven't used Struts that much. Thanks!

However, I did have to modify my struts.xml to allow different validations on different methods (validateAnnotatedMethodOnly had to be true). Here is what it looks like:

<?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"&gt;
<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>
Arthur