Spring控制器设置问题? [英] Spring controller setup question?

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

问题描述

我正在使用Spring MVC开发Java webapp。我有如下设置:该网站的2个页面负责呈现类似的内容。其中一个页面只是略微不同地呈现项目(因此需要不同的视图),并且数据略有不同(限制查询而不是拉动所有项目)。

I am using Spring MVC to develop a Java webapp. I have a setup as follows: 2 pages of the site are responsible for rendering similar content. One of the pages simply renders the items slightly differently (and hence would need a different view), and pulls the data slightly differently (with a limit on the query instead of pulling all the items).

我的问题是:最好的方法是实现2个独立的控制器来处理这些页面,还是使用单个控制器会更好?如果答案是使用单个控制器,我怎样才能从控制器内部找出请求来自哪个页面(以便我可以调用相应的服务和视图)?谢谢

My question is: would the best approach for this be to implement 2 separate controllers to handle each of these pages, or would it be better to use a single controller? If the answer is to use a single controller, how can I find out from inside the controller from what page the request came from (so that I can call the appropriate service and view)? Thanks

推荐答案

使用 Spring 2.5+带注释的控制器,有一个控制器或两个控制器之间的区别并不是特别相关。

With Spring 2.5+ annotated controllers, the difference between having one controller or two controllers is not especially relevant.

例如,假设你有一个这样的控制器类:

For example, say you have a controller class like this:

@Controller
public class MyController {

   @RequestMapping ("/pageA");
   public String handlePageA() {
      .. do stuff
   }

   @RequestMapping ("/pageB");
   public String handlePageB() {
      .. do stuff
   }
}

这应该是显而易见的。您可以获得一个控制器的好处,使用两个处理程序方法,每个页面一个。

It should be obvious how this works. You get the benefit of one controller, with two handler methods, one for each "page".

确实没有理由再使用2.5之前的控制器。您可以将新样式与旧样式一起使用,因此即使是旧版2.5之前的应用程序也可以在升级后使用新样式。

There really is no reason to use pre-2.5 controllers any more. You can use the new style alongside the old one, so even legacy pre-2.5 apps can use the new style after an upgrade.

这篇关于Spring控制器设置问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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