Struts2与Hibernate联合开发步骤
2017-05-05 15:48 fengna 阅读(365) 评论(0) 编辑 收藏 举报hibernate与struts2的联合开发步骤:
1. 建立web工程
2. 加入jar包,struts2、hibernate、数据库连接的包
3. 数据表和bean之间的映射,以及相应的映射文件*.hbm.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.amaker.bean.SystemMember" table="systemmembertab">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"/>
<property name="fullName"/>
<property name="password"/>
<property name="privilege"/>
</class>
</hibernate-mapping>
4. hibernate.cfg.xml文件配置
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate_pro_db</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--映射资源-->
<mapping resource="com/amaker/bean/SystemMember.hbm.xml"/>
<mapping resource="com/amaker/bean/Question.hbm.xml"/>
<mapping resource="com/amaker/bean/QuestionType.hbm.xml"/>
<mapping resource="com/amaker/bean/Answer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5. 配置web.xml文件 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HibernateProject</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
</web-app>
6. 建立form表单(jsp),并建立相应的Action(继承ActionSupport)
7. 配置struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="example" namespace="/" extends="struts-default">
<action name="login" class="com.amaker.action.LoginAction">
<result name="main_page">/main.jsp</result>
<result name="input">/Login.jsp</result>
</action>
</package>
</struts>