SSH框架整合(用户登录)
整个项目图解(实际项目用了四个实例分别完成三部分内容):
第一部分:保留三框架自己的配置文件不使用注解
1.准备数据库文件
本次整合使用的是mysql数据库,这里使用一张用户表,表名为t_user,t_user表中有id,username,password这三个字段。
SQL语句如下:
create table t_user(
id int(11) not null auto_increment,
usernamevarchar(255) default null,
password varchar(255) default null,
primary key(id)
);
2,配置Struts环境
SSH框架的整合其实是在项目中分别配置每个框架的环境,然后通过修改配置文件进行整合,首先配置Struts环境。
1.导入Struts的相关jar包,本次使用的Struts-2.3.24版本。导入的jar包共13个。分别是:
2.添加log4j.properties
项目中需要添加一个log4j.properties文件,作为log4j日志技术的配置文件。在添加log4j.properties文件之前,首先在项目中创建一个名为config的Source Folder,用于存放配置文件。然后在config中创建一个名为log4j.properties的文件,编写log4j.properties文件,具体内容如下:
#指定logger # 配置Logger组件 # 设定Log4j的日志级别(error warn info debug) # 输出目的地(Console, logfile是appender的名字,是自己定义的,后面才赋予具体的含义) log4j.rootLogger=debug, Console, logfile ################################################################################################################### #指定appender(目的地) #设定Logger的Console(appender的名字)的Appender类型为控制台输出 #org.apache.log4j.ConsoleAppender 表明 Console是控制台输出 log4j.appender.Console=org.apache.log4j.ConsoleAppender
#输出的格式 #设定Console的Appender布局Layout #org.apache.log4j.PatternLayout(可以灵活地指定布局模式)包含选项: # ConversionPattern=%m%n :指定怎样格式化指定的消息 log4j.appender.Console.layout=org.apache.log4j.PatternLayout #2009-09-29 07:30:43,265 INFO com.itcast.web.controller.SearchCdServlet.doGet() - e log4j.appender.Console.layout.ConversionPattern=%d %p %c.%M() - %m%n
################################################################################################################### #设定Logger的logfile(appender的名字)的Appender类型为文件大小到达指定尺寸的时候产生一个新的文件 log4j.appender.logfile=org.apache.log4j.RollingFileAppender
#设定文件的输出路径(指定文件的名字和路径,tomcat的启动路径) log4j.appender.logfile.File=../logs/logstrore.log
#设定后缀可以是KB, MB 或者是 GB. 在日志文件到达该大小时,将会自动滚动,即将原来的内容移到logstrore.log.1文件 log4j.appender.logfile.MaxFileSize=2048KB
#Keep three backup files.指定可以产生的滚动文件的最大数 log4j.appender.logfile.MaxBackupIndex=4
#设定logfile的Appender布局Layout log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p %c.%M() - %m%n #####################################################################
|
3.在web.xml配置Struts核心过滤器
配置文件如下:
xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
|
4.添加struts.xml配置文件
在config路径下添加struts配置文件struts.xml。配置如下:
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
|
3.配置spring环境
1.导入spring的依赖jar包
本系统使用的是Spring 3.2.2版本依赖的基础jar包共14个,具体如下:
2.核心配置文件applicationContext.xml
配置文件如下:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
|
3.在web.xml中配置spring的监听器
配置文件如下:
xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
|
4.配置hibernate环境
Struts和Spring的环境配置完毕后,接下来,对hibernate的环境进行配置。
1.hibernate项目所依赖的jar包需要11个,如下:
2.添加核心配置文件hibernate.cfg.xml:
配置文件如下:
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
org.hibernate.dialect.MySQL5Dialect
|
5.进行spring和hibernate的整合:
hibernate.cfg.xml文件是Hibernate中重要的配置文件,当Spring和Hibernate整合时,由于hibernate.cfg.xml文件中的配置信息可以交予Spring来管理,所以可以选择是否使用hibernage.cfg.xml文件。本文章将对使用hibernate.cfg.xm文件和不使用hibernate.cfg.xml文件两种情况来讲解Spring和Hibernate的整合。
Spring和Hibernate的整合,需要添加一个支持整合的jar包,srping-orm-3.2.2.RELEASE.jar。
1.首先建立实体类,新建entity包,新建User类,源码如下:
package entity;
public class User { private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; }
} |
2.在此包下新建User.hbm.xml,源代码如下:
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
此时,User.hbm.xml建立好后,需要在hibernate的核心配置文件中hibernate,cfg.xml中添加这个映射文件,源码入下:
|
3.dao层的编码,新建dao包,新建UserDao接口类,源码如下:
package dao; import java.util.List; import entity.User; public interface UserDao { public void save(User user); public void update(User user); public void delete(User user); public User findById(Integer id); public User findUser(String username,String password); public List } |
4.实现类的编码,新建dao.impl包,新建UserDaoImpl实现类,源码如下:
package dao.impl; import java.util.List; import org.springframework.orm.hibernate3.HibernateTemplate; import dao.UserDao; import entity.User; public class UserDaoImpl implements UserDao{ private HibernateTemplate hibernateTemplate; public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } public void save(User user) { this.hibernateTemplate.save(user); System.out.println("-----------------------ddd"); } public void update(User user) { hibernateTemplate.update(user); } public void delete(User user) { hibernateTemplate.delete(user); } public User findById(Integer id) { return hibernateTemplate.get(User.class, id); } public User findUser(String username, String password) { return null; } @SuppressWarnings("unchecked") public List return hibernateTemplate.find("from User"); } } |
5.service层的接口,新建接口类UserService,源码如下:
package service;
import java.util.List;
import entity.User;
public interface UserService { public void save(User user); public void update(User user); public void delete(User user); public User findById(Integer id); public List } |
6.service实现类,新建实现类UserServiceImpl,源码如下:
package service.impl;
import java.util.List;
import dao.UserDao; import entity.User; import service.UserService;
public class UserServiceImpl implements UserService{ private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save(User user) { this.userDao.save(user); }
public void update(User user) { userDao.update(user); }
public void delete(User user) { userDao.delete(user); }
public User findById(Integer id) { return userDao.findById(id); } public List return userDao.findAll(); } } |
7.spring配置文件applicationContext.xml的配置:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
|
9.测试Spring和Hibernate的整合是否成功。新建test包,新建TestApp测试类,源码如下:
package test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import service.UserService; import entity.User;
public class TestApp { @Test public void demo1() throws Exception { User user=new User(); user.setUsername("jack"); user.setPassword("1234"); String config="classpath:applicationContext.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(config); UserService userService=ac.getBean("userService",UserService.class); userService.save(user); System.out.println("操作成功!"); } } |
此时,如果程序控制台没有报错并且数据库中存入了相应的成功,即Spring和Hibernate整合完成。
6.进行Spring与Struts的整合:
1.在Spring和Struts整合时,需要导入一个jar包,jar包名称为:
struts2-spring-plugin-2.3.24.jar,此文件在struts文件包中可以找到。
2.创建action类:新建action包,新建UserAction类,源码如下:
package action; import service.UserService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import entity.User; public class UserAction extends ActionSupport implements ModelDriven private static final long serialVersionUID = -707433412533727588L; private User user=new User(); public User getModel() { return user; } private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } public String add(){ userService.save(user); return "add"; } } |
3.在applicationContext中配置Struts.
增加的配置信息如下:
|
4.struts.xml配置,源码如下:
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
|
5.添加测试页面,登录页面index.jsp,源码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
method="post"> 用户名: 密 码:
|
6.登录成功页面,success.jsp,源码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
添加成功!
|
至此,本部分整合完毕,将程序发布到tomcat,在浏览器输入后访问,结果页如下:
第二部分:不使用hibernate.cfg.xml
1.使用HibernateDaoSupport(需要修改UserDaoImpl中的代码),不再使用HibernateTemplate,修改UserDaoImpl的源码为:
package dao.impl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import dao.UserDao; import entity.User; public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public void save(User user) { this.getHibernateTemplate().save(user);
}
public void update(User user) { this.getHibernateTemplate().update(user); }
public void delete(User user) { this.getHibernateTemplate().delete(user); }
public User findById(Integer id) { return this.getHibernateTemplate().get(User.class, id); }
public User findUser(String username, String password) { return null; }
@SuppressWarnings("unchecked") public List return this.getHibernateTemplate().find("from User"); }
} |
本代码中,UserDaoImpl类继承了HibernateDaoSupport类,删除了原来使用HibernateTemplate的代码。
2.创建c3p0-db.properties文件:
在config目录中创建c3p0-db.properties配置文件,将连接数据库的基本4项编写到c3p0-db.properties文件中,源码如下:
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///test jdbc.user=root jdbc.password=root |
3.修改Spring中applicationContext.xml,修改后的源码如下:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
|
4.此时可以删除掉Hibernate.cfg.xml文件,打开test包中testApp进行测试。
第三部分:Struts不整合到Spring,自身创建Action
Action的创建工作是交予Spring框架来完成的,如果不想这样,删除掉Spring配置的关于对userAction的配置代码,同时修改Struts配置文件struts.xml,修改后的源代码为:
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
|
第四部分:使用注解形式实现三框架整合
[本部分代码使用了c3p0整合部分和保留三框架原来的配置文件,意在全面掌握框架整合时注解的使用。所以,这部分代码比较乱,是第一到三部分的结合体的修改,根据自身需求参考本部分代码适当修改代码即可]
1.需要导入一个注解开发包,名称为:struts2-convention-plugin-2.3.24。
该jar包在Struts的jar包中可以找到,在不使用注解的时候不要导入此包。
2.修改实体类,可以删除掉user.hbm.xml源代码如下:
package entity;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table;
@Entity @Table(name="t_user") public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; @Column(name="username",length=50) private String username; //不配置,使用默认值 private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; }
} |
3.修改Dao层中UserDaoImpl,源代码如下:
package dao.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Repository;
import dao.UserDao; import entity.User; @Repository public class UserDaoImpl implements UserDao { @Autowired private HibernateTemplate hibernateTemplate; public void save(User user) { this.hibernateTemplate.save(user);
}
public void update(User user) { this.hibernateTemplate.update(user); }
public void delete(User user) { this.hibernateTemplate .delete(user); }
public User findById(Integer id) { return this.hibernateTemplate .get(User.class, id); }
public User findUser(String username, String password) { return null; }
@SuppressWarnings("unchecked") public List return this.hibernateTemplate .find("from User"); }
} |
4.修改Service层中UserServiceImpl,源代码如下:
package service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
import dao.UserDao; import entity.User; import service.UserService; @Service public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao; @Transactional public void save(User user) { this.userDao.save(user); } @Transactional public void update(User user) { userDao.update(user); } @Transactional public void delete(User user) { userDao.delete(user); } @Transactional public User findById(Integer id) { return userDao.findById(id); }
@Transactional public User findUser(String username, String password) { return null; }
@Transactional(readOnly=true) public List return userDao.findAll(); }
} |
5. 修改aciton中UserAction类,源代码如下:
package action;
import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;
import service.UserService;
import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven;
import entity.User;
@Namespace("/") @ParentPackage("struts-default") @Controller public class UserAction extends ActionSupport implements ModelDriven private static final long serialVersionUID = -707433412533727588L; private User user=new User(); public User getModel() { return user; } @Autowired private UserService userService; @Action(value="userAction_add",results={@Result(name="add",location="/success.jsp")}) public String add(){ userService.save(user); return "add"; } } |
6.修改applicationContext.xml,源文件如下:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
|
7.修改hibernate.cfg.xml文件,源代码如下:
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
org.hibernate.dialect.MySQL5Dialect
org.hibernate.connection.C3P0ConnectionProvider
|
8.发布应用,在浏览器中输入地址进行访问测试。
以上内容请不要复制黏贴的形式新建项目,太容易出错了。如果复制黏贴的形式,至少要了解三框架一些基本内容,报错了有能力去解决 。
☆:其他SSH框架整合资料(比我的详细美观多了):
1.https://www.cnblogs.com/tuhooo/p/6491906.html
2.http://blog.csdn.net/jstxzhangrui/article/details/52825873
3.https://www.cnblogs.com/zhaowancheng/p/5841591.html
源代码和同此内容的对应文档下载地址:http://download.csdn.net/download/qq_25337221/10115306
本内容由安康学院”雨季”原创!