Struts 2 Request Parameter Validations (Int and Strings)

前端 未结 2 1655
既然无缘
既然无缘 2021-01-16 01:13

Let\'s say I have this URL here


  ${user.userId}
         


        
2条回答
  •  情话喂你
    2021-01-16 02:00

    ParametersInterceptor, using the XWorkConverter, would raise an Exception of type

    ognl.MethodFailedException: java.lang.NoSuchMethodException setId(java.lang.String);

    because it would search (through reflection) a Setter for a String, instead of an int.

    This error would stop the invocation in the middle of the InterceptorStack, and would send it back with the input result, that is the default result for input errors in the request.

    If you don't have an input result declared in your struts.xml, you will get an Exception of type

    No result defined for action your.package.your.Action and result input

    If you have it, then you will be redirected back to the result declared in your struts.xml (the same JSP, another JSP or an Action).

    You can handle this by outputting field errors through tag in your JSP, and eventually coloring with red borders the fields with javascript (not in your case, because they're URL parameters).

    To validate this kind of stuff properly (instead of asking Struts2 to fails and raises errors for you), you can use XML Validation, declaring an XML file in the same package of the Action with the name YourActionName-validation.xml

    In your case, you should use Required Validator and Int Validator

    (and specify 0 as min param in your Int Validator, to prevent negative values)

提交回复
热议问题