java - Spring MVC : how to launch default servlet

标签 java spring-mvc servlets dispatcher

我遇到了一个问题。我无法在 Spring MVC 应用程序中启动默认 servlet。虽然通过输入 url 显式启动它是有效的。

这是我的配置: servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />


<mvc:default-servlet-handler default-servlet-name="persons"/>



<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory -->
<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <!-- <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" /> -->
    <beans:property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />

    <beans:property name="url"
        value="jdbc:jtds:sqlserver://localhost:1433/Practice" />
    <beans:property name="username" value="abc" />
    <beans:property name="password" value="def" />
</beans:bean>

<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean ">
     <!-- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> -->
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>com.journaldev.spring.model.Person</beans:value>
        </beans:list>
    </beans:property>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
            </beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>

<beans:bean id="personDAO" class="com.journaldev.spring.dao.PersonDAOImpl">
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="personService" class="com.journaldev.spring.service.PersonServiceImpl">
    <beans:property name="personDAO" ref="personDAO"></beans:property>
</beans:bean>
<context:component-scan base-package="com.journaldev.spring" />

<tx:annotation-driven transaction-manager="transactionManager"/>

<beans:bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>

网络.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->

<!-- this context-param is needed otherwise app will look for /WEB-INF/applicationContext.xml -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-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>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 


    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

我已经尝试使用 url-pattern 作为/* 但它仍然无法正常工作。我在这里做错了什么?

我得到异常:

java.lang.IllegalStateException: A RequestDispatcher could not be located for the default servlet 'persons'
org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler.handleRequest(DefaultServletHttpRequestHandler

最佳答案

<mvc:default-servlet-handler default-servlet-name="persons"/>问题就在这里。您应该定义 default-servlet-name仅当无法自动检测到默认 servlet 时,我认为这不是您的情况。其次,您传递了错误的名称,因为您在 web.xml 中设置了默认的 servlet。并将其命名为appServlet .

因此,像这样重写该行:<mvc:default-servlet-handler />或者这个<mvc:default-servlet-handler default-servlet-name="appServlet"/> .应该可以。

关于java - Spring MVC : how to launch default servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32669022/

相关文章:

java - 无法触发构造函数级别验证

java - Maven 提示对 WAR 文件的依赖

java - 如何动态设置JSP隐藏值

multithreading - SpringFrameWork 注入(inject)的 bean 为空?

java - 如何将异常发送到 exceptionController?

tomcat - 处理应用程序 web.xml 中的错误 503?

java - 使用包含反斜杠的 URL 中的参数处理 POST 请求

java - 如何直接映射/扩展对象到流?

java - 非法状态异常 : migration from jboss 4 to jboss 5

jsp - 我可以在没有 JSP 的情况下部署 Java Spring MVC Web 应用程序吗?