Struts+Hibernate的配置问题
首先还是配置问题,记住引入Struts和Hibernate要用到的包(不清楚地话就全部都引进去)。配置文件方面,struts和Hibernate都可以单独配置.双方互不干扰,只是要记住Hibernate的配置文件要放在src的根目录下(不要放到包里,这样是为了它能生成到classes的根目录下)。
示例如下:
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>
http://jakarta.apache.org/struts/tags-logic
</taglib-uri>
<taglib-location>
/WEB-INF/tlds/struts-logic.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>
http://jakarta.apache.org/struts/tags-html
</taglib-uri>
<taglib-location>/WEB-INF/tlds/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>
http://jakarta.apache.org/struts/tags-bean
</taglib-uri>
<taglib-location>/WEB-INF/tlds/struts-bean.tld</taglib-location>
</taglib>
</web-app>
Struts-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="form" type="com.ln.hb.Customer"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="form"
name="form"
path="/test"
scope="request"
type="com.ln.struts.action.TestAction" >
<forward name="main" path="/main.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.ln.struts.ApplicationResources" />
</struts-config>
Hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<hibernate-configuration>
<session-factory>
<!-- properties -->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.url">
jdbc:mysql://192.168.151.72:3306/quickstart
</property>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- mapping files -->
<!-- The following mapping element was auto-generated in -->
<!-- order for this file to conform to the Hibernate DTD -->
<mapping resource="com/ln/hb/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
Tablename.hmb.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.ln.hb.Customer" table="CUSTOMER">
<id name="id" column="CID">
<generator class="identity" />
</id>
<property name="username" column="USERNAME" />
<property name="password" column="PASSWORD" />
</class>
</hibernate-mapping>