gpt4 book ai didi

java - Spring MVC + MongoDB

转载 作者:行者123 更新时间:2023-12-02 03:54:09 24 4
gpt4 key购买 nike

我在使用 Spring Tool Suite 的 servlet-context.xml 中遇到以下错误:

the prefix beans for element beans bean is not bound The error is at the line : http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

顺便说一句,我正在尝试使用以下配置将 Spring 连接到 MongoDB:

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

<!-- 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>

<context:component-scan base-package="com.etvld.mvc" />

<mongo:mongo host="192.168.1.19" port="27017"/>

</beans:beans>

最佳答案

您的开始和结束标记不匹配,并且您忘记定义 beans 命名空间:

<?xml version="1.0" encoding="UTF-8"?>
<beans:bean ...>
^^^^ opening tag => bean
...
</beans:beans>
| ^^^^^ ending tag => beans
|
+ =====> Where did you define beans namespace?

因此,将第一行替换为以下内容:

<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"

此外,添加 mvc 命名空间作为默认命名空间:

xmlns="http://www.springframework.org/schema/mvc"

您的servlet-context.xml最终看起来像这样:

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

<!-- 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>

<context:component-scan base-package="com.etvld.mvc" />

<mongo:mongo host="192.168.1.19" port="27017"/>

</beans:beans>

如果您发现自己对使用 Spring 配置感到不舒服(我想您确实如此),最好切换到 Spring Boot,它会自动执行大多数此类配置。

关于java - Spring MVC + MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35637767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com