该用户从未签到
struts 2、hibernate 、spring 所需jar包 - i8 {1 Q( C, V7 h" p
6 \) k5 p& R+ x' P: |0 l4 m struts-core-2.x.x.jar ----struts核心包
( E% t' P: g* p; F xwork-core-2.x.x.jar -----身体ruts在其撒很难过构建
1 Q6 b1 r3 K3 h9 ^( L& s+ B ognl-2.6.x.jar ----对象导航语言 / G9 I \# a) o
freemarker-2.3.x.jar ------struts2的ui标签的模板使用 1 b/ b" H0 [. V: r8 p
commons-fileupload-1.2.x.jar ----文件上传组件 2.1.6版本后需加入此文件
% e: y6 \6 d0 x" U" M. F struts-spring-plugin-2.x.x.jar ---用于struts2继承spring的插件
2 {, \* v2 W7 d8 @
* P# [" U: u1 `+ P hibernate核心安装包下的(下载路径:http://www.hibernate.org/ ,点击Hibernate Core 右边的download) 6 S! o- `; z1 z) j7 p. G
hibernate2.jar
0 ^. h& C8 J7 X% W) i lib\bytecode\hibernate-cglib-repack-2.1_3.jar 6 W8 J7 d8 \3 x4 Z1 B8 f: w
lib\required\*.jar
& W$ L4 q+ |7 z6 C- b* z& r9 g3 S hibernate安装包下的(下载路径:http://www.hibernate.org/;点击Hibernate Annotations 右边的下载) ( b% v& ^1 q0 j, I
hibernate-annotations.jar
5 f4 a& X* X0 S3 ` lib\ejb3-persistence.jar、hibernate-commons-annotations.jar 5 w+ G8 \0 U6 P- f2 K
hibernate针对JPA的实现包(下载路径:http://www.hibernate.org/ ,点击Hibernate Entitymanager右边下载)
0 g- E. o! |' p ~4 ~ hibernate-entitymanager.jar
" g. e1 ~9 Y. K" X8 a$ H lib\test\log4j.jar、 slf4j-log4j12.jar ' ~; A* w, w1 n! F
# t9 V, `; ~. t2 u spring安装包下的
! c6 U6 j) E& x9 K dist\spring.jar
6 x; F( P* d' q; I! p# M9 K2 G) w6 d lib\c3p0\c3p0-0.9.1.2.jar
: s4 X* A8 K- `1 e: o1 {0 z9 K lib\aspecti\aspectjweaver.jar - Z3 O/ O4 q5 t2 [" n" O
aspectjrt.jar
) k/ ^3 ~$ C7 |3 D, }' H1 C# o$ Q lib\colib\cglib-nodep-2.1_3.jar * w# V7 { o' H; `, l( f* T
lib\j2ee\common-annotations.jar
6 y: Y& H3 w. L. _2 A; Y+ O vlib\log4j\log4j-1.2.15.jar
2 @: {4 J+ X1 K/ w" V lib\jakarta-commons\commons_loggin.jar 8 g5 _/ d. W# O& T% `9 g; Q
( D4 R/ n8 U m! A$ y" {7 {. W 数据库驱动包
/ s9 A# h) u, W* Y) c" [; t b: \* N: N9 ?; x
2 `( z, A2 h7 w
2 T; C7 B, p" x0 m! H1 E 引用& [ ^/ \- k m$ |( w' Q1 P) Y
* z4 ^& C* F* c' p
( E( s! P! l7 N- W2 D 创建mysql数据库ssh 设置编码为utf-8 语句: 1 M1 Y/ _; M" u H9 f
create database ssh character set 'utf8' collate 'utf8_general_ci'
5 S: m7 s5 U0 V. r! r1 M# H ( D8 K: o# S s2 p1 V# e: {
9 t o+ X( i- g8 L1 D : K! r: O2 m5 |) G8 M
引用
& R' _+ \" `: d( K( P8 G5 [ 3 O+ @7 P& {4 G5 r7 ^- ^% v c0 T5 u
4 Y6 s4 S+ S: M: r$ w; ^) X3 {0 q 1.先整合spring和hibernate - g2 p2 d: `) K) {" e3 {9 X7 g7 ]
*将spring和hibernate的jar包放入lib下;
- s2 I" e1 f5 ^ *创建spring的beans.xml配置文件 / W* }' @4 I4 @; x6 e" n! Z
4 O& g7 g$ J& u% j8 g
8 I0 b# j |, c+ w; i ( v) {; U' Q6 S3 d! x& [5 [( n- G
java 代码
, T! [3 P' c7 J 1.<?xml version="1.0" encoding="UTF-8"?>
' ?9 O! a" I) ^$ s8 ^" X 2.<beans xmlns="http://www.springframework.org/schema/beans"
) s4 v8 @6 @9 ^4 \% Q2 T0 o 3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 1 \! V: O( _" S2 P$ Q4 i! f# \
4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" & l5 s$ {. @& G5 f! L2 @; G
5. xsi:schemaLocation="http://www.springframework.org/schema/beans 3 r# ]! u- N4 W9 r t
6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 1 D. ?" y' {6 h8 M- y: ?$ q
7. http://www.springframework.org/schema/context N& } z; p) l" C
8. http://www.springframework.org/schema/context/spring-context-2.5.xsd
o0 u3 a! M, [$ j1 d, H: c 9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
5 _' M+ t, ]; _" Z& ?" C- z 10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
* n7 l4 H, b3 ^ 11. , x6 s- M/ q F
12. <!-- 将bean交由spring管理可以 用<bean></bean>和扫描加注 --> % z" G+ P ^3 L- n% x8 ^3 ^, R) @
13. <!--
* H" e/ D( [3 k# b8 K7 I, V2 Q( ` 14. 扫描该包及该包下的子包 8 }& ~9 t, ]6 b. D9 V
15. --> 7 t! N0 v3 Q* {: U
16. <context:component-scan base-package="com.yss"></context:component-scan> 1 T6 i$ G1 h9 X3 t2 [+ o T
17.
- O) k3 m3 H/ T; f- D 18.
$ ^* B3 G" ?# o' b7 K 19. <!-- 集成hibernate sessionFactory单例模式 线程安全 创建耗内存--> $ x$ D- a) M5 J) ~2 Y. `
20. <!-- 将hibernate的事务也交由spring管理 -->
# t6 _) I$ y3 ^# {! f- U 21. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
, _% f# b: Y$ ?1 a$ z! U" L 22. destroy-method="close">
6 ^# L# e( C% j" O0 G0 q 23. <property name="driverClass" value="org.gjt.mm.mysql.Driver" /> ; k o' f* q5 g6 e/ N' o3 k S+ h
24. <property name="jdbcUrl"
3 h+ V. ~5 E+ I) O 25. value="jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=UTF-8" />
8 _& G0 L' l# i) N 26. <property name="user" value="root" /> . o# \! Z& B6 S/ F! [! L
27. <property name="password" value="root" /> & L3 t; D# K3 r- _" M
28. <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
; c1 Z7 n" V9 k B* x# y 29. <property name="initialPoolSize" value="1" />
4 _2 \1 P, h" n6 U: a! P 30. <!--连接池中保留的最小连接数。-->
1 v! d3 W1 w+ O5 H: L 31. <property name="minPoolSize" value="1" />
; |; d" |0 `" v9 c3 O0 x 32. <!--连接池中保留的最大连接数。Default: 15 -->
$ ]! Z$ K( f1 V 33. <property name="maxPoolSize" value="300" /> 3 P9 W6 }" @$ M2 q P! q- r. g& j
34. <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> 7 T" \& I Z% ^( C. o
35. <property name="maxIdleTime" value="60" />
& X$ _2 G+ { z+ w0 x0 @( G6 `5 x 36. <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
8 r4 y; Z% h. l& w4 T$ m; I 37. <property name="acquireIncrement" value="5" />
. o% ~+ T4 f) e9 z 38. <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
0 y4 S, [- ?8 m* L' M# _ 39. <property name="idleConnectionTestPeriod" value="60" /> $ h7 c! G/ T4 e& J
40. </bean> 9 P T n7 a. G% ^' d
41. 9 _3 H7 Y0 O/ s4 l* c
42. <bean id="sessionFactory" * Y- I' O7 v* j
43. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 2 G( S9 W5 _1 d5 V h, U
44. <property name="dataSource" ref="dataSource" /> + M. X+ q- Z% N) j) e1 G
45. <property name="mappingResources"><!-- 放置hibernate的配置文件 --> ) M0 h- P/ Q) P' `0 X: h6 i( L
46. <list> 2 V/ M& @& @3 P1 g$ Z
47. <value>com/yss/bean/Employee.hbm.xml</value>
0 Z( a* n' |$ u5 T6 F 48. </list>
; R1 v3 o( y* j& h( r4 u8 p+ l 49. </property> ( T. u6 e' a/ U8 U3 ~
50. <property name="hibernateProperties">
. E" E. E. V; D( w+ x2 D- n$ g 51. <value>
' t/ Y' C+ b) N' g 52. hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
6 {+ ?$ H3 i# i+ D 53. hibernate.hbm2ddl.auto=update
# {) |/ D. @/ \ 54. hibernate.show_sql=true 5 B# u% w+ r- U" v; F
55. hibernate.format_sql=false 2 t! U" V9 {# f5 j
56. </value>
( v0 T, O9 V( H" F% p 57. </property> , _8 b4 B2 _2 V, t( w% A7 _
58. </bean>
0 t! O( q4 N X 59. $ M4 U J9 ]" H* c3 P, D
60. <!--hibernate事务管理器配置-->
8 L0 d Z: s$ @2 {) M) t7 G 61. <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> - W e. f: |( M( ]
62. <property name="sessionFactory" ref="sessionFactory"></property> - O, N/ q/ y. L
63. </bean> % `7 u, l7 k5 \: E1 k
64.
5 e/ d7 m& e8 O! w 65. <!--spring可以用xml和注解来配置事务 声明 --> 5 @6 }5 I# c) M2 e! {- S0 C* ]
66. <tx:annotation-driven transaction-manager="transactionManager"/> ; `" `( a8 A/ e+ w2 A# ]4 D- d
67.</beans>
5 \7 N! T% `7 P0 F 6 u) C# q, g F, U- i' \& m+ L4 w
*配置hibernate的model.hbm.xml和创建model类 % }+ k" ~# n% c
*创建service h; p2 k1 Z# m$ V/ D3 K/ q
service接口: # J4 i/ [9 Z# Q# _
( P9 u5 c. C# t
* C1 E+ k3 @& ?# s
, l$ `$ V+ G: Q) D& f4 \% D; G Java代码
. A% K- o8 h* A) U v6 k 1.public interface EmployeeService { + c# g6 [* w7 [. g) c9 v
2. public boolean save(Employee employee); " t- z* F3 \; ?; J9 ?; L
3. public boolean update(Employee employee);
4 C% q' T/ t' U8 m 4. public Employee find(String username);
1 A- Y3 S5 j# R4 \/ V 5. public boolean delete(String... username);//表示可变参数 6 I" M/ G1 j8 p7 Y* ]% q% M. D) G
6. public List<Employee> findAll(); # p4 G B+ F& F. y2 C9 O$ U0 Q
7.}
/ I1 a; l2 s* t( S: Z0 F
' h# L0 C2 c$ b2 B% O service实现类:
% D% s7 k) V2 j- }6 z3 `+ V0 G: Y3 l
0 h0 H5 x$ k* k$ l2 F/ E. Q! w2 k n" C5 c% ?8 [6 [9 L- _
* k3 g3 [4 `" ]' v3 Q Java代码
5 F& [8 I2 L) ?, c. A8 b 1.import java.util.List; 3 D3 i( k; t2 L" O1 o; f8 |2 P1 ~
2.
/ [. Q1 @ {& v' J2 E 3.import javax.annotation.Resource; - e4 m) @/ W1 {% s% G
4.
9 E( ?7 s+ ]6 n; Y. G3 z0 }$ C+ r 5.import org.apache.log4j.Logger; 6 U8 K. N; ] B
6.import org.hibernate.SessionFactory; 5 l+ D: O0 M. p. f: `3 t2 ^
7.import org.springframework.stereotype.Service; % V3 O% @6 _0 S1 p# u- v
8.import org.springframework.transaction.annotation.Propagation; 2 Z' K. Y/ `) K/ e6 U
9.import org.springframework.transaction.annotation.Transactional;
& U$ ^" \5 E9 j) `% F; X! C 10. 6 p. p" ?- z9 J7 o8 A) Q2 H9 `( B% K
11.import com.yss.bean.Employee;
' ]9 Z$ d z# [/ e 12.import com.yss.service.EmployeeService;
( Q% s& P; H* d- i& s 13.
% \ u" ?1 y5 D9 y# U0 \& P 14./** 4 {; f1 R! X# M6 Z* ]1 W
15. * @author qing 默认bean名称 employeeServiceBean * Z* y* m' r3 F, [9 |: E
16. *@Service @Transactional 注入service和开启事务
' M* b9 }6 T" D5 L9 E 17. */ + |+ |, H5 m0 k; ]
18.@Service
$ s6 D0 w0 E6 C0 E 19.@Transactional
. }2 m7 L; I4 Y$ @. I% d, W2 d- F 20.public class EmployeeServiceBean implements EmployeeService {
5 ^/ P$ \% p2 z) i6 t 21. private static Logger logger = Logger.getLogger(Employee.class);
. L2 F/ o, Y( {! t4 G0 e 22. /**
; {+ S9 y6 d4 Y0 n P5 \' K 23. * 注入sessionFactory
. M) \, X' o5 U1 K6 y6 z+ ^ 24. */
! `; J8 L6 S! Z 25. @Resource SessionFactory factory;
' y5 M* C: F) b# @$ A4 b4 r5 S 26.
' h5 Y1 f0 I. C( B- P7 Y+ C# x# G" ] 27. public boolean delete(String... usernames) {
# M& H7 I0 f6 J- i2 i" O) c- S" J 28. try { ) H$ T4 {: g9 u \
29. for (String username : usernames) {
5 u6 U v( `5 g8 S2 M 30. factory.getCurrentSession().delete(
+ g$ L" P3 l l7 C/ y 31. factory.getCurrentSession().load(Employee.class,
" n$ K* L& F& J: q4 `6 K0 K3 H7 w. \ 32. username));
- G/ |* ]9 }7 F. I 33. }
. t& a- c. J2 e& Z# A; {6 g 34. } catch (Exception e) {
7 t( K$ n( M4 B5 e$ F; X) k 35. logger.error(e.getMessage()); + E# c1 b L! G, {
36. return false; 5 \1 S3 z5 f, p9 U. b
37. }
) ]/ P: C. Z/ u5 d) r# z+ v8 h 38. return true;
0 o* y. \' ^9 C9 a" D& ~% b 39. } % b1 ^* w0 a p4 w/ ~ M S" T* h
40.
; i, k) }% s8 R6 J, w% W 41. /* ; Y; g: g$ K5 @+ D. \
42. * (non-Javadoc)
* j# V# N! u( P3 e9 o8 i 43. *
% H0 p8 k/ e' |6 { ^ M v4 D 44. * @see com.yss.service.EmployeeService#find(com.yss.bean.Employee) $ \2 C/ |: p0 _/ D/ L. E. H
45. * 此标注表示不需要事务处理
- c* ]) [$ \6 U/ V( i) { 46. */ * e9 Y, t+ c. ]+ r& ]
47. @Transactional(propagation = Propagation.NOT_SUPPORTED) # K$ u3 i& l( G& Z
48. public Employee find(String username) { 6 i# J6 H _, i
49. return (Employee) factory.getCurrentSession().get(Employee.class, 5 a& `/ w x0 F: d% N$ Y0 ?
50. username);
& }" n$ g/ E& L f/ l 51. }
( w6 b$ J, q, e2 c0 b# A" D# P 52.
$ g% l: M( |, \2 A. g 53. @SuppressWarnings("unchecked") ; T5 U7 F; a' i7 U3 p
54. @Transactional(propagation = Propagation.NOT_SUPPORTED) + b0 j& ~% z$ ^
55. public List<Employee> findAll() { 2 d6 t6 }' F; _) D4 t' N7 B
56. return factory.getCurrentSession().createQuery("from Employee emp") ]5 j6 l9 `: P2 p, l0 r4 W$ q! P' _
57. .list(); $ W" I& Q- B1 o6 n4 `
58. }
6 p. ^8 o8 B8 \0 ~$ K7 D2 r 59.
! J+ z/ @ g m$ S. X R, m0 @, P 60. public boolean save(Employee employee) {
. `5 q3 `8 P- Y" ~, E' k# R! p& [ 61. try { % I. [# E3 J* J% G4 m
62. factory.getCurrentSession().persist(employee);// .save(employee);//
3 N0 Y0 v4 c0 W! V 63. // 获取已经开好的Session & c% n: K+ |3 `, k1 F
64. } catch (Exception e) {
* g* a" L4 ~5 r, ^+ ]- t( r 65. logger.error(e.getMessage());
+ j+ j/ C* G o1 \ 66. return false; " Z' C2 R M9 _/ ~# M
67. } 5 R# j* N# D# {* N0 a. X
68. return true;
) ]# z# H- p4 }9 h) |% r 69. }
1 i0 g2 x) Y) s9 U" B) | 70. 2 {& J/ B! S# [
71. public boolean update(Employee employee) { l- t+ F1 q, [- |! [, S
72. try {
- `; L* e9 r8 ^4 ]/ l 73. factory.getCurrentSession().merge(employee);// 类似于saveOrUpdate()方法
. Y9 }) {, a6 { 74. } catch (Exception e) {
8 ~6 X( M+ w! J& t 75. logger.error(e.getMessage()); " F4 _% p* @- {3 H- y- ^
76. return false; * ^( Q# I d# W! e' T% v
77. }
% `% y% F$ z3 `8 e1 o 78. return true; ! y* E4 f# e0 X5 H9 C2 o
79. } ) j V. S6 q/ \& a K9 ~0 m$ p* ?5 z
80.
3 @8 ^5 T! n4 K# x0 r2 g b: ^' U 81.}
' v& N& L& [5 C" ]* V8 ?" Q* L ) C4 F4 n! m; Q$ q; \
*新建测试类
. C* A. r/ r* W; ]! I2 m! z 9 |: N$ v2 f5 \9 z
$ @! e# T6 U4 }6 v+ f2 |$ |
# J5 _. s$ u; ^ _, F$ G Java代码
# f3 ?* w5 c' }6 E A 1.public class EmployeeTest {
* i& q0 p# l+ a 2. private static EmployeeService employeeService; ) l0 t+ m3 ^: n+ Q9 A9 k& ^: m4 `
3. 9 J7 C% F4 V E! f/ x/ s/ {% c
4. @BeforeClass 7 P( t" h: `- {3 P$ G9 F$ Q
5. public static void setUpBeforeClass() throws Exception {
1 m( `* r1 \7 g 6. try {
& E) H2 b# e! F 7. ApplicationContext context = new ClassPathXmlApplicationContext( ' ]/ R9 }9 D1 w- G
8. "beans.xml");
- @/ ^" @4 q+ C! e/ C2 X* f2 }5 | 9. employeeService = (EmployeeService) context
% t# c6 L2 y+ x% h: V! | 10. .getBean("employeeServiceBean"); 8 X. a. ?: W6 }8 ]0 M
11. } catch (Exception e) { 2 G8 P' B+ s/ L8 w. U7 Y0 d
12. System.out.println(e.getMessage());
$ I2 {8 _5 {1 ]( g/ |1 G2 p 13. } ]8 C+ @: f/ ]0 Z0 n- R9 {
14. }
g9 ~' l" T0 l. j 15.
, f2 P% d5 G% @: V, I 16. @Test
$ a9 S/ F7 L) K* B) h4 m% A& y. h" d 17. public void createTable() { 5 C; B' t' W2 d4 Q) M$ S
18. //new ClassPathXmlApplicationContext("beans.xml"); 5 ^7 N+ A' Y8 v/ K
19. }; , a) O* r/ t3 }, O" C
20. 4 S# r% g8 H5 F+ I- U3 S1 G
21. @Test
4 A7 K; z$ M$ D" m3 T& v1 i- g. M 22. public void save() { - _. Q' K$ K+ w1 a# y; Z$ \
23. boolean result=employeeService.save(new Employee("long","long"));
, {. q/ {; j/ Q2 e3 d 24. if (result) { ; h3 }0 ] `, @ L4 r, |
25. System.out.println("保存成功。。。。。");
' V& _3 G5 b) y 26. }else{ . C- a4 i" s5 t" d7 [
27. System.out.println("保存出错...."); ( p" ]$ ~" N$ p: P& x$ Z; x2 g
28. } 9 S8 ^, ]7 y8 V7 i+ _
29. }; ( v. j& V. f- ^( k
30.
0 S) r' ^- E6 R% i& {% m 31. @Test
+ y. p0 @/ B' P/ m4 o: I6 Z 32. public void delete() {
. K% V3 v- g3 z$ B; U3 { 33. boolean result=employeeService.delete("long"); 8 |) l- X1 o' a( q* d: t' e
34. if (result) { . q- i( X( ?9 Q- ~: P7 q2 B/ m
35. System.out.println("删除成功。。。。。"); 7 F$ d- s- M; h+ M9 X6 [) D9 x
36. }else{ ( r# i& h$ n* ~( o: Y* O# X, U" M
37. System.out.println("删除出错....");
7 K. ?, \6 r! [1 b+ p* \' H$ ~+ k 38. }
( Z4 ?! N6 R! k4 P' I6 s2 m 39. };
$ v) C2 U1 |* \ C 40.
! f, u- n) q/ v& V& [" n: ], b0 W; H 41. @Test
6 F% Y3 o: M, @5 x# |2 e 42. public void update() { 4 o3 E v! O$ R$ u3 r- Q5 ~
43. boolean result=employeeService.update((new Employee("qing","long")));
+ E6 v' F$ U1 v* P9 c 44. if (result) {
. n- Q; I7 k2 W2 _# c( H8 R6 P 45. System.out.println("更新成功。。。。。"); " J; \$ F+ N0 ~) S* [ ~5 K6 H
46. }else{
6 W$ j. a5 o, ^' Y0 S, f 47. System.out.println("更新出错...."); 0 F/ s+ L$ z% O; C, _8 O: q0 n
48. }
. N7 n* t" B% o. |8 p7 y, N/ W, i 49. };
3 ~& ^, E' q9 M0 ~3 w: }: v* v 50. ' w3 ~& L; w0 |8 Y
51. @Test , Z6 K9 J2 i( Y+ [) u
52. public void findAll() {
7 H( f1 a |& \& Y* L" c0 H& H9 d 53. List<Employee> elist=employeeService.findAll(); ! U! y* w1 e* F' i& {2 y4 v- R4 H
54. Iterator itor=elist.iterator(); 6 a9 x. z2 \7 E. q; n. w" ^
55. while(itor.hasNext()){ # G1 |9 A1 M- ?* ?5 ?$ T# t
56. Employee emp=(Employee)itor.next(); # e2 g' y4 y2 I- C9 F
57. System.out.println(emp.getPassword());
1 k$ `# U3 Z! o8 o, [ 58. }
+ ^- o: }- \4 D1 Z, V 59. };
- L' W) l& r" A 60. - Z% a) o* c# L1 w9 Q
61. @Test . n" {) E! L I7 i! K. \+ T
62. public void find(){ / ^' @. r g( @& v0 v9 g
63. Employee employee=employeeService.find("qing"); 3 m" I* x g$ Y) J
64. System.out.println(employee.getPassword());
- t) f: F; ?. @7 m( i* k/ A 65. }
( j' M7 l+ v% d8 c7 ~ 66.} N1 _2 T, N4 p
2 a5 q- S& B7 U6 {& ?5 o3 a *ok 没问题spring和hibernate整合完毕 ' F9 y3 I1 m1 ] T7 @9 X8 k
0 h5 W, \. R/ j( p; k$ o5 ^( v *再将struts2所需包加入lib中 . h+ y! J' N% Z' a; A+ W$ U8 _
*创建struts.xml配置文件 3 t$ e. ?; Y! A% p) { K5 n
: Q* X3 H' {! W: \( s2 [6 D $ \2 Q4 i- W0 M. F" ^
/ p/ g3 B) U1 j1 N
Java代码
! |$ }. x: z/ P% V/ c, v 1.<?xml version="1.0" encoding="UTF-8" ?>
7 T( M Z* j% W3 \) l 2.<!DOCTYPE struts PUBLIC : P* ^# ^) @1 ^' P9 i: z, D+ m
3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" % J9 p! R4 j7 s: w' T/ h
4. "http://struts.apache.org/dtds/struts-2.0.dtd">
: o) y2 y9 q3 c* t 5.<struts>
+ ? C8 X' q) w0 x; w g$ A9 l- J# L 6. <!-- 将struts的action交由spring管理 不在由struts的工厂介入 -->
) w% u: C5 U o4 u5 [* q2 c 7. <constant name="struts.objectFactory" value="spring" />
) c6 ?5 Q2 f |4 _! l. t 8.
% A* n% A% y' B Q$ q 9. <package name="employee" namespace="/employee" extends="struts-default"> ! Q# ^" a+ Y( g
10. <action name="list" class="employeeAction"> ( B( H& o; o: z1 G# m
11. <result name="success">
T4 `% U' @9 V! m& U1 X 12. /WEB-INF/feapp/employee.jsp 6 ?8 ]& U5 O3 z3 C, f1 T! y
13. </result>
. q+ ~4 p, ^& S4 x/ J 14. </action>
1 P% T& v% t& O 15.
! c1 U0 ^ q5 d& ? 16. <action name="manager_*" class="employeeManagerAction" method="{1}">
0 Q K5 X4 O& E& J+ x' c9 @3 j7 b 17. <result name="success"> 5 Q# @% {' k# e% ?/ H. P
18. /WEB-INF/feapp/employeeadd.jsp
/ T4 S5 l3 T% V# w, N. `- V 19. </result>
9 k) H, t8 x6 O6 r 20. <result name="message"> % e8 F! E; ^7 b3 \6 I) W
21. /WEB-INF/feapp/result.jsp
4 `( a$ h5 \1 l) f" _) l 22. </result>
, _ B' d4 v( z- T* d 23. </action>
9 W: ~6 V& P$ P- ~ 24. </package> 2 O; q' t4 p+ f
25.</struts>
1 f% l& R+ s6 H5 G+ Z7 e2 L 2 W. p- u9 A$ r/ U" o; I; U# ?
*在web.xml中加入
$ t: a$ e! n5 m! ` " N" C( ~5 [8 }% U
- T |" @+ b& B2 x
5 C" h4 `9 l4 e1 i V0 n& H* {' \& R Java代码
7 n% d8 w- h' f1 C# ]" c6 [4 W 1.<?xml version="1.0" encoding="UTF-8"?> . U, |7 Z$ `) \- n: g# f2 E
2.<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" ! V3 A* v2 E9 O2 P2 K
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
/ r3 ^! e/ |6 q* w8 _ `9 [6 F 4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
, ` E6 C7 g T1 J8 p 5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
9 }9 I/ k* h8 h8 J u/ n" p# ] 6.
) ~. K8 o8 N) ]; `8 a: Q( u& z 7. <!--
/ | \; z# T7 ?4 m. O" J 8. 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找
; |; K4 \. L6 T! u( I+ ^" Y 9. -->
8 ^; _* m/ S1 Y 10. <context-param> % l. G& r) Q5 S
11. <param-name>contextConfigLocation</param-name>
3 @9 V. J" Q9 k) J5 H 12. <param-value>classpath:beans.xml</param-value><!-- 多个配置文件的写法 classpath:beans1.xml,classpath:beans2.xml,classpath:beans3.xml --> 7 a' J: `' @5 l! A k/ S
13. </context-param>
2 u/ `4 w0 {+ p+ p 14. <!-- 对Spring容器进行实例化 -->
5 c4 I& T) v. f' t9 Z4 t 15. <listener>
! y0 f1 P: H% y V( K" m: ~6 D$ k- S 16. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
1 L3 P; Q. f2 Y8 m+ l, H 17. </listener> * Z3 E% p* t1 `; p
18. - H' ?6 F7 W) l% S" ~/ H2 l) W
19. <!-- struts2 的监听器 --> . L% f, X+ f/ Z7 b. g u9 A7 C
20. <filter> * a) z3 C7 {$ b' N* T# p n& L
21. <filter-name>struts2</filter-name> 5 w7 N6 n y' v7 k
22. <filter-class> 5 G; f8 J7 u, n3 L1 n: G9 c
23. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> . j1 I# Y( w$ z. w' l
24. </filter> $ b/ A* T0 o" m) q' A# t/ R
25. <filter-mapping>
4 E" c+ K* M* r 26. <filter-name>struts2</filter-name>
$ X) h/ h E: X. S; J 27. <url-pattern>/*</url-pattern> 7 Q a- N0 E% Q! O4 A
28. </filter-mapping>
4 B1 l3 w5 |8 h' F i; G 29. ; n6 f9 e& m$ Q' r, F. O' A7 r
30. <welcome-file-list>
% J+ r! z/ l$ x2 ^- K7 _9 D 31. <welcome-file>index.jsp</welcome-file>
9 q: `5 d) u$ t8 B 32. </welcome-file-list> % K1 B1 d: _4 \ j
33.</web-app>
3 r3 v' e0 a0 H+ d
* H, J" ~2 {1 U3 N( h$ { *创建相关jsp和action
, o/ [6 s. J1 a0 M* e: o+ y " x' F+ F/ f) S4 N6 ]8 ]9 h
' V. D8 m7 Q3 R o3 ?0 ~6 @
% y0 ~% R1 `' e7 Z* {. W Java代码 2 }* J! |/ c) C" z9 ~7 T1 ^
1.@Controller @Scope("prototype")
5 @4 j# H$ n$ N 2.public class EmployeeManagerAction extends ActionSupport { # v% A0 L0 A( v
3. @Resource EmployeeService employeeService;
7 n/ Q- ^& R' x% G- ?3 s 4. private Employee employee;
+ k+ C& P: I( y" b 5.
2 W' r4 h A* \6 R$ J0 L 6. public String addUI(){ , x; \7 B, a& g
7. //System.out.println("user come");
3 v7 d/ b9 I! J. k/ A! h O# b 8. return SUCCESS; * [& y7 V* ]: {+ p
9. }
; V$ H8 g2 H( o 10. ' M$ c2 B/ v: X1 N6 J- i
11. public String add(){
% J1 d- j R" J* | t' a; X* q 12. //System.out.println("--------------");
) ?4 z. m9 a# ?- s4 \# \: N 13. boolean result=employeeService.save(employee);
$ t& ?9 v- t2 h* W6 m m, m 14. //ActionContext.getContext().put("genders", Gender.values()); & e) v9 n9 X d% F1 R) S( o
15. if(result){
4 _5 v5 S+ ?) n; Y0 J 16. ActionContext.getContext().put("message", "保存成功!"); ( U- O0 c* y b9 M! h1 Q
17. }else{ 6 }* g0 ]( m" e% L7 P
18. ActionContext.getContext().put("message", "保存失败!");
1 H; b+ h( C3 Z3 ~" Q5 ]. h0 x K 19. } ' k a* r; m; ^' [2 ?( G
20. return "message";
2 \* K: C; C% S. P7 [6 i 21. } 6 B# G7 A/ K) ^# }8 J% e3 ~
22. 0 X) ?0 W# {1 P7 ?" Q/ {0 x
23. public Employee getEmployee() { 9 ^; L, _# J6 ?/ B$ x6 G1 E1 y" a" r& S
24. return employee;
: L( g9 q* [2 X. V1 e* s* J 25. } & h! R) f" i- S
26. " S) `9 E w/ \
27. public void setEmployee(Employee employee) { 3 r1 J! E- N" f2 k# f& [' o
28. this.employee = employee; % o8 I5 p" V! k, O
29. }
! n" C3 H% N) |" r) m 30.} 复制代码
, P4 j0 m( T2 r3 z4 `+ J' j / T4 t$ d% I. r' c. N# P# Z+ N/ I
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :