LynAs
LynAs

Reputation: 6537

Spring AOP pass argument of a controller method

I have a controller as follows

@Controller
@RequestMapping(value = "/")
public class HomeController {
    @RequestMapping("/")
    public String home(Map<String,Object> map) {
        map.put("val2","val2");
        return "mainpage"; //it is the jsp file name
    }
}

Now In my aspect class method I want to put another value in this map variable defined in the controller method

@Aspect
public class UserInfo {
    @Before("execution(* org.controller.HomeController(..)) ")
    public void m1(){
        //Map<String,Object> map
        // get the map here and add
        map.put("val1","val1);
    }
}

so that when i call this map form mainpage.jsp file I get both value as

${val1}
${val2}

How can I do this???

Upvotes: 2

Views: 3283

Answers (2)

kriegaex
kriegaex

Reputation: 67297

Actually SMA's solution is nice, but not very elegant and type-safe. How about binding the argument to a correctly typed and conveniently named argument right in the pointcut?

@Before("execution(* org.controller.HomeController.*(..)) && args(map)")
public void m1(Map<String, Object> map) {
    map.put("AspectJ", "magic");
}

Upvotes: 0

SMA
SMA

Reputation: 37023

You could use getArgs on JoinPoint to get the argument to the method like:

Object[] signatureArgs = joinPoint.getArgs();

Your m1 method should be like:

public void m1(JoinPoint joinPoint){

You already know that you just have on argument to the method, so you would need to type cast it to a map and then put your new values and call proceed method to proceed with further actual call.

Upvotes: 2

Related Questions