Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

java - Changing parameters after bind in Struts 2

I have an action that receives some parameters from user (e.g. date). This action produces many different reports, so it has many different methods. I need to tune those parameters (set a time to midnight) before every method. The prepare method is executed before parameters are bound. Is there any other interceptor or any other convention that allows me to do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Another way to go (cheap if you are coding right now, expensive if you've already coded everything) would be to modularize every action you have to perform in one single Struts2 Action.

Then you will have something like an BaseReportAction, containing all the common attributes and methods shared using protected instead of private, doing your tuning on parameters and the common operations in the execute() method;

And one Action for each report extending the BaseReportAction, let's say

ExcelReportAction, PdfReportAction, etc...

or

MinimalReportAction, CompleteReportAction, etc...

or also

DailyReportAction, MonthlyReportAction, etc...

And the only requirement would be using super.execute(); as first statement of every child Action's execute() method.

This way you could take advantage of the inheritance, to have a lot of smaller, cleaner (eventually packaged into several sub-packages) Actions instead of one huge Action with a lots of methods.

All the utility methods used by few reports would be available only for those reports, not for all the others (let's say PDF and XLS stuff for example)...

You could benefit of the XML Validation too for different Actions (maybe one report requires different inputs from another).

Finally, your tuning-up code would be Thread-Safe (Actions are Thread-Safe, Interceptors don't).

But as said, this is a choice of implementation that better suits the pre-code phase (even if it's not that hard to refactor, according to the size of the web application...).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...