struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Copeland" <>
Subject Re: Struts2 Annotation Based Validation
Date Thu, 16 Aug 2007 14:30:24 GMT
Here's my struts.xml

    <package name="noshame" extends="struts-default">
        <action name="Register">

my web.xml:





<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form id="register" action="registerNew">
required="true" name="name" label="Your Name" />
    <s:textfield required="true" name="email" label="Email Address" />
    <s:password required="true" name="password" label="Password" />
    <s:password required="true" name="passwordConfirm" label="Confirm
Password" />
    <input class="submitButton" type=submit>

My Action class:

    @Result(name="success", value="/Success.jsp"),
    @Result(name="error", value="/Register.jsp")
public class RegisterNewAction
    public String execute() throws Exception
        // omittied boring logic
        return "success";

    private String itsName;
    @RequiredFieldValidator(type = ValidatorType.FIELD, message =
"Name is required")
    public void setName(String name) { itsName = name; }
    public String getName() { return itsName; }

    // omitted other accecssors

The docs for doing validation via XML indicate setting up some sort of
interceptor (but there is no real instruction for that, either), so is
there some annotation way I have to do that?

On 8/15/07, Laurie Harper <> wrote:
> David Copeland wrote:
> > I'm a Struts newb and am trying to get up to speed on Struts 2.
> >
> > The scant documentation is throwing me a bit; I'm trying to do
> > everything annotation-based, and for Validation, it seems to be simply
> > not working at all.
> >
> > Per the docs, I have tagged my action class with @Validator and tagged
> > my setXXX method with @RequiredFieldValidator.
> >
> > My form's JSP contains <s:fielderror><s:param>XXX</s:param></s:fielderror>.
> >
> > When I submit the form with no value for XXX, my execute method
> > executes as it would before I added the validation stuff.
> >
> > I would really like to avoid XML files, but it seems that if I'm using
> > annotations, everything needs to be done that way.
> I'm not sure what that last paragraph means; you can certainly use XML
> validation for one action and annotations in another. I'm not sure to
> what extent it's possible to mix XML and annotation based validation in
> the *same* action, but there's certainly no requirement to pick one or
> the other for the application as a whole.
> It may help if you post the relevant parts of your config, JSP and
> action code. There are several 'moving parts' involved in making
> validation work, and it's hard to guess where the problem is without
> seeing what you have done.
> L.
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message