ActionContext ctx = ActionContext.getContext(); Map session = ctx.getSession(); 細心的朋友可以發現這里的session是個map對象在Struts2中底層的session都被封裝成了Map類型我們可以直接操作這個map 進行對session的寫入和讀取操作而不用去直接操作HttpSession對象
另外,org.apache.struts2.ServletActionContext作為輔助類(Helper Class),可以幫助您快捷地獲得這幾個對象。
HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); HttpSession session = request.getSession(); 如果你只是想訪問session的屬性(Attribute),你也可以通過ActionContext.getContext().getSession()獲取或添加session范圍(Scoped)的對象。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns=http://java.sun.com/xml/ns/j2ee
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- struts2 spring 整合 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext-*.xml,classpath*:applicationContext-*.xml
</param-value>
</context-param>
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>-->
<!-- 自動關閉Hibernatesession -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 亂碼 -->
<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2.0配置 -->
<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>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2. struts2-spring-plugin-2.0.14.jar
3.applicationContext-actions.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="loginManger" class="com.xushi.Manager.Impl.LoginSericeImpl"/>
<bean id="login" class="com.xushi.action.LoginAction">
<property name="loginManger" ref="loginManger"></property>
</bean>
</beans>
4.applicationContext-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>
5.applicationContext-common.xml<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 配置事務特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置那些類的方法進行事務管理 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution (* com.xushi.manager.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
</beans>
6記住配置ant環境變量
build.xml
<?xml version="1.0" encoding="GBK"?>
<project name="OA【01】系統構建腳本" default="生成Hibernate配置文件" basedir=".">
<property name="src.dir" value="${basedir}/src"/>
<property name="xdoclet.home" value="D:/xdoclet-plugins-dist-1.0.4"/>
<!-- Build classpath -->
<path id="xdoclet.task.classpath">
<fileset dir="${xdoclet.home}/lib">
<include name="**/*.jar"/>
</fileset>
<!-- <fileset dir="${xdoclet.home}/plugins">
<include name="**/*.jar"/>
</fileset>-->
</path>
<taskdef
name="xdoclet"
classname="org.xdoclet.ant.XDocletTask"
classpathref="xdoclet.task.classpath"
/>
<target name="生成Hibernate配置文件">
<xdoclet>
<fileset dir="${src.dir}/com/xushi/model">
<include name="**/*.java"/>
</fileset>
<component
classname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin"
destdir="${src.dir}"
version="3.0"
hbm2ddlauto="update"
jdbcurl="jdbc:mysql://localhost/oa"
jdbcdriver="com.mysql.jdbc.Driver"
jdbcusername="root"
jdbcpassword="wzy"
dialect="org.hibernate.dialect.MySQLDialect"
showsql="true"
/>
</xdoclet>
</target>
<target name="生成hibernate映射文件">
<xdoclet>
<fileset dir="${src.dir}/com/xushi/model">
<include name="**/*.java"/>
</fileset>
<component
classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
version="3.0"
destdir="${src.dir}"
/>
</xdoclet>
</target>
</project>
7.Orgnization.java
package com.xushi.model;
import java.util.Set;
/**
*
* @author Administrator
* @hibernate.class table="T_Orgnization"
*
*/
public class Orgnization {
/**
* @hibernate.id
* generator-class="native"
*/
private int id;
/**
* @hibernate.property
*/
private String name;
/**
* @hibernate.property
*/
private String sn;
/**
* @hibernate.property
*/
private String description;
/**
* @hibernate.many-to-one
* column="pid"
*/
private Orgnization parent;
/**
* @hibernate.set inverse="true" lazy="extra"
* @hibernate.key column="pid"
* @hibernate.one-to-many class="com.xushi.model.Orgnization"
*/
private Set children;
public Set getChildren() {
return children;
}
public void setChildren(Set children) {
this.children = children;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Orgnization getParent() {
return parent;
}
public void setParent(Orgnization parent) {
this.parent = parent;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
}
8.Person.java
package com.xushi.model;
/**
*
* @author Administrator
* @hibernate.class table="T_Person"
*/
public class Person {
/**
* @hibernate.id
* generator-class="native"
*/
private int id;
/**
* @hibernate.property
*/
private String name;
/**
* @hibernate.property
*/
private String sex;
/**
* @hibernate.property
*/
private String address;
/**
* @hibernate.property
*/
private String duty;
/**
* @hibernate.property
*/
private String phone;
/**
* @hibernate.property
*/
private String description;
/**
* @hibernate.many-to-one
*/
private Orgnization org;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDuty() {
return duty;
}
public void setDuty(String duty) {
this.duty = duty;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Orgnization getOrg() {
return org;
}
public void setOrg(Orgnization org) {
this.org = org;
}
}
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
package com.xushi.bll;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class HelloChina extends ActionSupport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
HttpServletRequest request = ServletActionContext.getRequest();
request.getSession().setAttribute("abc", request.getParameter("name"));
//Map map=ActionContext.getContext().getSession();
// map.put("abc", request.getParameter("name"));
return "test";
}
}
3.test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:property value="#session.abc" />
</body>
</html>
4.struts.xml
<action name="HelloChina" class="com.xushi.bll.HelloChina">
<result name="test">/pack/test.jsp</result>
</action>
package com.xushi.bll;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class HelloChina extends ActionSupport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
return "test";
}
}
3.test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:textfield value="${requestScope.name}"></s:textfield>
<s:property value="name"/>
</body>
</html>
4.struts.xml
<action name="HelloChina" class="com.xushi.bll.HelloChina">
<result name="test">/pack/test.jsp</result>
</action>
1.2.顯示標簽 property 用于輸出指定值:
<s:property value=" userName "/> 當action返回到指定頁面是,可以通過該標簽顯示action中的屬性
信息。(注 userName必須在action中存在,并有GET方法)。
1.3.往對象中傳值<s:textfield> 用于從頁面往action中的對象內傳值:
<s:text name="user. userName " id="username"/>
該標簽可以在頁面中向action中的實體對象內直接傳值。(注:在action中要存在user對象,并有
SET/GET方法。)
1.4.判斷<s:if> </s:if> 用于在頁面中判斷指定數據
<s:if test="userName == admin">…. </s:if>
<s:else>…. </s:else>
該標簽用于對指定的數據進行判斷,但指定的數據必須在action中存在。
1.5.迭代<s:iterator>用于將List、Map、ArrayList等集合進行循環遍歷
<s:iterator value="userList" id="user" status="u">
<s:property value="userName"/></a>
</s:iterator>
該標簽用于將userList集合中的元素進行循環,用過property進行顯示集合中的指定元素值。
1.6.URL地址標簽,<s:url>用于生成一個URL地址,可以通過URL標簽指定的<s:param>子元素向URL地址
發送請求參數
<s:url action=" ">
<s:param name=" " value=""></s:param>
</s:url>
其中action為請求地址,param子元素為地址后面所帶的參數。
1.7.超鏈接 <a href >一般和<s:url>標簽一起使用,用于帶多個參數。
<s:url id='url' action='HelloWorld'>
<s:param name="request_locale">en</s:param>
</s:url>
<s:a href="%{url}">超鏈接</s:a>
1.8.set標簽,用于將某個值放入指定的范圍內。例如application,session等。
<s:set name="user" value="userName" scope=”request”/>
將user值放入request范圍內。
2. Struts2頁面開發中常用標簽使用規范:
1.1.通過<input name="userName" type="text" class=" " size="15">
往action中傳值,action中要有相應的屬性,并提供SET/GET方法。
(在數量少時可以使用request.getParameter(“userName”);)
1.2.在頁面顯示action傳來的數據,使用property標簽。需要顯示的數據從action中獲得。
1.3.往action中的實體對象內傳值,使用textfield標簽。
1.4.在頁面中判斷指定值使用<s:if>標簽。判斷數據從action中獲得。
1.5.在頁面中需要循環列表顯示的數據使用<s:iterator>標簽,列表數據從action中獲得。
1.6.URL地址和超鏈接可以使用<a href>和<s:url>,在傳多值時建議<a href>和<s:url>一起使用
按字母分類:
A:
<s:a xhref=""></s:a>-----超鏈接,類似于html里的<a></a>
<s:action name=""></s:action>-----執行一個view里面的一個action
<s:actionerror/>-----如果action的errors有值那么顯示出來
<s:actionmessage/>-----如果action的message有值那么顯示出來
<s:append></s:append>-----添加一個值到list,類似于list.add();
<s:autocompleter></s:autocompleter>-----自動完成<s:combobox>標簽的內容,這個是ajax
B:
<s:bean name=""></s:bean>-----類似于struts1.x中的,JavaBean的值
C:
<s:checkbox></s:checkbox>-----復選框
<s:checkboxlist list=""></s:checkboxlist>-----多選框
<s:combobox list=""></s:combobox>-----下拉框
<s:component></s:component>-----圖像符號
D:
<s:date/>-----獲取日期格式
<s:datetimepicker></s:datetimepicker>-----日期輸入框
<s:debug></s:debug>-----顯示錯誤信息
<s:div></s:div>-----表示一個塊,類似于html的<div></div>
<s:doubleselect list="" doubleName="" doubleList=""></s:doubleselect>-----雙下拉框
E:
<s:if test=""></s:if>
<s:elseif test=""></s:elseif>
<s:else></s:else>-----這3個標簽一起使用,表示條件判斷
F:
<s:fielderror></s:fielderror>-----顯示文件錯誤信息
<s:file></s:file>-----文件上傳
<s:form action=""></s:form>-----獲取相應form的值
G:
<s:generator separator="" val=""></s:generator>----和<s:iterator>標簽一起使用
H:
<s:head/>-----在<head></head>里使用,表示頭文件結束
<s:hidden></s:hidden>-----隱藏值
I:
<s:i18n name=""></s:i18n>-----加載資源包到值堆棧
<s:include value=""></s:include>-----包含一個輸出,servlet或jsp頁面
<s:inputtransferselect list=""></s:inputtransferselect>-----獲取form的一個輸入
<s:iterator></s:iterator>-----用于遍歷集合
L:
<s:label></s:label>-----只讀的標簽
M:
<s:merge></s:merge>-----合并遍歷集合出來的值
O:
<s:optgroup></s:optgroup>-----獲取標簽組
<s:optiontransferselect doubleList="" list="" doubleName=""></s:optiontransferselect>-----左右選擇框
P:
<s:param></s:param>-----為其他標簽提供參數
<s:password></s:password>-----密碼輸入框
<s:property/>-----得到'value'的屬性
<s:push value=""></s:push>-----value的值push到棧中,從而使property標簽的能夠獲取value的屬性
R:
<s:radio list=""></s:radio>-----單選按鈕
<s:reset></s:reset>-----重置按鈕
S:
<s:select list=""></s:select>-----單選框
<s:set name=""></s:set>-----賦予變量一個特定范圍內的值
<s:sort comparator=""></s:sort>-----通過屬性給list分類
<s:submit></s:submit>-----提交按鈕
<s:subset></s:subset>-----為遍歷集合輸出子集
T:
User.java
package com.zx.test.model;
public class User {
private Long id;
private String name;
private Integer age;
private String password;
// get/set方法
}
User.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <hibernate-mapping> UserDao.java package com.zx.test.dao; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.zx.test.model.User; public class UserDao extends HibernateDaoSupport { //update, delete ,findAll 方法同上,省略! } UserService.java package com.zx.test.service; import com.zx.test.dao.UserDao; public class UserService { private UserDao userDao; // 在此處添加業務邏輯方法; UserAction.java package com.zx.test.action; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private User user; public void setUser(User user) { public void setUserService(UserService userService) { //添加action方法 } struts.xml: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC hibernate.cfg.xml <?xml version='1.0' encoding='UTF-8'?> </session-factory> applicationContext.xml web.xml: <!-- 配置Struts2 --> index.jsp: <form action="<%=request.getContextPath() %>/user/userAdd.action" name="form1" method="post"> userShow.jsp 在第一行添加 struts2 的標簽 即:<%@ taglib prefix="s" uri="/struts-tags" %> <body>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"
<class name="com.zx.test.model.User" table="LB_USER">
<id name="id" type="java.lang.Long">
<column name="ID"/>
<generator class="sequence">
<param name="sequence">SEQ_LB_USER</param>
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20"/>
</property>
<property name="age" type="java.lang.Integer">
<column name="age" length="2"/>
</property>
<property name="password" type="java.lang.String">
<column name="password" length="20"/>
</property>
</class>
</hibernate-mapping>
/**
* 保存user
* @param user
*/
public void saveUser(User user){
// spring自帶方法 保存
this.getHibernateTemplate().save(user);
}
import com.zx.test.model.User;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void saveUser(User user){
userDao.saveUser(user);
}
}
import com.zx.test.model.User;
import com.zx.test.service.UserService;
public User getUser() {
return user;
}
this.user = user;
}
private UserService userService ;
this.userService = userService;
}
public String userAdd(){
this.userService.saveUser(user);
return SUCCESS;
}
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"
<struts>
<include file="struts-default.xml"/>
<package name="user" extends="struts-default" namespace="/user">
<action name="userAdd" class="userAction" method="userAdd">
<result name="success">/userShow.jsp</result>
</action>
</package>
</struts>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"
<hibernate-configuration>
<session-factory>
<!-- 基本配置 -->
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<property name="bytecode.use_reflection_optimizer">true</property>
<property name="max_fetch_depth">2</property>
<property name="default_batch_fetch_size">8</property>
<property name="jdbc.batch_size">20</property>
<mapping resource="com/zx/test/model/User.hbm.xml" />
</hibernate-configuration>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value ="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value ="jdbc:oracle:thin:@192.168.0.95:1521:ZXDB2"/>
<property name="username" value ="test"/>
<property name="password" value ="test"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="configLocation">
<value type="org.springframework.core.io.Resource">classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="userDao" class="com.zx.test.dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userService" class="com.zx.test.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userAction" class="com.zx.test.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<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>/*</url-pattern>
</filter-mapping>
<!-- 配置Spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:*.xml</param-value>
</context-param>
userName: <input type ="text" name="user.name"><br>
password:<input type="password" name="user.password"><br>
age:<input type="text" name="user.age"><br>
<input type="submit" value="submit"> <input type="reset" value="reset">
</form>
userName: <s:property value="user.name"/><br>
password:<s:property value="user.password"/><br>
age:<s:property value="user.age"/>
</body>
struts.properties 文件可要可無!(可以自己配置)
]]>
</session-factory>
</hibernate-configuration>
系統給的文件 只需要拷貝,都是一些數據庫庫的基本配置
5.在src根目錄 拷貝struts.properties文件
struts.objectFactory = spring 該項是告訴系統 用spring來 控制hibernate和Action
struts.action.extension=action 告訴系統訪問時以什么結尾。可以改為: do 例如:login.do ,此處為:login.action
6.在src根目錄下 創建applicationContext.xml
配置數據源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value ="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value ="jdbc:oracle:thin:@192.168.0.95:1521:ZXDB2"/>
<property name="username" value ="test"/>
<property name="password" value ="test"/>
</bean>
創建sessionFactory 工廠
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="configLocation">
<value type="org.springframework.core.io.Resource">classpath:hibernate.cfg.xml</value>
</property>
</bean>
考來用
7.創建 實體類 <hibernate-mapping> 9.創建一個 Dao 實現操作方法 此處配置了訪問時的路徑:/user/userAdd 14.將8中的配置文件 加入到 4 中 15.寫jsp頁面 表單提交到 Action 9.10.11:是 Action 調用 Service ,Service調用 Dao 也是分層的體現 Web.xml 是web工程的配置文件 ApplicationContext 是 spring的配置文件 Hibernate.hbm.xml 是hibernate的配置文件
就是寫一個bean 含有get/set 方法
8.在實體類所在的目錄下,寫一個 類名.hbm.xml文件,在配置文件中添加屬性
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"
<class name="com.zx.test.model.User" table="LB_USER">
<id name="id" type="java.lang.Long">主鍵的類型為Long
<column name="ID"/>
<generator class="sequence">
<param name="sequence">SEQ_LB_USER</param>指定主鍵生成方式,必須在pl/sql創建主鍵生成方式
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20"/>
</property>
<property name="age" type="java.lang.Integer">
<column name="age" length="2"/>
</property>
<property name="password" type="java.lang.String">
<column name="password" length="20"/>
</property>
所有屬性的name 必須與 bean中的set后的名稱一樣
</class>
</hibernate-mapping>
10 創建 service 調用 Dao中的方法
11.創建 Action 將service的得到的user 加到Action中
12.在applicationContext.xml 中配置
將userDao注入到sessionFactory中
將userService 注入到UserDao中
將userAction 注入到 userService中
13.在struts.xml中 配置Action
<package name="user" extends="struts-default(固定與include對應)" namespace="/use(訪問時路徑)r">
<action name="userAdd(訪問時路徑)" class="userAction" method="userAdd">
<result name="success">/userShow.jsp</result>
</action>
</package>
<mapping resource="com/zx/test/model/User.hbm.xml" />
引用配置文件
建表是 注意創建主鍵時 name 為 表明
]]>