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

查看:104
本文介绍了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.

在使用Spring MVC或其他任何框架时,可以选择包含application-context.xml.这为您提供了一个可用于配置其他类型的Spring Bean的容器,这些类型的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天全站免登陆