web工程配置struts2只要3步
1、添加架包
struts2-core-2.0.6.jar、xwork-2.0.1.jar、commons-logging-1.1.jar、freemarker-2.3.8.jar、ognl-2.6.11.jar、(ojdbc-1.4.jar)oracle驱动包
2、在src目录下新建struts2配置文件struts.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? xml version = "1.0" encoding = "GBK" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" < struts > < package name = "user" extends = "struts-default" > < action name = "user_*" class = "com.hyw.action.UserAction" method = "{1}" > < result name = "list" >/user_list.jsp</ result > < result name = "edit" >/user_edit.jsp</ result > < result name = "show" >/user_show.jsp</ result > </ action > </ package > </ struts > |
3、配置web.xml文件
1
2
3
4
5
6
7
|
< 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 > |
到这里struts2就已经和web工程集成了。
例:做一个对用户表的增删改查操作。
1、首先创建表和序列
1
2
3
4
5
6
7
8
|
create sequence seq_userId start with 1; //创建序列 从1开始递增 create table t_user( id number(10) not null , name nvarchar2(50) not null , pwd nvarchar2(50) not null , sex number(1), age number(10) ) |
2、创建JDBC连接数据库类JDBCManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package com.hyw.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JDBCManager { public final static String DRIVER = "oracle.jdbc.OracleDriver" ; public final static String URL = "jdbc:oracle:thin:@localhost:1521:huangyuewang" ; public final static String USERNAME = "hyw" ; public final static String PWD = "hyw" ; public static Connection getConnection(){ Connection con = null ; try { Class.forName(DRIVER); con = DriverManager.getConnection(URL, USERNAME, PWD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return con; } public static void closeAll(Connection conn,PreparedStatement ps,ResultSet rs){ try { if ( null != conn) conn.close(); if ( null != ps) ps.close(); if ( null != rs) rs.close(); } catch (Exception e) { e.printStackTrace(); } } } |
3、编写User.java实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package com.hyw.entity; public class User { private Integer id; private String name; private String pwd; private Integer sex; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this .pwd = pwd; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this .sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this .age = age; } } |
4、编写数据访问层UserDaoImp.java类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
package com.hyw.dao.imp; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.hyw.dao.UserDao; import com.hyw.entity.User; import com.hyw.util.JDBCManager; public class UserDaoImp implements UserDao { private Connection conn = null ; private PreparedStatement ps = null ; private ResultSet rs = null ; public void saveUser(User user){ conn = JDBCManager.getConnection(); String sql = "insert into t_user(id,name,pwd,sex,age) values(?,?,?,?,?)" ; try { int id = 0 ; String sql2 = "select seq_userId.nextval from dual" ; ps = conn.prepareStatement(sql2); rs = ps.executeQuery(); while (rs.next()){ id = rs.getInt( 1 ); } ps = conn.prepareStatement(sql); ps.setInt( 1 , id); ps.setString( 2 , user.getName()); ps.setString( 3 , user.getPwd()); ps.setInt( 4 , user.getSex()); ps.setInt( 5 , user.getAge()); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { JDBCManager.closeAll(conn, ps, rs); } } /** * 修改用户 */ public void updateUser(User user){ conn = JDBCManager.getConnection(); String sql = "update t_user set name=?,pwd=?,sex=?,age=? where id=?" ; try { ps = conn.prepareStatement(sql); ps.setString( 1 , user.getName()); ps.setString( 2 , user.getPwd()); ps.setInt( 3 , user.getSex()); ps.setInt( 4 , user.getAge()); ps.setInt( 5 , user.getId()); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { JDBCManager.closeAll(conn, ps, rs); } } /** * 删除用户 */ public void removeUser(String ids){ if ( null != ids && ids.length() > 0 ){ conn = JDBCManager.getConnection(); String sql = "delete from t_user where id in (" +ids+ ")" ; try { ps = conn.prepareStatement(sql); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { JDBCManager.closeAll(conn, ps, rs); } } } /** * 查询所有的用户记录 */ public List<User> findUsers(){ List<User> list = new ArrayList<User>(); conn = JDBCManager.getConnection(); String sql = "select * from t_user" ; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()){ User user = new <
|