2

I am doing validation inside validate() method.

public void validate(){
    if(continent.equals("-1")){
        HttpServletRequest request=ServletActionContext.getRequest();  
        HttpSession session=request.getSession();  
        String s=(String)session.getAttribute("operation");
        if(s.equals("edit"))
                edit();
        else if(s.equals("add"))
                add();
        addFieldError( "Continent", "Continent must be selected");
    }
}

And in jsp(view) added form attribute validate=true

<s:form action="add" name="aaa" cssClass="yy" method="post" validate="true">
        <s:textfield name="Code" label="Code" readonly="false" cssClass="defaultTextBox"/>
        <s:textfield name="Name" label="Name" cssClass="defaultTextBox"/>
        <s:select name="Continent" label="Continent" headerKey="-1" headerValue="Select" list="continentlist" cssClass="defaultTextBox"/>
        <s:textfield name="IndepYear" label="Independance Year" cssClass="defaultTextBox" />
        <s:submit value="Save" cssClass="login login-submit" theme="simple"/>
</s:form>

But only server side validation is working. My question is -->is it not possible to add client side validation using validate() method?

Tanmoy Banerjee
  • 1,433
  • 3
  • 22
  • 31

3 Answers3

1

In Struts 2, Client Side Validation has different meanings, and totally depends on the type of theme you are using.

  1. With XHTML (default) and CSS XHTML, you can use the

    that is totally client side, Javascript based and doesn't communicate with the server.

  2. With the AJAX theme instead, you can run the

    that will contact the server, running the whole validation Stack, and (to answer your question) running your validate() methods too.


I personally prefer to use the SIMPLE theme, completely handling the HTML, the CSS and the JavaScript on my own.

Since the server-side validation is mandatory, the client-side validation is to be considered just a surplus, positive for making the page more user-friendly, and to reduce the network traffic in high users environment (you block unsuccessfull - but legit - requests before they go through the wire :)

Consider using HTML5 types with fallback on jQuery, especially if you are targeting the mobile.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • `ajax` theme is deprecated and it is not shipped with S2 latest jars. – Aleksandr M Jan 09 '15 at 09:59
  • @Andrea so how can I achieve it in my program given above. – Tanmoy Banerjee Jan 09 '15 at 10:03
  • 1
    @AleksandrM wow, I considered it old but didn't know (or remember) it was deprecated. That's a good thing, BTW. – Andrea Ligios Jan 09 '15 at 10:38
  • 2
    @tanmoy: there are not other framework out-of-the-box ways to perform client side validation using your server-side code. Then you can use server-side only, or write your own client side, duplicating your java logic in javascript. If your requirement is strict, your project big enough, and the architecture will be reused in other projects in the future, you can also consider writing a custom AJAX theme on your own, a newer, custom version of the old dojo AJAX theme, maybe based on jQuery. – Andrea Ligios Jan 09 '15 at 10:40
0

actually you shouldn't mix up server side and client side code. validate method can be invoked only in server side... So there is no way to use this method on client side. You need to write your own JS side validation as the same as server side validation.

Eugene Stepanenkov
  • 896
  • 16
  • 34
  • There is one way, though is not my preferred way. Read below for details – Andrea Ligios Jan 09 '15 at 09:44
  • @Andrea you are absolutely right, but I meant that you can make ajax call from client side, but it will be called in server side, and you will have server side validation. The same behavior can be reached by a lot of ways. – Eugene Stepanenkov Jan 09 '15 at 09:59
0

It is possible to perform AJAX validation using your server side code using struts2-jquery plugin , as shown in the Showcase under:

  • Form Forms with Validation
  • Form Forms with Custome Validation

The example that might interest you more is Form Submit without AJAX.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243