Spring MVC:将控制器+动作名称传递给视图的最佳方法是什么? [英] Spring MVC : What is the best way to pass the controller + action name to the view?

查看:89
本文介绍了Spring MVC:将控制器+动作名称传递给视图的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让视图知道调用哪个控制器和执行什么操作来生成它.实际上,我想在视图中执行以下操作:

I want the view to know what controller and what action were called to generate it. In fact, I want to do something like this in the view:

<script>
    var controller = "${controllerName}";
    var controllerAction = "${controllerAction}";
</script>

,以及后来包含的.js,

and, in a later included .js,

<script src="/public/script.js"></script>

...在此.js中,我想访问两个变量:

... in this .js I want to have access to both variables:

if(controller == "UserController" && controllerAction == "Edit")
{
    // ...
}
else
{
    // ...
}

我的问题:将那些控制器和动作名称自动传递给视图的最佳方法是什么?

My question: what would be the best way to automatically pass those controller and action names to the view?

我的控制器扩展了一个抽象的"BaseController",所以我猜那里是将这些变量添加到ModelAttribute的最佳位置.我希望具体的控制器对要添加的那些变量不做任何事情.但是父级控制器如何知道子级具体控制器的名称和操作的名称?

My controllers extend an abstract "BaseController", so I guess the best place to add those variables to the ModelAttribute would be there. I would prefere the concrete controllers to have nothing to do for those variables to be added. But how could the parent controller know the name of the child concrete controller and the name of the action?

我可以在BaseController中检查StackTrace,但是我不太喜欢这种解决方案...

I could check the StackTrace in BaseController, but I don't really like this solution...

您有更好的建议解决方案吗?

Do you have a better solution to suggest?

推荐答案

为什么不能仅从URL中获取此信息?通常,URL映射到Spring中的控制器和动作.在JavaScript中,为什么不只解析URL来获取此信息?如果问题是操作因帖子,获取或标题信息而不同,则可以将其作为参数附加在url中.

Why can't you just get this information from your url? normally a url maps to a controller and action in Spring. In javascript why not just parse the url to get this info? If the problem is that the action is different depending on a post, get or header information then you can probably append this in the url as a parameter.

这篇关于Spring MVC:将控制器+动作名称传递给视图的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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