将商城项目改为struts2和hibernate,并增加管理员页面
对注册用户的操作:
1.model层:
View Code
1 package com.shopping.model; 2 3 import java.sql.Timestamp; 4 5 import javax.persistence.Entity; 6 import javax.persistence.GeneratedValue; 7 import javax.persistence.Id; 8 9 @Entity(name="ruser") 10 public class User { 11 private int id; 12 private String username; 13 private String password; 14 private String phone; 15 private String address; 16 private Timestamp rdate; 17 18 @Id 19 @GeneratedValue 20 public int getId() { 21 return id; 22 } 23 public void setId(int id) { 24 this.id = id; 25 } 26 public String getUsername() { 27 return username; 28 } 29 public void setUsername(String username) { 30 this.username = username; 31 } 32 public String getPassword() { 33 return password; 34 } 35 public void setPassword(String password) { 36 this.password = password; 37 } 38 public String getPhone() { 39 return phone; 40 } 41 public void setPhone(String phone) { 42 this.phone = phone; 43 } 44 public String getAddress() { 45 return address; 46 } 47 public void setAddress(String address) { 48 this.address = address; 49 } 50 public Timestamp getRdate() { 51 return rdate; 52 } 53 public void setRdate(Timestamp rdate) { 54 this.rdate = rdate; 55 } 56 57 58 }
2.DAO层:
View Code
1 package com.shopping.dao; 2 3 import java.util.List; 4 5 import com.shopping.model.User; 6 7 public interface UserDAO { 8 9 public boolean save(User u); 10 11 public List<User> getUsers(); 12 13 public void deleteById(int id); 14 15 }
3.DAOImpl:
View Code
1 package com.shopping.dao.impl; 2 3 import java.util.List; 4 5 import org.hibernate.HibernateException; 6 import org.hibernate.Query; 7 import org.hibernate.Session; 8 9 import com.shopping.dao.UserDAO; 10 import com.shopping.model.User; 11 import com.shopping.util.HibernateUtil; 12 13 public class UserDAOImpl implements UserDAO{ 14 15 Session s = null; 16 17 private void begin(){ 18 if(null != s){ 19 s.beginTransaction(); 20 } 21 } 22 23 private void commit(){ 24 if(null != s){ 25 s.getTransaction().commit(); 26 } 27 } 28 29 @Override 30 public boolean save(User u) { 31 s = HibernateUtil.getSession(); 32 this.begin(); 33 try { 34 s.save(u); 35 } catch (HibernateException e) { 36 e.printStackTrace(); 37 return false; 38 } 39 this.commit(); 40 return true; 41 } 42 43 @Override 44 public List<User> getUsers() { 45 s = HibernateUtil.getSession(); 46 this.begin(); 47 Query q = s.createQuery("from ruser"); 48 List<User> users = (List<User>)q.list(); 49 this.commit(); 50 return users; 51 } 52 53 @Override 54 public void deleteById(int id) { 55 s = HibernateUtil.getSession(); 56 this.begin(); 57 User u = new User(); 58 u.setId(id); 59 s.delete(u); 60 this.commit(); 61 } 62 63 }
4.Service层:
View Code
1 package com.shopping.service; 2 3 import java.util.List; 4 5 import com.shopping.dao.UserDAO; 6 import com.shopping.dao.impl.UserDAOImpl; 7 import com.shopping.model.User; 8 9 public class UserService { 10 11 UserDAO userDAO; 12 13 public UserService() { 14 userDAO = new UserDAOImpl(); 15 } 16 17 public boolean addUser(User u){ 18 return userDAO.save(u); 19 } 20 21 public void delUser(int id){ 22 userDAO.deleteById(id); 23 } 24 25 public List<User> getUsers(){ 26 return userDAO.getUsers(); 27 } 28 }
5.Action层:
View Code
1 package com.shopping.action; 2 3 import java.sql.Timestamp; 4 import java.util.List; 5 6 import com.opensymphony.xwork2.ActionSupport; 7 import com.shopping.model.User; 8 import com.shopping.service.UserService; 9 import com.shopping.util.DBUtil; 10 11 public class UserAction extends ActionSupport{ 12 13 private UserService userService; 14 15 private int id; 16 private String username; 17 private String password; 18 private String phone; 19 private String address; 20 private Timestamp rdate; 21 22 private List<User> users = null; 23 24 public void setUsers(List<User> users) { 25 this.users = users; 26 } 27 public List<User> getUsers() { 28 return users; 29 } 30 31 32 public int getId() { 33 return id; 34 } 35 public void setId(int id) { 36 this.id = id; 37 } 38 public String getUsername() { 39 return username; 40 } 41 public void setUsername(String username) { 42 this.username = username; 43 } 44 public String getPassword() { 45 return password; 46 } 47 public void setPassword(String password) { 48 this.password = password; 49 } 50 public String getPhone() { 51 return phone; 52 } 53 public void setPhone(String phone) { 54 this.phone = phone; 55 } 56 public String getAddress() { 57 return address; 58 } 59 public void setAddress(String address) { 60 this.address = address; 61 } 62 public Timestamp getRdate() { 63 return rdate; 64 } 65 public void setRdate(Timestamp rdate) { 66 this.rdate = rdate; 67 } 68 69 public void initUserService(){ 70 userService = new UserService(); 71 } 72 73 public String getAllUsers(){ 74 initUserService(); 75 users = userService.getUsers(); 76 if(null != users){ 77 return "getSuccess"; 78 } 79 80 return ERROR; 81 } 82 83 public String add() throws Exception { 84 85 initUserService(); 86 User u = new User(); 87 u.setUsername(username); 88 u.setPassword(password); 89 u.setAddress(address); 90 u.setPhone(phone); 91 u.setRdate(new Timestamp(System.currentTimeMillis())); 92 93 boolean flag = userService.addUser(u); 94 if(!flag){ 95 return "adderror"; 96 } 97 98 return "addsuccess"; 99 } 100 101 public String delete() throws Exception { 102 103 initUserService(); 104 105 106 userService.delUser(id); 107 108 return "delsuccess"; 109 } 110 }
6.写一个Hibernate辅助类,用于得到Session:
View Code
1 package com.shopping.util; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.AnnotationConfiguration; 6 7 public class HibernateUtil { 8 9 private static SessionFactory sf = null; 10 11 public static Session getSession(){ 12 sf = new AnnotationConfiguration().configure().buildSessionFactory(); 13 14 return sf.getCurrentSession(); 15 } 16 17 }
7.列出用户列表(带用户删除功能)jsp:
View Code
1 <%@ page language="java" contentType="text/html; charset=GB18030" 2 pageEncoding="GB18030"%> 3 <%@ taglib uri="/struts-tags" prefix="s" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 8 <title>用户列表</title> 9 </head> 10 <body> 11 12 <% 13 String path=request.getContextPath(); 14 String rpath=request.getScheme()+"://" + request.getServerName()+":" + 15 request.getServerPort() + path + "/"; 16 %> 17 18 <table border="1"> 19 20 <tr> 21 <td>ID</td> 22 <td>用户名</td> 23 <td>密码</td> 24 <td>电话</td> 25 <td>地址</td> 26 <td>注册日期</td> 27 <td>删除</td> 28 </tr> 29 30 <s:iterator value="users"> 31 <tr> 32 <td><s:property value="id"/></td> 33 <td><s:property value="username"/></td> 34 <td><s:property value="password"/></td> 35 <td><s:property value="phone"/></td> 36 <td><s:property value="address"/></td> 37 <td><s:property value="rdate"/></td> 38 <td><a href="<%=rpath %>delete!delete?id=<s:property value="id"/>">删除</a></td> 39 </tr> 40 </s:iterator> 41 42 </table> 43 <s:debug></s:debug> 44 </body> 45 </html>
8.struts2配置文件:
View Code
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <!-- 8 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 9 <constant name="struts.devMode" value="false" /> 10 11 <package name="default" namespace="/" extends="struts-default"> 12 13 <default-action-ref name="index" /> 14 15 <global-results> 16 <result name="error">/error.jsp</result> 17 </global-results> 18 19 <global-exception-mappings> 20 <exception-mapping exception="java.lang.Exception" result="error"/> 21 </global-exception-mappings> 22 23 <action name="index"> 24 <result type="redirectAction"> 25 <param name="actionName">HelloWorld</param> 26 <param name="namespace">/example</param> 27 </result> 28 </action> 29 </package> 30 31 <include file="example.xml"/> 32 --> 33 <!-- Add packages here --> 34 35 <constant name="struts.devMode" value="true" /> <!-- 开发模式 --> 36 <constant name="struts.i18n.encoding" value="gbk"/> <!-- 解决中文乱码 --> 37 <package name="register" namespace="/" extends="struts-default"> 38 39 <action name="register" class="com.shopping.action.UserAction" method="add"> 40 <result name="addsuccess"> 41 /registerOK.jsp 42 </result> 43 <result name="adderror"> 44 /registerFail.jsp 45 </result> 46 </action> 47 48 <action name="delete" class="com.shopping.action.UserAction" method="delete"> 49 <result name="delsuccess"> 50 /admin/userlist.jsp 51 </result> 52 <result name="delerror"> 53 /registerFail.jsp 54 </result> 55 </action> 56 57 <action name="list" class="com.shopping.action.UserAction" method="getUsers"> 58 <result name="getSuccess"> 59 /admin/userlist.jsp 60 </result> 61 <result name="error"> 62 /registerFail.jsp 63 </result> 64 </action> 65 </package> 66 </struts>
9.Hibernate配置文件:
View Code
1 <?xml version='1.0' encoding='utf-8'?> 2 <!-- 3 ~ Hibernate, Relational Persistence for Idiomatic Java 4 ~ 5 ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as 6 ~ indicated by the @author tags or express copyright attribution 7 ~ statements applied by the authors. All third-party contributions are 8 ~ distributed under license by Red Hat Inc. 9 ~ 10 ~ This copyrighted material is made available to anyone wishing to use, modify, 11 ~ copy, or redistribute it subject to the terms and conditions of the GNU 12 ~ Lesser General Public License, as published by the Free Software Foundation. 13 ~ 14 ~ This program is distributed in the hope that it will be useful, 15 ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 ~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 17 ~ for more details. 18 ~ 19 ~ You should have received a copy of the GNU Lesser General Public License 20 ~ along with this distribution; if not, write to: 21 ~ Free Software Foundation, Inc. 22 ~ 51 Franklin Street, Fifth Floor 23 ~ Boston, MA 02110-1301 USA 24 --> 25 <!DOCTYPE hibernate-configuration PUBLIC 26 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 27 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 28 29 <hibernate-configuration> 30 31 <session-factory> 32 33 <!-- Database connection settings --> 34 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 35 <property name="connection.url">jdbc:mysql://localhost:3306/shopping</property> 36 <property name="connection.username">root</property> 37 <property name="connection.password">1234</property> 38 39 <!-- JDBC connection pool (use the built-in) --> 40 <!-- 41 <property name="connection.pool_size">1</property> 42 --> 43 <!-- SQL dialect --> 44 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 45 46 <property name="current_session_context_class">thread</property> 47 48 <!-- Disable the second-level cache --> 49 <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 50 51 <!-- Echo all executed SQL to stdout 把sql语句输出出来--> 52 <property name="show_sql">true</property> 53 54 <!-- 把sql语句更漂亮的输出出来--> 55 <property name="format_sql">true</property> 56 57 <!-- Drop and re-create the database schema on startup --> 58 <!-- 表示如果表存在则直接操作,若表不存在则创建表后再插入 --> 59 <property name="hbm2ddl.auto">update</property> 60 61 <!-- 用hbm来使用student类 62 <mapping resource="com/hibernate/model/Student.hbm.xml"/> 63 --> 64 65 <!-- 用annotation来使用teacher类 66 <mapping class="com.hibernate.model.Teacher"/> 67 --> 68 69 <!-- 70 <mapping class="com.hibernate.model.hql.Category"/> 71 <mapping class="com.hibernate.model.hql.Topic"/> 72 <mapping class="com.hibernate.model.hql.Msg"/> 73 --> 74 <mapping class="com.shopping.model.User"/> 75 </session-factory> 76 77 </hibernate-configuration>
管理员界面目前只有显示用户列表功能也就是功能7.
PS:应当在userlist中的删除连接里写上 onclick="return window.confirm('你确定要删除吗?')" .....按下确定才真正删除!

浙公网安备 33010602011771号