gpt4 book ai didi

java - Spring数据库配置问题

转载 作者:行者123 更新时间:2023-12-02 03:49:18 25 4
gpt4 key购买 nike

我是 Spring-MVC 的新手,我正在尝试创建一个 Spring-MVC使用 annotation 的项目在controller并且还创建 databases .

当我写下所有 xml 时,应用程序工作正常。代码 spring-dispatcher-servlet.xml文件,但是当我分开 spring database connection xmlspring servlet xml文件,它停止工作。

以下代码成功在数据库中创建表,但无法加载 Controller ,它给了我 404 not found当我尝试点击任何 Controller 时的页面。如果我评论<listner>代码在我的web.xml文件成功加载所有 Controller ,但未执行任何数据库操作。

请指导我在这里做错了什么。

web.xml

<!--  JPA  -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</context-param>



<!-- Servlet Dispatcher -->
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>

Spring.xml

    <mvc:annotation-driven />

<context:annotation-config />
<context:component-scan base-package="com.evantage.models" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/springDB" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- This produces a container-managed EntityManagerFactory; rather than
application-managed EntityManagerFactory as in case of LocalEntityManagerFactoryBean -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/>
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="persistenceXmlLocation" value="/WEB-INF/persistence.xml" />
</bean>



<jpa:repositories base-package="com.evantagesoft.springmvctiles.repository"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager"/>

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

</beans>

Spring-dispatcher-servlet.xml

<context:component-scan base-package="com.evantage.controllers" />
<mvc:annotation-driven />
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
<property name="preparerFactoryClass"
value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory" />
</bean>

<!-- View Handler -->
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="favorPathExtension" value="true" />
<property name="mediaTypes">
<map>
<entry key="xml" value="text/xml" />
<entry key="json" value="application/json" />
<entry key="html" value="text/html" />
<entry key="less" value="text/html" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- <property name="prefix" value="/"/> -->
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</list>
</property>
</bean>

最佳答案

您需要在调度程序 servlet xml 中添加 context:component-scan base-package="controller package"才能使其正常工作。需要进行组件扫描来扫描包并注册您的 Controller 类

关于java - Spring数据库配置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36012951/

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