Friday, February 29, 2008

Strutes2 client side Custem Validations to password confirmation and string equals

Hi ,
Can some body tell me a way to do the password validation in client side by doing a custom validations. It is not possible to check string equal with current one.

any suggestions please.

tnx.

Reply i.

You can do this buy creating a class by extending FieldValidatorSupport as following example
and creating a validator.xml and relevant validation xml as show in the image below.

Example for FieldValidatorSupport extended class,

import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
import com.opensymphony.xwork2.validator.*;

public class EqualsStringValidator extends FieldValidatorSupport {


private String fieldName2;

public String getFieldName2() {
return fieldName2;
}

public void setFieldName2(String fieldName2) {
this.fieldName2 = fieldName2;
}

public void validate(Object o) throws ValidationException {
//
String fieldName = getFieldName();
String fieldName2 = getFieldName2();

System.out.println("fieldName-------->>> " + fieldName);

String val = (String) getFieldValue(fieldName, o);
String val2 = (String) getFieldValue(fieldName2, o);

System.out.println("Val >>" + val + "Val2>>>" + val2);


}

and do the following , click on the image to see it.

Wednesday, February 27, 2008

How to identify the Input error in Struts2

Hi I'm getting input error when i submit the page . How i can identify the error and solve this.
The issues comes with check box.

tnx

  • Reply 1.

You can identify the error by adding a result tag named as input to ( result name="input") "-pages/xxx.jsp-"
and add a s:fieldError to that xxx.jp

thus , when you submit the data it will show the error on the xxx.jsp page.


  • Reply 2.
Thanks . It works and i got the error , it was assigning a int value to check box.
It is really easy to get an idea about the error with this.

Restric the values in S:TextFields.

Hi all ,

Can some body please tell me a way to restrict the data that we can eneter to a textfield in struts2.

What I wanted to do is , for the s:textfield , I need to enter only

values these values {1-10 , +,-,.} .

Thank you.

  • Reply 1.

The only way I know would be to use JavaScript in an onkeyup handler that would check the entered value.

(*Chris*)

  • Reply 2.

On the server-side, use an expression validator to ensure the input contains only valid characters.

On the client-side either:

- use ajax validation; or

- use a javascript function that listens for the keypress event and rejects invalid keystrokes

As usual, the javascript keypress event on IE is different from all other browsers so the function has to include browser detection.

Here is the gist of what you need:

javascript:

function filterKeypress(e) {

var accept = /[\d|\+|\-|\.]/; // a regex pattern of accepted chars

var keyChar;

if (msie) { // use your favourite broswer detection function

keyChar = String.fromCharCode(e.keyCode);

} else {

keyChar = String.fromCharCode(e.which);

}

// check if the character is acceptable

if (!accept.test(keyChar)) {

e.preventDefault(); // if it's not, prevent the keypress event

from proceeding

}

}

  • Reply 4.

It works Thanks.

Monday, February 25, 2008

Struts2 Validaions

Hi ,

I got a problem in struts2 validations.
I have used both xml based and annotation based validations
But , The error messages don't get cleared once we get a error message and it will repeat in the UI.

This will happen with the simple theme ,as if we don't use the theme the components will not be placed in the proper way.

That issue was not there when we dot use a theme but the components were here and there.

So can u please help me to use the validations correctly with a example.

Reply 1.

Actually , i got the same problem and i change the ( override )

simple theme by changing the form-close_validate.ftl , form_validate.ftl files and adding a error list to it and use that error list in the code to show all errors and clear that list.



Struts2 - Session Invalidation

Hi all,

Can somebody please tell me how can I invalidate a session in struts2 .
I tried the following ,

if (session instanceof org.apache.struts2.dispatcher.SessionMap) {

try {

((org.apache.struts2.dispatcher.SessionMap)

session).invalidate();

} catch (IllegalStateException e) {

logger.error(e);

}

}

But still I can go to the pages in my web project and do the operations. Can somebody please suggest me a solution for this.



  • Reply 1.

try

this

((org.apache.struts2.dispatcher.SessionMap)

ActionContext.getContext().getSession()).invalidate();

  • Reply 2

HI ,

I think it do not remove the data in the session after we invalidate it.

So do you have any idea of removing the data in the session.



  • Reply 3.

((org.apache.struts2.dispatcher.SessionMap)

> ActionContext.getContext().getSession()).clear();

It might have been quicker to look up the JavaDocs.