Spring控制器编程风格

发布于 2024-10-10 04:45:51 字数 458 浏览 0 评论 0原文

我有一些 MVC 框架的经验,最近我开始对 Spring 感兴趣。我认为就我到目前为止所看到的而言,这是一个很好的框架。不管怎样,根据我过去的经验,我习惯了不同的编程风格,尤其是控制器的结构。我习惯使用控制器的方式是不同的。当然,我曾经以某种方式将请求映射到它们(想想具有各种操作的结构),但我真正喜欢的是你可以将操作传递给其他控制器,然后它们将仍然是“活动”控制器,并且那些将负责处理下一个请求的过程,它可能来自 HTTP 请求,也可能来自其他请求。我确实喜欢这个,因为以自动化的方式保持用户会话的状态非常好,使代码干净并在不同的控制器中分离不同的情况。现在我已经阅读了 Spring Web MVC,文档只讨论了应答请求的控制器,但它们不保留状态,并且没有关于链接控制器(除了转发)和状态保留的任何内容。

你如何在 spring 中处理这些主题,是否有不同的方法,或者我应该实现我自己的有状态控制器和状态/操作类?

我希望我的问题足够清楚,对于它的广泛性我深表歉意。

I come from some experience with MVC frameworks and recently I started to get interested in Spring. I think it's a good framework, for what I've seen until now. Anyway, in my past experiences I was used to a different programming style, especially for the structure of controllers. The way I was used to to employ controllers was different. Of course I used to map somehow a request to them (think of structs with various actions), but then what I really liked was that you could pass actions to other controllers, then they would remain the "active" controller and those would be responsible of handling the next request, may it come from an HTTP request or from something else. I did enjoy this because it was very good to keep the state of a user session in an automated way, making code clean and separating different situations in different controllers. Now I've read Spring Web MVC and the docs only talk about controllers that answer requests, but they don't keep state and there's no word about chaining controllers (apart from forwarding) and state retention.

How do you handle these topics in spring, is there a different way, or should I implement my own stateful controllers and state/action classes?

I hope my question is clear enough and I apologize for its broadness.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

触ぅ动初心 2024-10-17 04:45:51

在Java中,有状态/无状态Web框架之间的区别通常被描述为基于操作的框架和基于组件的框架之间的区别。

这两种方法都是有效的,但通常当您选择一个框架时,您会陷入该框架的执行方式:

流行的操作框架:

流行的组件框架:

(当然还有更多,在这两个类别中)

In Java, the difference between stateful / stateless web frameworks is usually described as the difference between action-based and component-based frameworks.

Both approaches are valid, but usually when you choose a Framework you are stuck with that framework's way of doing it:

Popular Action Frameworks:

Popular Component Frameworks:

(There are of course many more, in both categories)

迷你仙 2024-10-17 04:45:51

在 Spring MVC 中,您可以使用无状态控制器转发请求/调用会话范围的 bean。

但这是否是一个好主意很大程度上取决于您的用例。 -- 仅当只有少数这种“奇怪”控制器时我才会这样做。 -- 如果你有很多这样的问题,我认为值得寻找其他解决方案。 (例如 JSF 与 Spring)

In Spring MVC you could use the stateless controllers forwarding the request / invoking session scoped beans.

But if this is a good idea or not strongly depends on your use case. -- I would do this only if I there are only a few of this "strange" controllers. -- If you have a lot of them I think it is worth to look for an other solution. (for example JSF with Spring)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文