SSH框架整合实现信息管理系统
SSH框架整合实现信息管理系统
视频讲解 项目结构
web.xml
<? xml version="1.0" encoding="UTF-8" ?> < web-app version ="2.5" xmlns ="http://java.sun.com/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" > <!-- 首页 --> < welcome-file-list > < welcome-file > index.jsp </ welcome-file > </ welcome-file-list > <!-- Struts过滤转发器 --> < filter > < filter-name > struts2 </ filter-name > < filter-class > org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </ 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 > </ web-app >
JSP 页面
登入页面( index.jsp )
<% @ page pageEncoding = " GBK " %> < h1 > 管理员登录 </ h1 > (测试账号密码都是admin) < form action ="UserAction_login" method ="post" > 账号 < input type ="text" name ="user.name" >< br > 密码 < input type ="password" name ="user.pswd" >< br > < input type ="submit" value ="登录" > </ form >
列表页面( list.jsp )
<% @taglib prefix = " s " uri = " /struts-tags " %> < table border ="1" > < tr > < th > ID </ th >< th > name </ th >< th > pswd </ th >< th > update </ th >< th > delete </ th > </ tr > < s:iterator value ="#request.list" > < tr > < td >< s:property value ="id" /></ td > < td >< s:a href ="UserAction_detail?user.id=%{id}" >< s:property value ="name" /></ s:a ></ td > < td >< s:property value ="pswd" /></ td > < td >< s:a href ="UserAction_toUpdate?user.id=%{id}" > update </ s:a ></ td > < td >< s:a href ="UserAction_delete?user.id=%{id}" > delete </ s:a ></ td > </ tr > </ s:iterator > </ table > < a href ="add.jsp" > add </ a >
添加页面( add.jsp )
< form action ="UserAction_add" method ="post" > name < input name ="user.name" type ="text" >< br > pswd < input name ="user.pswd" type ="password" >< br > < input type ="submit" value ="add" > </ form >
详情页面( detail.jsp )
<% @taglib prefix = " s " uri = " /struts-tags " %> id: < s:property value ="user.id" />< br /> name: < s:property value ="user.name" />< br /> pswd: < s:property value ="user.pswd" />
更新页面( update.jsp )
<% @taglib prefix = " s " uri = " /struts-tags " %> < form action ="UserAction_update" method ="post" > < s:hidden name ="user.id" ></ s:hidden > name: < input name ="user.name" type ="text" value ="<s:property value=" user.name" /> "> < br > pswd: < input name ="user.pswd" type ="password" >< br > < input type ="submit" value ="modify" > </ form >
控制层( Action )
Struts.xml 配置 Action
<? xml version="1.0" encoding="UTF-8" ?> <! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd" > < struts > < constant name ="struts.devMode" value ="true" /> < constant name ="struts.i18n.encoding" value ="GBK" ></ constant > < package name ="user" extends ="struts-default" > < action name ="UserAction_*" class ="userAction" method ="{1}" > <!-- *匹配任意字符,{1}指第一个星号匹配到的字符 --> <!-- 重定向到UserAction执行list方法 --> < result name ="success" type ="redirectAction" > UserAction_list </ result > <!-- 返回结果相应页面 --> < result name ="login" > /index.jsp </ result > < result name ="list" > /list.jsp </ result > < result name ="detail" > /detail.jsp </ result > < result name ="update" > /update.jsp </ result > </ action > </ package > </ struts >
UserAction.java
1 package com.lyltim.action; 2 3 import javax.annotation.Resource; 4 5 import org.apache.struts2.ServletActionContext; 6 import org.springframework.stereotype.Controller; 7 8 import com.lyltim.bean.User; 9 import com.lyltim.service.IUserService; 10 import com.opensymphony.xwork2.ActionSupport; 11 12 @Controller("userAction" ) 13 public class UserAction extends ActionSupport { 14 15 private User user; 16 17 // 注入userService 18 @Resource 19 IUserService userService; 20 21 public void setUserService(IUserService userService) { 22 this .userService = userService; 23 } 24 public User getUser() { 25 return user; 26 } 27 public void setUser(User user) { 28 this .user = user; 29 } 30 31 public String login() { 32 if (userService.checkLogin(user)) 33 return SUCCESS; 34 else 35 return LOGIN; 36 } 37 38 public String list() { 39 ServletActionContext.getRequest().setAttribute("list", userService.list()); 40 return "list" ; 41 } 42 43 public String add() { 44 userService.add(user); 45 return SUCCESS; 46 } 47 48 public String detail() { 49 user = userService.getUser(user.getId()); 50 return "detail" ; 51 } 52 53 // 先获得要更新的用户的信息,显示到更新页面。 54 public String toUpdate() { 55 user = userService.getUser(user.getId()); 56 return "update" ; 57 } 58 59 // 更新用户信息 60 public String update() { 61 userService.update(user); 62 return SUCCESS; 63 } 64 65 public String delete() { 66 userService.delete(user.getId()); 67 return SUCCESS; 68 } 69 70 }
实体层( Bean )
User.java
1 package com.lyltim.bean; 2 public class User { 3 private int id; 4 private String name, pswd; 5 // Getters and Setters... 6 }
User.hbm.xml 实体映射配置文件
<? xml version="1.0" ?> <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > < hibernate-mapping > < class name ="com.lyltim.bean.User" table ="user" > < id column ="id" name ="id" type ="int" > < generator class ="native" /> </ id > < property column ="name" name ="name" not-null ="true" type ="string" /> < property column ="pswd" name ="pswd" not-null ="true" type ="string" /> </ class > </ hibernate-mapping >
业务逻辑层( Service )
IUserService 接口
1 package com.lyltim.service; 2 3 import java.util.List; 4 import com.lyltim.bean.User; 5 6 public interface IUserService { 7 boolean checkLogin(User user); // 登入验证 8 void add(User user); // 添加用户 9 List<User> list(); // 列出所有用户 10 User getUser( int id); // 根据ID查找用户 11 void update(User user); // 修改用户 12 void delete( int id); // 删除用户 13 }
UserServiceImpl.java 实现类
1 package com.lyltim.service; 2 3 import java.util.List; 4 import javax.annotation.Resource; 5 import org.springframework.stereotype.Service; 6 7 import com.lyltim.bean.User; 8 import com.lyltim.dao.IUserDAO; 9 10 @Service("userService" ) 11 public class UserServiceImpl implements IUserService { 12 13 // 注入userDAO 14 @Resource 15 private IUserDAO userDAO; 16 17 public void setUserDAO(IUserDAO userDAO) { 18 this .userDAO = userDAO; 19 } 20 21 // 登入验证 22 public boolean checkLogin(User user) { 23 if ("admin".equals(user.getName()) && "admin" .equals(user.getPswd())) 24 return true ; 25 else 26 return false ; 27 } 28 29 // 添加用户 30 public void add(User user) { 31 userDAO.add(user); 32 } 33 34 // 列出所有用户 35 public List<User> list() { 36 return userDAO.list(); 37 } 38 39 // 查询某个用户 40 public User getUser( int id) { 41 return userDAO.getUser(id); 42 } 43 44 // 修改某个用户的资料 45 public void update(User user) { 46 userDAO.update(user); 47 } 48 49 // 删除某个用户 50 public void delete( int id) { 51 userDAO.delete(id); 52 } 53 54 }
数据持久层( DAO )
IUserDAO接口
1 package com.lyltim.dao; 2 3 import java.util.List; 4 import com.lyltim.bean.User; 5 6 public interface IUserDAO { 7 void add(User user); // 添加用户 8 List<User> list(); // 列出所有用户 9 User getUser( int id); // 根据ID查找用户 10 void update(User user); // 修改用户 11 void delete( int id); // 删除用户 12 }
UserDAOImpl.java 实现类
1 package com.lyltim.dao; 2 3 import java.util.List; 4 5 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 6 import org.springframework.stereotype.Repository; 7 8 import com.lyltim.bean.User; 9 10 @Repository("userDAO" ) 11 public class UserDAOImpl extends HibernateDaoSupport implements IUserDAO { 12 13 public void add(User user) { 14 getHibernateTemplate().save(user); 15 } 16 17 public List<User> list() { 18 return getHibernateTemplate().find("from User" ); 19 } 20 21 public User getUser( int id) { 22 return getHibernateTemplate().get(User. class , id); 23 } 24 25 public void update(User user) { 26 getHibernateTemplate().update(user); 27 } 28 29 public void delete( int id) { 30 getHibernateTemplate().delete(getHibernateTemplate().get(User. class , id)); 31 } 32 }
Spring 全局配置文件( applicationContext.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:context ="http://www.springframework.org/schema/context" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" > < bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method ="close" > < property name ="driverClassName" value ="com.mysql.jdbc.Driver" /> < property name ="url" value ="jdbc:mysql:///test" /> < property name ="username" value ="root" /> < property name ="password" value ="123" /> </ bean > < bean id ="sessionFactory" class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" > < property name ="dataSource" ref ="dataSource" /> < property name ="mappingResources" > < list > < value > com/lyltim/bean/User.hbm.xml </ value > </ list > </ property > < property name ="hibernateProperties" > < props > < prop key ="hibernate.dialect" > org.hibernate.dialect.MySQL5Dialect </ prop > < prop key ="hibernate.hbm2ddl.auto" > create-drop </ prop > < prop key ="hibernate.show_sql" > true </ prop > </ props > </ property > </ bean > < bean id ="userDAO" class ="com.lyltim.dao.UserDAOImpl" > < property name ="sessionFactory" ref ="sessionFactory" /> </ bean > <!-- 启用注解注入 --> < context:annotation-config /> <!-- 启用组件扫描 --> < context:component-scan base-package ="com.lyltim" /> </ beans >
分类: Java
标签: Java , JavaEE
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did45365