• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing an INTERCEPTOR and Configuring it

 
aneesh vijayakumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am using struts 2 in my application. I have to implement validation in one of my form. I was able to do the basic validation using the struts default validators.

My issue is that, I have to submit the form only when the integer value I entered is greater than a predefined value which is database driven.
I have no idea in writing the interceptor and calling it in struts.xml

The basic code i wrote was like

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class AuctionValidationInterceptor implements Interceptor {

public String intercept(ActionInvocation invocation) throws Exception {

AuctionAction auction = (AuctionAction)invocation.getAction();
Integer bidPriceTextField = 0;
auction.setBidPriceTextField(bidPriceTextField );
AutoAction auto = (AutoAction)invocation.getAction();
auto.getReservepriceTextField();
if(bidPriceTextField <auto.getReservepriceTextField())
{

}
return invocation.invoke();
}
public void destroy() {
}
public void init() {
}
}



I don't know whether it is correct or not. AuctionAction and AutoAction are the 2 action classes used.

I someone can guide me in this, I will be really greatfull.

Thanks in advance

Aneesh
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's absolutely no reason to do this in an interceptor, and IMO an interceptor would be an inappropriate place to do it: interceptors are generally for application-wide functionality.

Just implement a validate() method, call super.validate() to run the XML- and/or annotation-driven validation, then do your own DB-driven validation. (This assumes your action extends ActionSupport, if not, you'll need to implement ValidationAware and Validatable.)

(I'd also probably load the value into the application scope on application startup to avoid having to go to the database all the time for such a simple requirement; if the value is updateable simply use a synchronized value in app scope and change it, as well as the DB table, when it's updated. That's a separate issue, though.)
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"aneesh vk" please check your private messages. You can see them by clicking the My Private Messages link above.

And welcome to Javaranch
 
aneesh vijayakumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just implement a validate() method, call super.validate() to run the XML- and/or annotation-driven validation, then do your own DB-driven validation. (This assumes your action extends ActionSupport, if not, you'll need to implement ValidationAware and Validatable.)

David thanks for the reply..
but as i said i am new to this. So if you don't mind can you explain the above.
Presently I validate using the javascript. Its working fine. but i would really like to do the validation through struts.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://struts.apache.org/2.x/docs/validation.html

Start there; if you have specific questions ask on the struts-user list or here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic