How to deal with the monstrous actions of Struts?

I inherited this gigantic legacy of a Java web application using Struts 1.2.4. I have a specific question about actions. Most pages have exactly one action, and the processExecute () methods are disgusting monsters (very long and tons of nested if statements based on query parameters).

Given that Actions is an implementation of a command template, I’m going to split these actions into one action for each user gesture. It will be a lot of refactoring, though, and I'm curious:

  • Is it correct?
  • Is there an intermediate step that I could take, a model that deals with the disorder within monolithic actions? Maybe a different command template inside Action?
+5
source share
7 answers

My way to handle this:

  • Do not do it all at once.
  • whenever you change anything, leave it better than you found it
    • replacing conditional expressions with individual Action implementations is one step.
    • Better yet: make your implementations separate from the Action classes so you can use them when changing frameworks
    • Save your new Command implementation without references to Struts, use your new actions as Wrapper around these implementations.
    • Struts ActionForms, . , , ActionForms, (. Struts 1.2 ActionForms).
  • . Struts 1.2 , , , . .

- , ...

+9

Struts, , . - , , Response , , .

. , , , , . Struts , , , -, .

.

+5

. Action ( ClassA). , . Action (ClassB, ClassC). , ClassA , . , :

Original Hierarchy:      New Hierarchy:

     Action                   Action
       |                        |
       |                      BaseA
  (old)ClassA                   |
                       +--------+----------+
                       |        |          |
                   ClassB (new)ClassA   ClassC
+2
  • , . ( , , )
  • , , , .
  • , .

. .

, .

, , , .

, , .

+2

, -.

, -, "" ( , ), , .

; ... - - ... struts / .

+1

, switch, ( - ).

.

, , , , , /, . ifs, , , .

enum Operation {
  ADD, DELETE;
}

...

Operation operation = determineOperation(form);
if (operation == Operation.DELETE) { 
  doDelete(form); 
} else if (operation == Operation.ADD) {
  doAdd(form);
}

, , , .

- , . , , .

+1

, , , .

+1

All Articles