【发布时间】:2018-08-12 16:26:47
【问题描述】:
我正在使用 Eclipse 构建一个 Web 应用程序,我想在其中使用框架 Struts 2 和 Hibernate。这次我没有使用 Maven,只是因为我想知道如果我不使用 Maven,如何使它工作。
版本:
- Struts 2.5
- 休眠 5.2.14
现在,工作集是:
META-INF 和 WEB-INF 是:
如图所示,persistence.xml 在 META-INF 中,web.xml 文件在 WEB-INF 中。
web.xml(全部在 web-app 标签中):
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>LearningStruts</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml(在 DOCTYPE 之后):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.locale" value="zh_CN"></constant>
<constant name="struts.custom.i18n.resources" value="mess" />
<constant name="struts.i18n.encoding" value="UTF-8" />
</struts>
因为我使用注解,所以所有的配置都很短。
persistence.xml(持久性的所有部分)是:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="CRM">
<description>
Persistence unit for Hibernate User Guide
</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/test_db" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
在一个类中,我使用注解注入EntityManagerFactory:
public class UserDao {
@PersistenceUnit(unitName = "CRM")
private EntityManagerFactory emf;
public void insert() {
System.out.println(emf);
}
}
但输出是null。
为什么?
【问题讨论】:
标签: eclipse hibernate jpa configuration struts2