wood burning stoves 2.0*
The moose likes Struts and the fly likes Writing an INTERCEPTOR and Configuring it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Writing an INTERCEPTOR and Configuring it" Watch "Writing an INTERCEPTOR and Configuring it" New topic
Author

Writing an INTERCEPTOR and Configuring it

aneesh vijayakumar
Greenhorn

Joined: Jul 01, 2008
Posts: 13
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jun 06, 2007
Posts: 2716
    
    6

"aneesh vk" please check your private messages. You can see them by clicking the My Private Messages link above.

And welcome to Javaranch


SCJP, SCWCD.
|Asking Good Questions|
aneesh vijayakumar
Greenhorn

Joined: Jul 01, 2008
Posts: 13
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

Joined: Sep 29, 2008
Posts: 12617

http://struts.apache.org/2.x/docs/validation.html

Start there; if you have specific questions ask on the struts-user list or here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing an INTERCEPTOR and Configuring it
 
Similar Threads
ActionInvocation.getAction() method
Get the exception object into Interceptor
Interceptors in Struts 2
Need help for refreshing the page