Spring + Web MVC:dispatcher-servlet.xml 与 applicationContext.xml(加上共享安全) [英] Spring + Web MVC: dispatcher-servlet.xml vs. applicationContext.xml (plus shared security)

查看:21
本文介绍了Spring + Web MVC:dispatcher-servlet.xml 与 applicationContext.xml(加上共享安全)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用两个上下文的正确方法是什么:dispatcher-servlet.xmlapplicationContext.xml?什么去哪里?

What is the correct way to use the two contexts: dispatcher-servlet.xml and applicationContext.xml? What goes where?

我想编写一个部署在 servlet 容器中的相当典型的应用程序.它有一些带有 JSP 视图的控制器.它在后端也有一些重要的逻辑.我真的需要这两种情况吗?它们之间的关系如何?我如何决定将什么放入哪个?

I want to write a fairly typical app deployed in a servlet container. It has some controllers with JSP views. It also has some nontrivial logic on the back-end. Do I really need both contexts? How are they related to each other? How can I decide what to put in which?

另外,我想为我的应用程序使用 Spring-security.我可能想在 web 控制器以及更深层中使用它的功能(如带有注释的声明性安全性).在这种情况下,我应该如何配置安全性?它应该在这些文件之一(哪个?)中,还是两者中?

Also, I want to use Spring-security for my application. I may want to use its features (like declarative security with annotations) in web controllers as well as in deeper layers. How should I configure security to work in this case? Should it be in one of those files (which?), or both?

推荐答案

dispatcher-servlet.xml 文件包含您对 Spring MVC 的所有配置.因此,您会在其中找到诸如 ViewHandlerResolversConverterFactoriesInterceptors 等 bean.所有这些 bean 都是 Spring MVC 的一部分,它是一个框架,用于构建您处理 Web 请求的方式,提供有用的功能,例如数据绑定、视图解析和请求映射.

The dispatcher-servlet.xml file contains all of your configuration for Spring MVC. So in it you will find beans such as ViewHandlerResolvers, ConverterFactories, Interceptors and so forth. All of these beans are part of Spring MVC which is a framework that structures how you handle web requests, providing useful features such as databinding, view resolution and request mapping.

application-context.xml 可以在使用 Spring MVC 或任何其他与此相关的框架时有选择地包含在内.这为您提供了一个容器,可用于配置其他类型的 spring bean,为数据持久性等提供支持.基本上,您可以在此配置文件中提取 Spring 提供的所有其他优点.

The application-context.xml can optionally be included when using Spring MVC or any other framework for that matter. This gives you a container that may be used to configure other types of spring beans that provide support for things like data persistence. Basically, in this configuration file is where you pull in all of the other goodies Spring offers.

这些配置文件在web.xml文件中配置如下:

These configuration files are configured in the web.xml file as shown:

调度程序配置

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

应用配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/application-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

要配置控制器,用 @Controller 注释它们,然后在 dispatcher-context.xml 文件中包含以下内容:

To configure controllers, annotate them with @Controller then include the following in the dispatcher-context.xml file:

<mvc:annotation-driven/>
<context:component-scan base-package="package.with.controllers.**" />

这篇关于Spring + Web MVC:dispatcher-servlet.xml 与 applicationContext.xml(加上共享安全)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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