Springs Element 'beans' cannot have character [children], because the type's content type is element-only
Asked Answered
D

16

25

I search some page in stackoverflow to solve this problem, did follow some true answer but not working. I am new in spring , sorry. here is my dispatcher-servlet

<?xml version='1.0' encoding='UTF-8' ?>
<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" 
       xmlns:aop="http://www.springframework.org/schema/aop"

       xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context    
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

 <context:component-scan base-package="controller" />
  <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>   < -- line 52 here...

Here is my web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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>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>

And my ApplicationContext

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!--bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.url}"
    p:username="${jdbc.username}"
    p:password="${jdbc.password}" 
</beans>

I have controller package in Source Package. The log say:

 04-Nov-2014 07:15:34.187 SEVERE [http-nio-8084-exec-375] org.apache.catalina.core.StandardWrapperValve.invoke Allocate exception for servlet dispatcher
 org.xml.sax.SAXParseException; lineNumber: 52; columnNumber: 9; cvc-complex-type.2.3: Element 'beans' cannot have character [children], because the type's content type is element-only.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
    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.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:458)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3237)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidComplexType(XMLSchemaValidator.java:3200)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidType(XMLSchemaValidator.java:3160)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processElementContent(XMLSchemaValidator.java:3062)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleEndElement(XMLSchemaValidator.java:2140)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:859)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2973)
    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)
    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:658)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1241)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1154)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

remove 2 lines:

 <context:component-scan base-package="controller" />
      <mvc:annotation-driven />

then the project run normally. I do not know why....

enter image description here

Dunnage answered 3/11, 2014 at 23:40 Comment(6)
unfortunately that image is too small to see detail, do you actually have a package called "controller" in which your spring mvc code is?Hen
so is this question still valid?Hen
sorry I changed the title,Take new post take fooling around. I tried 2 hours but can not solve it. Try to change, try to add new code, and get new problem...Dunnage
ok paste in your whole configuration xml files so that it can be reviewed. Also paste in your whole error stack.Hen
okey, done it after 1 minuteDunnage
do you find out something....Dunnage
H
4

Noticed in the your dispatcher-servlet.xml file you are missing "<?xml version="1.0" encoding="UTF-8"?>".

Please add it to top of file and re-run. Let me know if it doesn't work.

Hen answered 4/11, 2014 at 0:56 Comment(3)
just clear this code: <context:component-scan base-package="controller" /> <mvc:annotation-driven /> . Then the project run normally, i do not know why....Dunnage
can you confirm the location of your controller package, give the full address starting from src level of project.Hen
No worries. 99% of my code problems stem from silly trivial issues.Hen
S
44

I had the same problem. I looked at it for hours. I could not find any problem. Then I decided, sometimes you see something different if you use a different editor. I closed Netbeans and opened the file in emacs. I immediately saw, there were some kind of non-breaking space characters, or tabs, or some type of whitespace that wasn't whitespace. I don't know which character it was but it showed up in red in emacs, but was whitespace in Netban

Sway answered 6/9, 2015 at 0:14 Comment(0)
E
8

Unfortunately mine was a simple mistake I type a '.' character in the middle.

Solved by just removing the '.' - so will get this type of error if any unused and unknown character is present in the configuration file.

Earpiercing answered 17/6, 2015 at 9:23 Comment(2)
so do I, having extra > in configurationThanksgiving
I ran into this when I added a C-style // comment to the file stupidly.Echinate
Q
6

Follow the below steps outlined and you should be able to resolve it :

  1. Try to open the xml in Internet Explorer or any xml view tool and see whether the xml is rendered properly.
  2. Or you can do a ctrl + shift + F to format the xml file and see if xml is well formed or not.
  3. With the above step you should be able to find the exact location where you need to make changes to make the xml well formed.
  4. In my case I had to just remove the extra "<" closing tag to resolve the issue.
Quickie answered 22/4, 2016 at 14:19 Comment(1)
in my case i typed double --> -->Paphos
H
4

Noticed in the your dispatcher-servlet.xml file you are missing "<?xml version="1.0" encoding="UTF-8"?>".

Please add it to top of file and re-run. Let me know if it doesn't work.

Hen answered 4/11, 2014 at 0:56 Comment(3)
just clear this code: <context:component-scan base-package="controller" /> <mvc:annotation-driven /> . Then the project run normally, i do not know why....Dunnage
can you confirm the location of your controller package, give the full address starting from src level of project.Hen
No worries. 99% of my code problems stem from silly trivial issues.Hen
B
3

Yes, i experience thesame problem. I solved it by first: format the xml. Then i took a keen look through the lines, i found an extra closing tag (/>) which have been blocking the closing tag from seeing the open tag. So i removed the bug (/>) and the program deployed.

Brasier answered 12/5, 2016 at 12:26 Comment(0)
P
2

I got similar error. The way how I fixed it was replacing:

<property name="param">true</property>

on

<property name="param" value="true"/>
Picturize answered 21/10, 2016 at 8:6 Comment(0)
S
2

This is the problem with some special hidden characters. You can copy it to some text editor and remove any hidden characters and format it better and now see.. This issue must be resolved.

Sholem answered 8/5, 2017 at 7:56 Comment(0)
Q
1

The problem was an uncommented line in the springsecurity.xml which was missing when I commented the whole file and uncommented. Make sure you have the extra text commented or remove them.

Quasijudicial answered 3/2, 2016 at 10:48 Comment(0)
H
1

I had the same problem and I resolved it by validating my xml using the online xml validation tool and found out that there was an extra whitespace which was the cause for the error.

Hungerford answered 18/4, 2017 at 6:21 Comment(1)
Hi. You can add your validating xml tool link here to help somebody who meet this problem. :)Dunnage
E
1

Even I got the similar problem. When I copied it in the text editor, I observed there is " got appended at the end of the line. I deleted the " at the end of the line, it got Resolved..

Exotoxin answered 11/6, 2018 at 21:8 Comment(0)
E
1

for me I mistyped a character somewhere in the xml file . just remove it and the error gone.

Epistyle answered 15/8, 2018 at 19:37 Comment(1)
This is very trueErgo
A
0

I got the same problem. Realized it is due to some unknown spaces that my IDE couldn't recognize. Opened it in Sublime text and corrected the spaces. Works perfectly now.

Amorist answered 12/10, 2016 at 19:8 Comment(0)
C
0

I got the same problem, but after looking at the code closely, I found that my IDE was showing the error mark on the wrong line of code while the problem was arising because of an extra angular-bracket.

Coper answered 10/9, 2020 at 9:35 Comment(0)
B
0

I know this is an older question but we just had this happen the other day and it was very annoying not knowing why.

I emptied our applicationContext.xml of all bean entries and pasted them back in one by one. Eventually, I found the culprit was a simple ending comment after a been definition! It took a half hour of complaining about it giving the error and < 5 minutes to just paste the beans back in and find the errant comment.

Biggerstaff answered 6/10, 2022 at 13:25 Comment(0)
C
0

I was getting the same error and that was because I didn't use the right convention to add the comment in the xml file.

I used the JAVA way to add the comment. We have to use the XML way to add the comment to avoid this error.

This error could also be because of formatting , space issue or you have typed anything by mistake in between , etc.

Cabinetwork answered 24/12, 2022 at 21:19 Comment(0)
S
-2

manually check whether there are unnecessary spaces tabs before each nodes, you can do with eclipse

regards

Streaming answered 19/1, 2017 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.