Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于UserVo参数 #6

Open
zhangsunsuochang1 opened this issue Apr 5, 2022 · 3 comments
Open

关于UserVo参数 #6

zhangsunsuochang1 opened this issue Apr 5, 2022 · 3 comments

Comments

@zhangsunsuochang1
Copy link

controller中的每个函数好像都会自带UserVo参数,但是前端ajax好像并没有传递用户的信息,请问这是什么原理呢

@parkt90
Copy link
Owner

parkt90 commented Apr 5, 2022

你好,原理是:通过自定义拦截器,拦截方法;然后根据请求方法中携带的cookie从redis中获取用户信息,就为UserVo。
实现参考:
(1)拦截器accessInterceptor和userArgumentResolver注册代码在:dis-seckill-gateway\src\main\java\com\seckill\dis\gateway\config\WebConfig.java中。
(2)AccessInterceptor:dis-seckill-gateway\src\main\java\com\seckill\dis\gateway\config\access\AccessInterceptor.java中,
preHandle里面的getUser方法,可以根据cookie获取用户信息,然后保存在ThreadLocal中。
(3)userArgumentResolver:dis-seckill-gateway\src\main\java\com\seckill\dis\gateway\user\UserController.java中,
resolveArgument方法通过getUser方法从ThreadLocal中获取用户信息,然后自动解析为方法中UserVo参数。

@zhangsunsuochang1
Copy link
Author

你好,原理是:通过自定义拦截器,拦截方法;然后根据请求方法中携带的cookie从redis中获取用户信息,就为UserVo。 实现参考: (1)拦截器accessInterceptor和userArgumentResolver注册代码在:dis-seckill-gateway\src\main\java\com\seckill\dis\gateway\config\WebConfig.java中。 (2)AccessInterceptor:dis-seckill-gateway\src\main\java\com\seckill\dis\gateway\config\access\AccessInterceptor.java中, preHandle里面的getUser方法,可以根据cookie获取用户信息,然后保存在ThreadLocal中。 (3)userArgumentResolver:dis-seckill-gateway\src\main\java\com\seckill\dis\gateway\user\UserController.java中, resolveArgument方法通过getUser方法从ThreadLocal中获取用户信息,然后自动解析为方法中UserVo参数。

非常感谢及时的解答,我还有一个小问题
OrderController 中
public Result orderInfo(Model model, UserVo user, @RequestParam("orderId") long orderId)

这里的model参数又是从哪里来的呢

@parkt90
Copy link
Owner

parkt90 commented Apr 6, 2022

你好,在这个函数里面,model是没有用到的参数,可以去掉。
当前端用到Thymeleaf 模板时,Model 对象负责在控制器和展现数据的视图之间传递数据。参考知识:https://www.cnblogs.com/youcoding/p/13763621.html。
项目中代码事例:templates/goods_detail.html和goods/GoodsController.java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants