有人可以解释Spring web.xml文件吗? [英] Can someone explain the Spring web.xml file?

查看:90
本文介绍了有人可以解释Spring web.xml文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java Enterprise和Spring的新手,但是我对标准Java有很深的了解.我正在浏览一个现有的Web应用程序项目.该项目使用Tomcat/Spring/Hibernate,据我所知是相当普遍的.它还将DWR用于远程方法调用.我发现很难区分职责:Tomcat负责什么,Spring负责什么,请求如何从一个请求到另一个请求以及Spring的主要部分如何组合在一起.我已经阅读了很多有关Spring的文档,尤其是关于bean和bean factory的文档,并且仍在阅读中.大家的建议都将受到欢迎,但我会提供一些具体问题.

问题1:web.xml在哪里适合使用(何时使用/调用它,从哪里调用)?

代码示例1:

    <servlet>
    <servlet-name>qrst</servlet-name>
        <display-name>qrst Servlet</display-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

以上代码段是做什么的(或者,它导致什么发生)?在我的Web应用中的某个时刻,qrst.jsp被使用了.是使用Servlet名称调用qrst.jsp的DispatcherServlet吗?否则,servlet名称的含义是什么?什么是启动时的负载?

代码示例2:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /someLocation/some-servlet.xml
    </param-value>
</context-param>

以上内容的链接或解释?从查看XML文件中可以看到它包含bean定义,并且我确实了解bean是什么以及如何使用bean,但是我不知道任何其他细节,并且希望这么做.

代码示例3:

<servlet>
  <servlet-name>dwr-invoker</servlet-name>
  <display-name>DWR</display-name>
  <servlet-class>
        org.directwebremoting.servlet.DwrServlet
</servlet-class>
    <init-param>
        <param-name>classes</param-name>
        <param-value>
            somepackage.someclass
        </param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

根据我对bean的了解,我相信那些init-param元素只是在servlet的java类中设置的参数. servlet名称的含义是什么,启动时的负载又如何?该Web应用程序以某种方式知道"何时进行AJAX(dwr)调用,而不是首次加载该Web应用程序时(首次加载时应使用qrst.jsp).它怎么知道的?它如何决定将请求路由到DWR而不是qrst.jsp?它在哪里做?

谢谢.

解决方案

Servlet是JavaEE的惯用法,用于回答 HTTP请求.您可以在Servlet中对应用程序的行为进行编程,该Servlet将响应请求.

Tomcat是一个Servlet容器,这意味着您将应用程序部署在Tomcat中,它将为您管理所有通信基础结构:它接受连接,管理数据库连接(*),并调用Servlet来处理传入的请求./p>

web.xml是任何JavaEE应用程序的一部分,而不是Spring的一部分.您的代码示例1声明您的应用程序将使用类org.springframework.web.servlet.DispatcherServlet的实例来处理传入的请求.

尽管servlet是JavaEE开发的基本基础,但不建议您创建自己的servlet.相反,使用Spring,您可以创建 MVC控制器.然后DispatcherServlet将调用这些控制器来处理请求.这只是另一种间接方式(但功能非常强大!)

使用Servlet名称调用qrst.jsp的是DispatcherServlet吗?

不直接.您的servlet和JSP文件具有相同的名称只是一个巧合.

启动时加载了什么?

您的代码样本2指示DispatcherServlet从文件/someLocation/some-servlet.xml加载Bean.如果此文件中有 controller bean ,并且根据您配置 url映射的方式,该文件中的bean将回答传入的请求.参见参考.. >

我相信这些init-param元素只是在servlet的java类中设置的参数

web.xml中的init-param元素用于servlet类.

Web应用程序会以某种方式知道"何时发生AJAX(dwr)调用,以及首次加载Web应用程序时(首次加载时应使用qrst.jsp).它怎么知道的?

该问题遗漏的是<servlet-mapping>元素(在web.xml中找到)或url映射(在spring文件中找到).这些负责确定URL是由调度程序servlet还是dwr servlet处理.

例如,具有如下所示的servlet映射:

<servlet-mapping>
    <servlet-name>qsrt</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>dwr</servlet-name>
    <url-pattern>*.dwr</url-pattern>
</servlet-mapping>

然后,所有以.do结尾的URL将由分派器servlet回答,而那些以.dwr结尾的URL将由dwr servlet处理. servlet的名称在这里很重要.

JSP文件是另一回事.容器将仅使用它们来处理以* .jsp结尾的URL.不要为以*.jsp结尾的URL创建onw servlet映射.这只会引起头痛.这可能是未指定的行为.

但是,浏览器地址栏中的URL始终看起来相同:它将始终调用qrst servlet

然后,您的servlet映射可能太宽泛(例如:<url-pattern>/*</url-pattern>),它将处理您在服务器上抛出的所有内容,而永远不会给其他servlet提供处理它的机会.

最后但并非最不重要的一点是,当使用DWR或任何Ajax技术时,为Firefox安装 HttpFox扩展,以便您可以监视应用程序的Ajax调用.

I'm new to Java Enterprise and to Spring but I have a strong grasp of standard Java. I am looking through an existing web application project. The project uses Tomcat/Spring/Hibernate which I understand is fairly common. It also uses DWR for remote method invocations. I'm finding it somewhat difficult to separate responsibilities: what Tomcat is responsible for, what Spring is responsible for, how a request gets from one to the other, and how the major pieces of Spring fit together. I've read a great deal of documentation on Spring, particularly about beans and bean factory and am still in process of reading more. Any advice you guys have would be welcome, but I'll provide some specific questions.

Question 1: Where does the web.xml fit into things (when is it used/called, and where is it called from)?

Code sample 1:

    <servlet>
    <servlet-name>qrst</servlet-name>
        <display-name>qrst Servlet</display-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

What does the above snippet do (or, what does it cause to happen)? At some point in my web app qrst.jsp gets used; is it the DispatcherServlet that calls qrst.jsp using the servlet name? Else what is the significance of the servlet name? What is load on startup?

Code sample 2:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /someLocation/some-servlet.xml
    </param-value>
</context-param>

Links or explanation of what the above does? I can see from looking at the XML file that it contains bean definitions and I do understand what beans are and how they are used, but I don't know any other details about this and would like to.

Code sample 3:

<servlet>
  <servlet-name>dwr-invoker</servlet-name>
  <display-name>DWR</display-name>
  <servlet-class>
        org.directwebremoting.servlet.DwrServlet
</servlet-class>
    <init-param>
        <param-name>classes</param-name>
        <param-value>
            somepackage.someclass
        </param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

From what I read about beans, I believe those init-param elements are just parameters that get set in the servlet's java class. What's the significance of the servlet name, and what about the load on startup? The web app somehow "knows" when an AJAX (dwr) call is happening versus when the web app is being loaded for the first time (when its loading for the first time it should use qrst.jsp). How does it know this? How does it decide to route the request to DWR instead of to qrst.jsp? Where does it do this?

Thanks.

解决方案

Servlets are JavaEE's idiom for answering HTTP requests. You program the behavior of your application in a Servlet which will respond to a request.

Tomcat is a Servlet container, which means you deploy your application in Tomcat and it will manage all the communication infrastructure for you: it accepts connections, manages database connections(*) and will call upon your servlets to handle incoming requests.

web.xml is part of any JavaEE application, not Spring. Your code sample 1 declares that your app will use an instance of class org.springframework.web.servlet.DispatcherServlet to handle incoming requests.

Although servlets are the basic foundations for JavaEE development, it is not advised to create your own; instead, with Spring, you create MVC controllers. Then the DispatcherServlet will call upon these controllers to handle the requests. It's just another indirection (but a very powerful one!)

is it the DispatcherServlet that calls qrst.jsp using the servlet name?

Not directly. It's just a coincidence that your servlet and the JSP file have the same name.

What is loaded on startup?

Your code sample 2 instructs the DispatcherServlet to load the beans from file /someLocation/some-servlet.xml. If there are controller beans in this file and according to how you configured the url mapping, beans from this file will answer the incoming requests. See the reference.

I believe those init-param elements are just parameters that get set in the servlet's java class

The init-param elements in web.xml are for the servlet class.

The web app somehow "knows" when an AJAX (dwr) call is happening versus when the web app is being loaded for the first time (when its loading for the first time it should use qrst.jsp). How does it know this?

Missing from the question are either the <servlet-mapping> element (found in web.xml), or the url mappings (found in the spring files). These are responsible for deciding whether an URL should be handled by the dispatcher servlet or the dwr servlet.

For instance, with an servlet mapping like below:

<servlet-mapping>
    <servlet-name>qsrt</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>dwr</servlet-name>
    <url-pattern>*.dwr</url-pattern>
</servlet-mapping>

Then all URLs ending in .do will be answered by the dispatcher servlet, and those ending with .dwr will be handled by the dwr servlet. Here's where the names of the servlets are important.

JSP files are a different story. The container will simply use them to handle a URL ending in *.jsp. Do not create your onw servlet mapping for URLs ending in *.jsp. This will only cause headaches. This is probably unspecified behavior.

Edit:

However, the URL in the browser's address bar always looks the same: it would always invoke the qrst servlet

Then it is possible that your servlet-mapping is so broad (something like: <url-pattern>/*</url-pattern>) that it will handle anything you throw at the server and never give a chance for the other servlets to handle it.

Last but not least, when working with DWR or any Ajax technology, install the HttpFox extension for Firefox so you can monitor the Ajax calls of your application.

这篇关于有人可以解释Spring web.xml文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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