严重:上下文初始化失败,org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException

时间:2014-09-29 07:52:04

标签: java jsp spring-mvc jdo

我是春季mvc和jdo的新手,我试着学习我的错误

HTTP错误503

访问/添加问题。原因是:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML document from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 28; The prefix "mvc" for element "mvc:annotation-driven" is not bound.

我的spring mvc程序放在这里:

person.java:

package com.web.project;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;




@PersistenceCapable
public class Person {

        // ID or key unique variable 
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;

        // String or integer or other variable related to person
        @Persistent
        private String name ;

        @Persistent
        private String email;

        @Persistent
        private Date date;

        //getter and setter methods

        public void setKey(Key key) {
            this.key = key;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public void setDate(Date date) {
            this.date = date;
        }

        public Key getKey() {
            return key;
        }

        public String getName() {
            return name;
        }

        public String getEmail() {
            return email;
        }

        public Date getDate() {
            return date;
        }

        @Override
        public String toString() {
            return "Person [key=" + key + ", name=" + name + ", email=" + email
                    + ", date=" + date + "]";
        }    
}

personalcontroller.java:

package com.web.project;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServletRequest;

import com.web.project.Person;

import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class Personalcontroller {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    List<Person> results = null;

    @RequestMapping(value="/add")
    public ModelAndView add(HttpServletRequest request, ModelMap model)
    {
        System.out.println("inside the add service");
        String name = request.getParameter("name");
        String email = request.getParameter("email");


        try
        {
            Person p = new Person();
            p.setName(name);
            p.setEmail(email);
            p.setDate(new Date());

            pm.makePersistent(p);
        }

        finally
        {
            pm.close();
        }

        return new ModelAndView("redirect:link");
    }

    @RequestMapping(value="/update")
    public ModelAndView update(HttpServletRequest request, ModelMap model)
    {
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String key = request.getParameter("key");
        try 
        {
        Person p = pm.getObjectById(Person.class, key);
        p.setName(name);
        p.setEmail(email);
        p.setDate(new Date());          
        } 
        catch (Exception e)
        {
        pm.close();
        }
        return new ModelAndView("redirect:link");
    }

    @RequestMapping(value = "/delete/{key}")
    public ModelAndView delete(@PathVariable String key, HttpServletRequest request, ModelMap model) {
        try {

            Person p = pm.getObjectById(Person.class, key);
            pm.deletePersistent(p);

        } finally {

            pm.close();
        }
        return new ModelAndView("redirect:list");
    }
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String listPerson(ModelMap model) {

        Query q = pm.newQuery(Person.class);
        q.setOrdering("date desc");

        try {
            results = (List<Person>) q.execute();

            if (results.isEmpty()) {
                model.addAttribute("list", null);
            } else {
                model.addAttribute("list", results);
            }

        } finally {
            q.closeAll();
            pm.close();
        }

        return "list";

    }
}

PMF.java:

package com.web.project;

import javax.jdo.*;

public final class PMF {
    private static final PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional");
    private PMF(){

    }
    public static PersistenceManagerFactory get()
    {
        return pmfInstance;
    }

}

personal.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory, java.util.Date, java.util.List;" %>
<%@ page import="com.web.project.Person" %>
<!DOCTYPE html>
<html>
<body>
    <h1>GAE + Spring 3 MVC REST + CRUD Example with JDO</h1>

    Function : <a href="add.jsp">Add Person</a>
    <hr />

    <h2>All Person</h2>
    <table border="1">
        <thead>
            <tr>
                <td>Name</td>
                <td>Email</td>
                <td>Created Date</td>
                <td>Action</td>
            </tr>
        </thead>

        <%

        if(request.getAttribute("list")!=null){

            List<Person> person = (List<Person>) request.getAttribute("list");

            if(!person.isEmpty()){
                 for(Person p : person){

        %>
                <tr>
                  <td><%=p.getName() %></td>
                  <td><%=p.getEmail() %></td>
                  <td><%=p.getDate() %></td>
              <td><a href="update/<%=p.getName()%>">Update</a> | 
                                      <a href="delete/<%=KeyFactory.keyToString(p.getKey()) %>">
                                       Delete</a>
                                  </td>
                </tr>
        <%  

                }

            }

           }
        %> 
    </table>

</body>
</html>

add.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<body>
    <h1>Add Person</h1>

    <form method="GET" action="/add">
        <table>
            <tr>
                <td>UserName :</td>
                <td><input type="text" style="width: 185px;" maxlength="30"
                    name="name" id="name" /></td>
            </tr>
            <tr>
                <td>Email :</td>
                <td><input type="text" style="width: 185px;" maxlength="30"
                    name="email" id="email" /></td>
            </tr>
        </table>
        <input type="submit" class="save" title="Save" value="Save" />
    </form>

</body>
</html>

update.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<%@ page import="com.web.project.Person" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<html>
<body>
    <h1>Update Customer</h1>

    <%
        Person person = new Person();

        if(request.getAttribute("person")!=null){

            person = (Person)request.getAttribute("person");

        }

    %>

    <form method="post" action="../update" >
        <input type="hidden" name="key" id="key" 
            value="<%=KeyFactory.keyToString(person.getKey()) %>" /> 

        <table>
            <tr>
                <td>
                    UserName :
                </td>
                <td>
                    <input type="text" style="width: 185px;" 
                                             maxlength="30" name="name" id="name" 
                        value="<%=person.getName() %>" />
                </td>
            </tr>
            <tr>
                <td>
                    Email :
                </td>
                <td>
                    <input type="text" style="width: 185px;" 
                                             maxlength="30" name="email" id="email" 
                        value="<%=person.getEmail() %>" />
                </td>
            </tr>
        </table>
        <input type="submit" class="update" title="Update" value="Update" />
    </form>

</body>
</html>

在执行期间 错误:

SEVERE: Context initialization failed

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML document from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 29; The prefix "mvc" for element "mvc:annotation-driven" is not bound.
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:398)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:335)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:643)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:606)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:657)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:525)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:466)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:440)
    at org.mortbay.jetty.servlet.ServletHolder.doStart(ServletHolder.java:263)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:685)
    at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
    at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
    at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.Server.doStart(Server.java:224)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at com.google.appengine.tools.development.JettyContainerService.startContainer(JettyContainerService.java:255)
    at com.google.appengine.tools.development.AbstractContainerService.startup(AbstractContainerService.java:288)
    at com.google.appengine.tools.development.AutomaticInstanceHolder.startUp(AutomaticInstanceHolder.java:26)
    at com.google.appengine.tools.development.AbstractModule.startup(AbstractModule.java:87)
    at com.google.appengine.tools.development.Modules.startup(Modules.java:105)
    at com.google.appengine.tools.development.DevAppServerImpl.doStart(DevAppServerImpl.java:258)
    at com.google.appengine.tools.development.DevAppServerImpl.access$000(DevAppServerImpl.java:47)
    at com.google.appengine.tools.development.DevAppServerImpl$1.run(DevAppServerImpl.java:213)
    at com.google.appengine.tools.development.DevAppServerImpl$1.run(DevAppServerImpl.java:211)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.google.appengine.tools.development.DevAppServerImpl.start(DevAppServerImpl.java:211)
    at com.google.appengine.tools.development.DevAppServerMain$StartAction.apply(DevAppServerMain.java:277)
    at com.google.appengine.tools.util.Parser$ParseResult.applyArgs(Parser.java:48)
    at com.google.appengine.tools.development.DevAppServerMain.run(DevAppServerMain.java:219)
    at com.google.appengine.tools.development.DevAppServerMain.main(DevAppServerMain.java:210)
Caused by: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 29; The prefix "mvc" for element "mvc:annotation-driven" is not bound.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:289)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2786)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:76)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadDocument(XmlBeanDefinitionReader.java:428)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
    ... 46 more

4 个答案:

答案 0 :(得分:2)

请检查你的上下文xml。我认为你没有为“mvc”定义模式。

例如:

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

答案 1 :(得分:1)

检查您的项目是否正在构建...还要检查是否在项目菜单下选中/选择了自动构建。

答案 2 :(得分:0)

问题在于名为 /WEB-INF/mvc-dispatcher-servlet.xml 的文件

您似乎尝试使用xml命名空间,但未正确定义它们。

如果您使用带有 mvc 前缀的元素,请确保您拥有 xmlns:mvc =&#34; http://www.springframework.org/schema/ mvc&#34; 在您的命名空间声明中,如上例所示。您还需要schemaLocation,如下例所示。

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

    <mvc:interceptors>
    </mvc:interceptors>

</beans>

答案 3 :(得分:0)

以下是我用于spring-mvc应用程序的架构。我遇到了同样的问题,因为我错过了添加spring-mvc模式的机会。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

有关要使用的架构的更多信息,请参见以下链接:

https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/xsd-configuration.html