java 在 Spring MVC 中自定义 DispatcherServlet 是一种常见的做法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1304719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:02:50  来源:igfitidea点击:

Is customizing DispatcherServlet in Spring MVC a common practice?

javaspringspring-mvc

提问by Winston Chen

I am new to Spring MVC. But I had certain experience in working with Struts 1.x. I would like to know if it is a common practice to customize DispatcherServletwhile working with Spring MVC, like people sometimes customize ActionServletor RequestProcessorin Struts 1.x?

我是 Spring MVC 的新手。但是我在使用 Struts 1.x 方面有一定的经验。我想知道在使用 Spring MVC 时自定义DispatcherServlet是否是一种常见的做法,就像人们有时在 Struts 1.x 中自定义ActionServletRequestProcessor 一样

Or let's extend the question a little bit. Where are the entry points for a veteran Spring MVC programmer to customize the MVC framework?

或者让我们稍微扩展一下问题。资深Spring MVC程序员定制MVC框架的切入点在哪里?

Thank you.

谢谢你。

采纳答案by adatapost

Definitely it is a good practice. Spring Framework - Web MVC framework

这绝对是一个很好的做法。Spring Framework - Web MVC 框架

SUMMARY: You can customize Spring's DispatcherServlet by adding context parameters .... especially handy when you have a lot of common functionality in one controller.

总结:您可以通过添加上下文参数来自定义 Spring 的 DispatcherServlet .... 当您在一个控制器中有很多常用功能时尤其方便。

回答by skaffman

DispatcherServlet, like most of the Spring API, is very much designed for extension. Indeed, if you find a part of it that's not ammenable to subclassing, file an issue on their JIRA and they'll likely fix it for you (I've done that on a number of occasions).

DispatcherServlet,像大多数 Spring API 一样,是专为扩展而设计的。事实上,如果您发现其中的一部分不适合子类化,请在他们的 JIRA 上提交问题,他们可能会为您修复它(我已经多次这样做了)。

Having said that, the vast majority of functionality can be achieved via configuration of DispatcherServletrather than extension of it.

话虽如此,绝大多数功能都可以通过配置DispatcherServlet而不是扩展来实现。

回答by Java Maverick

Here's my suggestion where I am thinking that extending the dispatcher servlet would be 'a' solution.

这是我的建议,我认为扩展调度程序 servlet 将是“一个”解决方案。

  1. separate validation, conversion from controller (for several good reasons)
  2. make the controller just a delegator to service layer.
  3. build/populate the domain/request object fully 'before' it reaches the controller.
  1. 单独的验证,从控制器转换(有几个很好的原因)
  2. 使控制器只是服务层的委托人。
  3. 在它到达控制器之前完全构建/填充域/请求对象。

回答by duffymo

I, for one, have never found a reason to extend DispatcherServlet.

一方面,我从来没有找到扩展 DispatcherServlet 的理由。

回答by Nick Holt

Why would you want specialize org.springframework.web.servlet.DispatcherServlet?

为什么要专门化org.springframework.web.servlet.DispatcherServlet

Any request pre-processing can/should be achieved with a javax.servlet.Filterand the rest is down to the implementation of org.springframework.web.servlet.mvc.Controller.

任何请求预处理都可以/应该使用javax.servlet.Filter来实现,其余的取决于 org.springframework.web.servlet.mvc.Controller 的实现

Alternatively see adatapost's answer, which refers to configuration of the org.springframework.web.servlet.DispatcherServletvia the web.xml file.

或者,请参阅adatapost的答案,它指的是通过 web.xml 文件配置org.springframework.web.servlet.DispatcherServlet