简体   繁体   中英

HTTP 500 error: How do I fix my `dispatcher-servlet.xml` file?

As I am new to Spring MVC framework and trying to learn, but an exception is thrown. What could be the cause?

It seems like I have an error in my dispatcher-servlet.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="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"
xsi:schemaLocation="http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  <annotation-driven />
  <context:component-scan base-package="kz.controller" />
  <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
  </bean>
</beans:beans>

My controller classes are annotation driven, but it is throwing this error:

HTTP Status 500 - Servlet.init() for servlet dispatcher threw exception

type Exception report

message Servlet.init() for servlet dispatcher threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException : Servlet.init() for servlet dispatcher threw exception org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException : Line 23 in XML document from ServletContext resource [ /WEB-INF/dispatcher-servlet.xml ] is invalid;

Your XML is incorrect. You defined the bean namespace under the beans namespace. The correct XML is like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="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"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">



    <annotation-driven />
    <context:component-scan base-package="kz.controller" />
    <beans:bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <beans:property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <beans:property name="prefix" value="/WEB-INF/jsp/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
</beans:beans>

Usually in spring applications the beans schema is the default so it would be even better if you would change the beans schema to be the default then you can delete all the "beans:" and add "mvc:" to the other tags.

Check the error message. It says that the file /WEB-INF/dispatcher-servlet.xml is not a valid xml file. Use an IDE (eclipse, intellij, netbeans, etc) to open the file and it should say why the file is invalid.

In particular the error is in

</bean>
</beans:beans>

you don't need </bean> .

bottom line, use a good IDE! we're not any more in the '60 where we need to code with punch cards & hope our code is correct :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM