Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是在spring中为每个包创建的dispatcher xml吗_Java_Spring_Spring Mvc - Fatal编程技术网

Java 是在spring中为每个包创建的dispatcher xml吗

Java 是在spring中为每个包创建的dispatcher xml吗,java,spring,spring-mvc,Java,Spring,Spring Mvc,我在阅读spring教程时,对spring的dispatcher xml特性产生了疑问,我不确定这是否是一个有效的疑问,我找不到任何与此相关的信息。我根据教程中的教程编写了一个DispatcherXML,下面给出了DispatcherXML <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:con

我在阅读spring教程时,对spring的dispatcher xml特性产生了疑问,我不确定这是否是一个有效的疑问,我找不到任何与此相关的信息。我根据教程中的教程编写了一个DispatcherXML,下面给出了DispatcherXML

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.o rg/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="Student" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/jsp/" /> 
        <property name="suffix" value=".jsp" /> 
    </bean> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 
    <mvc:annotation-driven />
</beans>
<context:component-scan base-package="Student" />  
我的疑问在于DispatcherXML中的一行,该行如下所示

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.o rg/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="Student" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/jsp/" /> 
        <property name="suffix" value=".jsp" /> 
    </bean> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 
    <mvc:annotation-driven />
</beans>
<context:component-scan base-package="Student" />  
如果必须在每个dispatcher xml中指定基本包,我应该为我创建的每个包创建一个新的dispatcher xml,还是将所有包作为子包放到主包中,这里的开发标准是什么,通常做什么

下面给出了我的web.xml

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet> 
    <servlet-name>SpringStud</servlet-name> 
    <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>SpringStud</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping>
<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> 
    <load-on-startup>2</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>*.htm</url-pattern> 
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

似乎您并不真正理解servlet是什么。您发布的是SpringMVC的XML配置。它不是一个servlet。servlet是一个Java类,与Spring完全无关,Spring扩展了HttpServlet。由于您的术语完全错误,我无法理解您的问题。很抱歉,我使用了servlet这个术语,因为这些xml文件的名称后缀为-servlet,并且这些文件在web.xml中的调用方式与j2ee中的servlet相同。我已将术语更改为DispatcherXML。如果我错了,请纠正我学生真的是包裹的名字吗?无论哪种方式,组件扫描都将递归地检查从Student开始的所有较低级别的包——如果这是一个包名的话。您还可以使用逗号分隔的包作为值,即base package=com.mypackage,com.myotherpackage xml文件是web.xml中声明的一个DispatcherServlet实例的配置文件。一个典型的webapp只使用一个这样的实例,那么为什么需要多个xml文件来配置它呢?@JB Nizet我可以使用两个这样的xml文件,默认的一个是dispatcher-servlet.xml,它具有所有的初始配置,我创建了一个类似SpringStud-servlet.xml的文件。我在我的web.xml文件中使用了这两个包,它们都工作得很好,我不知道您可以像peeskillet在评论中提到的那样使用不同的基本包。我还在问题中添加了我的web.xml