1

Hi just want to ask a design question in Spring MVC Controller. A request URL will have multiple parameters, eg. type=v1&par2=v2&par3=v3&par4=v4. Among the 4 parameters, 'type' will decide the transaction mode and the other parameters will be the input for each transaction.

How will you organize the controller code? Traditionally, you may write something like the following,

if (type.equals('1')) 
{
   callMode1(par2,par3, par4);
} 
else if (type.equals('2') 
{
    callMode2(par2, par3, par4);
}
else if (type.equals('3'))
{
    callMode3(par2, par3, par4);
}

callMode1 - callMode2 are private methods in the controller.

Just wondering if there is any better way to write the code here?

Thanks in advance!

1
  • What do you mean by 'transaction mode'. Are your trying to change tx isolation via an http paramater? Commented Jan 21, 2011 at 2:52

3 Answers 3

3

You should create one request handling method for each "type". Each of this method needs a @RequestMapping annotation.

The RequestMapping annotation provides an attribute called params:

abstract String[] params

The parameters of the mapped request, narrowing the primary mapping.

Same format for any environment: a sequence of "myParam=myValue" style expressions, with a request only mapped if each such parameter is found to have the given value. Expressions can be negated by using the "!=" operator, as in "myParam!=myValue". "myParam" style expressions are also supported, with such parameters having to be present in the request (allowed to have any value). Finally, "!myParam" style expressions indicate that the specified parameter is not supposed to be present in the request.

Then you can implement it in this way:

@Controller
@RequestMapping( what ever you have in common )
class Handler {

  @RequestMapping(params={"type=1"})
  public void mode1() {
     ...
  }

  @RequestMapping(params={"type=2"})
  public void mode2() {
     ...
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this works well. How about the other scenarios that is not defined here? ie. 'type' is an unexpected value?
Having read the javadoc, I should use @RequestMapping({}) for other undefined request mapping. :)
1

Have a look at using the @RequestMapping annotation to separate out your three mode methods into public request methods. For example:

@RequestMapping(value="/type/1")
public void mode1() {
   ...
}

@RequestMapping(value="/type/2")
public void mode2() {
   ...
}

@RequestMapping(value="/type/3")
public void mode3() {
   ...
}

You'll need to change your URLs to /type/1/..., etc but then you'll be able to able to access your other variables as method arguments. Read the whole MVC page in the Spring docs for other ideas too. Good luck!

Comments

0

I find this as a good use case for an enum:

public enum Type {
    T1 {
        @Override
        public void call(String par1, String par2, String par3) {
            callMode1(par1, par2, par3);
        }
    },
    T2 {
        @Override
        public void call(String par1, String par2, String par3) {
            callMode2(par1, par2, par3);
        }
    },
    T3 {
        @Override
        public void call(String par1, String par2, String par3) {
            callMode3(par1, par2, par3);
        }
    },

    public abstract void call(String par1, String par2, String par3);

    public static Type valueOfNumber(String number) {
        return valueOf("T" + number);
    }

}

Then, your controller will simply get the corresponding Type and invoke call on it:

Type t = Type.valueOfNumber(type);
t.call(par1, par2, par3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.