struts2 的mvc模式配置和JDBC连接oracle数据库开发


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  User(); 
     user.setId(rs.getInt( "id" )); 
     user.setAge(rs.getInt( "age" )); 
     user.setName(rs.getString( "name" )); 
     user.setPwd(rs.getString( "pwd" )); 
     user.setSex(rs.getInt( "sex" )); 
       
     list.add(user); 
   
   catch  (Exception e) { 
    e.printStackTrace(); 
   finally
    JDBCManager.closeAll(conn, ps, rs); 
  
     
   return  list; 
  }  
  /** 
   * 查询所有的用户记录 
   */
  public  List<User> findUsers(User user1){ 
   List<User> list =  new  ArrayList<User>(); 
   conn = JDBCManager.getConnection(); 
   StringBuilder sql =  new  StringBuilder( "select * from t_user where 1=1 " ); 
   if ( null  != user1){ 
    if ( null  != user1.getName() && ! "" .equals(user1.getName())) 
     sql.append( "and name like '%" +user1.getName()+ "%' " ); 
  
   try 
    ps = conn.prepareStatement(sql.toString()); 
    rs = ps.executeQuery(); 
    while (rs.next()){ 
     User user =  new  User(); 
     user.setId(rs.getInt( "id" )); 
     user.setAge(rs.getInt( "age" )); 
     user.setName(rs.getString( "name" )); 
     user.setPwd(rs.getString( "pwd" )); 
     user.setSex(rs.getInt( "sex" )); 
       
     list.add(user); 
   
   catch  (Exception e) { 
    e.printStackTrace(); 
   finally
    JDBCManager.closeAll(conn, ps, rs); 
   }   
   return  list; 
  }  
  /** 
   * 查询一条记录 
   */
  public  User findUser(Integer id){ 
   User user =  null
   conn = JDBCManager.getConnection(); 
   String sql =  "select * from t_user where id=?"
   try 
    ps = conn.prepareStatement(sql); 
    ps.setInt( 1 , id); 
    rs = ps.executeQuery(); 
    while (rs.next()){ 
     user =  new  User(); 
     user.setAge(rs.getInt( "age" )); 
     user.setId(rs.getInt( "id" )); 
     user.setName(rs.getString( "name" )); 
     user.setPwd(rs.getString( "pwd" )); 
     user.setSex(rs.getInt( "sex" )); 
   
   catch  (Exception e) { 
    e.printStackTrace(); 
   finally
    JDBCManager.closeAll(conn, ps, rs); 
  
   return  user; 
 
}

 

5、将该类提取接口UserDao.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package  com.hyw.dao; 
import  java.util.List; 
import  com.hyw.entity.User; 
   
public  interface  UserDao { 
  public  void  saveUser(User user); 
    
  public  void  updateUser(User user); 
    
  public  void  removeUser(String ids); 
    
  public  List<User> findUsers(); 
    
  public  List<User> findUsers(User user); 
    
  public  User findUser(Integer id); 
}

 

6、书写业务层UserServiceImp.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
package  com.hyw.service.imp; 
import  java.util.List; 
import  com.hyw.dao.UserDao; 
import  com.hyw.dao.imp.UserDaoImp; 
import  com.hyw.entity.User; 
import  com.hyw.service.UserService; 
   
public  class  UserSeviceImp  implements  UserService { 
  private  UserDao userDao =  new  UserDaoImp(); 
    
  public  void  saveUser(User user){ 
   
   userDao.saveUser(user); 
 
    
  /** 
   * 修改用户 
   */
  public  void  updateUser(User user){ 
   
   userDao.updateUser(user); 
 
    
  /** 
   * 删除用户 
   */
  public  void  removeUser(String ids){   
   
   userDao.removeUser(ids); 
 
    
  /** 
   * 查询所有的用户记录 
   */
  public  List<User> findUsers(){ 
     
   return  userDao.findUsers(); 
 
    
  public  List<User> findUsers(User user){ 
     
   return  userDao.findUsers(user); 
 
    
  /** 
   * 查询一条记录 
   */
  public  User findUser(Integer id){ 
     
   return  userDao.findUser(id); 
 
}

 

7、将该业务类提取接口UserService.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package  com.hyw.service; 
import  java.util.List; 
import  com.hyw.entity.User; 
   
public  interface  UserService { 
  public  void  saveUser(User user); 
    
  public  void  updateUser(User user); 
    
  public  void  removeUser(String ids); 
    
  public  List<User> findUsers(); 
    
  public  User findUser(Integer id); 
    
  public  List<User> findUsers(User user); 
}

 

8、编写控制器UserAction.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
package  com.hyw.action; 
import  java.util.List; 
import  com.hyw.entity.User; 
import  com.hyw.service.UserService; 
import  com.hyw.service.imp.UserSeviceImp; 
import  com.opensymphony.xwork2.ModelDriven; 
   
public  class  UserAction  implements  ModelDriven{ 
  private  UserService userService =  new  UserSeviceImp(); 
  private  User user =  new  User(); 
  private  List<User> users;   //查询所有的用户信息 
  private  String ids;   //得到多个id用","隔开 
  private  String userId;  //得到一个id 
  private  String msg;   //jsp页面的提示消息 
    
  public  String list(){ 
   users = userService.findUsers(); 
   return  "list"
 
    
  public  String query(){ 
   users = userService.findUsers(user); 
     
   return  "list"
 
    
  public  String show(){ 
   if ( null  != userId && ! "" .equals(userId)){ 
    user = userService.findUser( new  Integer(userId)); 
  
   return  "show"
 
    
  public  String edit(){ 
   if ( null  != userId && ! "" .equals(userId)){ 
    user = userService.findUser( new  Integer(userId)); 
  
   return  "edit"
 
    
  public  String save(){ 
   if ( null  != user.getId()){ 
    userService.updateUser(user); 
    setMsg( "修改用户成功!" ); 
   } else
    userService.saveUser(user);    
    setMsg( "新增用户成功!" ); 
  
   return  list(); 
 
    
  public  String remove(){ 
   userService.removeUser(ids); 
     
   setMsg( "删除用户成功!" ); 
   return  list(); 
 
   
  public  Object getModel() { 
   
   return  user; 
 
   
  public  User getUser() { 
   return  user; 
 
   
  public  void  setUser(User user) { 
   this .user = user; 
 
   
  public  List<User> getUsers() { 
   return  users; 
 
   
  public  void  setUsers(List<User> users) { 
   this .users = users; 
 
   
  public  String getIds() { 
   return  ids; 
 
   
  public  void  setIds(String ids) { 
   this .ids = ids; 
 
   
  public  String getMsg() { 
   return  msg; 
 
   
  public  void  setMsg(String msg) { 
   this .msg = msg; 
 
   
  public  String getUserId() { 
   return  userId; 
 
   
  public  void  setUserId(String userId) { 
   this .userId = userId; 
  }  
}

 

9、方法已处理完毕,现在开始视图层操作,修改index.jsp页面

1
2
3
4
5
6
7
8
9
10
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
   
< html
< body
    < p
     < button  onclick = "window.navigate('user_list.action');"  > 用户列表</ button >  
    </ p
   </ body
</ html >

 

10、列表展示页面user_list.jsp

 

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
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
   
< html
< body  onload = "if('${msg }') alert('${msg }'); "
   < form  action = "user_query.action"  method = "post"
   < table  cellpadding = "0"  cellspacing = "0"  border = "1"  width = "80%"
     < tbody >    
      < tr
       < td >用户名:</ td
       < td >< input  type = "text"  name = "name"  value = "${user.name}" /></ td
       < td >< input  type = "submit"  value = "查&nbsp;询"  /></ td
       < td >< input  type = "button"  value = "取&nbsp;消"  onclick = "window.navigate('user_list.action');" /> </ td
      </ tr >    
     </ tbody
    </ table
    </ form
    < p
     < button  onclick = "window.navigate('user_edit.action');" >增&nbsp;加</ button >  &nbsp;&nbsp;  
     < button   onclick = "window.navigate('user_edit.action?userId='+document.getElementById('ids').value);" >修&nbsp;改</ button > &nbsp;&nbsp;  
     < button  onclick = "document.getElementById('myform').submit();" >删&nbsp;除</ button
    </ p
    < form  action = "user_remove.action"  method = "post"  id = "myform"
    < table  cellpadding = "0"  cellspacing = "0"  border = "1"  width = "80%"
     < thead
      < tr
          < td >操作</ td
       < td >用户名</ td
       < td >性别</ td
       < td >年龄</ td
      </ tr
     </ thead
     < tbody
     < s:iterator  value = "users"
      < tr
       < td >< input  type = "checkbox"  name = "ids"  id = "ids"  value = "${id}" /></ td
       < td >< a  href = "user_show.action?userId=${id}"  >${name}</ a ></ td
       < td >${sex eq 1?'男':'女'}</ td
       < td >${age }</ td
      </ tr
     </ s:iterator
   
     </ tbody
    </ table
    </ form >  
   </ body
</ html >

 

11、单击“新增”按钮,新增user_edit.jsp页面

 

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
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
   
< html
   < body
   < form  action = "user_save.action"  namespace = "user"  method = "post"
   < s:hidden  name = "user.id"  /> 
    < table  cellpadding = "0"  cellspacing = "0"  border = "1"  width = "80%"
     < tbody
      < tr
       < td >用户名 :</ td
       < td >< input  name = "user.name"  value = "${user.name eq null?'':user.name }"  ></ td
       < td >年龄</ td
       < td >< input  name = "user.age"  value = "${user.age eq null?'':user.age }" /></ td
      </ tr
      < tr
       < td >密码 :</ td
       < td >< input  type = "password"  name = "user.pwd"  value = "${user.pwd eq null?'':user.pwd }"  /></ td
       < td >性别</ td
       < td >< input  type = "radio"  name = "user.sex"  value = "1"  ${user.sex eq 1 or user.sex eq null?'checked':'' }/> 男< input  type = "radio"  name = "user.sex"  value = "0"  ${user.sex eq 0?'checked':'' }/> 女</ td
      </ tr
      < tr
       < td  colspan = "4"  align = "center"
        < input  type = "submit"  value = "保&nbsp;存" /> 
        < input  type = "button"  value = "返&nbsp;回"  onclick = "window.navigate('user_list.action');" />  
        </ td
      </ tr
     </ tbody
    </ table
    </ form
   </ body
</ html >

 

12、新增查看页面user_show.jsp

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
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
< html
   < body
    < table  cellpadding = "0"  cellspacing = "0"  border = "1"  width = "80%"
     < tbody
      < tr
       < td  align = "right" >用户名 :</ td
       < td >${user.name eq null?'':user.name }&nbsp;</ td
       < td  align = "right" >年龄:</ td
       < td >${user.age eq null?'':user.age }&nbsp;</ td
      </ tr
      < tr
       < td  align = "right" >密码 :</ td
       < td >${user.pwd eq null?'':user.pwd }&nbsp;</ td
       < td  align = "right" >性别:</ td
       < td >${user.sex eq 1 or user.sex eq null?'男':'女' }&nbsp;</ td
      </ tr
      < tr
       < td  colspan = "4"  align = "center"
        < input  type = "button"  value = "返&nbsp;回"  onclick = "window.navigate('user_list.action');" />  
        </ td
      </ tr
     </ tbody
    </ table
   </ body
</ html >

 

到目前为止,对该用户的增删改查操作都已经处理完毕。

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值