控制器方法的 Spring AOP 传递参数 [英] Spring AOP pass argument of a controller method

查看:43
本文介绍了控制器方法的 Spring AOP 传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器如下

@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);
    }
}

所以当我调用这个地图表单 mainpage.jsp 文件时,我得到两个值

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

${val1}
${val2}

我该怎么做???

推荐答案

您可以在 JoinPoint 上使用 getArgs 来获取方法的参数,例如:

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

Object[] signatureArgs = joinPoint.getArgs();

你的 m1 方法应该是这样的:

Your m1 method should be like:

public void m1(JoinPoint joinPoint){

您已经知道该方法只有 on 参数,因此您需要将其类型转换为映射,然后放置新值并调用继续方法以继续进行进一步的实际调用.

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.

这篇关于控制器方法的 Spring AOP 传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆