Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Spring and the fly likes get parameter from Form page in validator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "get parameter from Form page in validator" Watch "get parameter from Form page in validator" New topic
Author

get parameter from Form page in validator

Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 122
Hi
I have a bean class (User1) in which credential is stored .Now I am filling one form in which I have written ----<form:form method="POST" commandName="user1">
<form:errors path="oldcredential" />
Credential :<form:input path="oldcredential" />

……………………………………
…………………………………….
</form:form>
Now I want to validate the data in my validator class .
public class EditCredentialValidator implements Validator{
public boolean supports(Class<?> clazz) {
return User1.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
if(user1.getCredential()==***********){
……………………………………………………
……………………………………………………..
}
}
I have to get the value which I have put against “oldcredential” at “***********”.
How I will get ?I can’t use request.getParameter(“oldcredential”) because no HttpServletRequest req, HttpServletResponse res in validate() method.
Please provide some suggestion

Thanks in advance
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 537

The parameter 'Object target' will be containing that value, you need to typecast it to your commandClass and get the value..
Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 122
So do i have to write this ---

@Override
public void validate(Object target, Errors errors) {
User1 user = (User1)target;
if(user1.getCredential()==user.getCredential()){
……………………………………………………
……………………………………………………..
}
}

I am not understanding exactly what i will have to write
sorry
please haelp

Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 122
Ok i have got one thing ,that i will have to do

@Override
public void validate(Object target, Errors errors) {
User1 user = (User1)target;
if(user.getCredential()==request.getParameter(“oldcredential”)){
……………………………………………………
……………………………………………………..
}
}

But how I will get requestParameter("oldcredential" which I have set in form) here?
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 537

Can you please clearly explain us, what you are trying to do?
Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 122
I have a form named "form.jsp" in which I have put value for a field "credential"
this value stored into a property of bean name "User1". Then I redirect it to other
form name "editform.jsp.In "editform.jsp" same field "credential" is present.I have
attached a validator against "editform.jsp" named "EditCredentialValidator" where I want
to check the two credential value is same or not .I don't want to hard code.I want to
access the HttpRequest value of editform.jsp in validator class . How I will do this ?
I have attached two forms here--

editform.jsp

<%@ taglib prefix="form" uri="/WEB-INF/spring-form.tld" %>

<form:form method="POST" commandName="user1">
<form:errors path="credential" />
Credential :<form:input path="credential" />
<form:errors path="password" />
Password :<form:password path="password"/>

<input type="submit" value="save" />
</form:form>

form.jsp

<%@ taglib prefix="form" uri="/WEB-INF/spring-form.tld" %>

<form:form method="POST" commandName="user1">
<form:errors path="credential" />
Credential :<form:input path="credential" />
<form:errors path="password" />
Password :<form:password path="password"/>

<input type="submit" value="save" />
</form:form>

In EditCredentialValidator---
@Override
public void validate(Object target, Errors errors) {
User1 user1=(User1)target;
//ValidationUtils.rejectIfEmptyOrWhitespace(errors, "credential", "credential.required","give actual value");
**** if((user1.getCredential()=="Abhra")){
errors.rejectValue("credential", "credential.unmatched","credential is not equal");
}

ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.required","give proper value");

}
}

I want to use request.getParameter("credential") or anything elese to get the value from editform.jsp


Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 537

This is not the validator is defined for.. I didn't clearly understand, what you are trying to do.. Perhaps, you can tell what you want to do, without using code, we could suggest you a better way or an alternative to this one..
Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 122
First step :-
WEB-INF/jsp/index.jsp is called where I put firstname, lastname,it stored in a bean “User”.Then through controller it goes to a jsp named “result.jsp”,there I set firstname, lastname in two session attributes , and redirect for another form name “form.jsp. (firstname,lastname will be required at the last of the project )

Second step:-
I put credential and password in WEB-INF/jsp/form.jsp , it stored in a bean “User1”.After that it goes to page “result1.jsp”, here I set these field in to session attributes and redirect to another for another form name “editform.jsp”.

Third step:-
In “editform.jsp” I put values for same field credential and password.I have set a validator for this form where I want to check that the value come from editform.jsp and
the value previously stored into User1 is same or not .If the values are same then I will proceed further
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 537

Instead of using validator, you can use method of the SimpleFormController.. So that, you get HttpServletRequest as well as you can validate them..

EDIT: Assuming you are using SimpleFormController, in case you use other controller, find the appropriate method, which does what above method does..

P:S: I am just curious to know what is business scenario/requirement driving the above code..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get parameter from Form page in validator