从大学的时候就开始用SSH(hibernate+spring+struts2)开发,但是始终没有整理出怎么集成这个框架的,最近所做的网站不是很忙,抽个时间写了出来,也是当学习怎么样搭建,温故而知新吧。集成SSH当然少不了去下载他们各自的jar包了,我的jar包列表在附件的图片中,当然有部分包是不需要的,我为了简便就全部加进来,有时间的可以慢慢调试,看看少什么包,自己在相应的加就行了。
当然这个只是一个很简单的demo。
SSH集成,我的方法是先Hibernate连接数据调试通了,在集成Spring,Hibernate连接数据库这里就不做分析了,很简单加入相应的包,配置一下就行。Hibernate集成Spring就是把hibernate的相应配置文件交给Spring处理,或者托管之类的。我的在applicationContext.xml配置文件托管配置如下:
<!-- 配置dbcp形式的数据连接池 -->
<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://localhost:3306/SSH"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 配置Hibernate托管到Spring中 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/lzb/hibernate/entity/xml/TUser.hbm.xml</value>
</list>
</property>
</bean>
上面那样基本就集成了hibernate和spring,以为hibernate和spring并不一定要在web工程中使用,所以这里没有必要配置web.xml。而要是集成SSH,一般都是用于web工程,当然就有需要配置web.xml了。
接下来就要介绍集成SSH了,集成struts和加入spring的监听器。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
这样就集成SSH了。
说到这里,我将我的demo,集成SSH模拟用户登录展示出来。
POJO
package com.lzb.hibernate.entity;
import java.util.Date;
public class TUser {
private int userID;
private String userName;
private String passWord;
private Date birthday;
// get/set
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.lzb.hibernate.entity.TUser" table="t_user">
<id name="userID" column="id">
<generator class="native"></generator>
</id>
<property name="userName" column="username"></property>
<property name="passWord" column="password"></property>
<property name="birthday" column="birthday"></property>
</class>
</hibernate-mapping>
Dao
package com.lzb.hibernate.dao;
import java.util.List;
import com.lzb.hibernate.entity.TUser;
public interface TUserDao {
public void insertUser(TUser user);
public List<TUser> findAllUsers();
}
DaoImpl
package com.lzb.hibernate.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.lzb.hibernate.dao.TUserDao;
import com.lzb.hibernate.entity.TUser;
public class TUserDaoImpl extends HibernateDaoSupport implements TUserDao {
public List<TUser> findAllUsers() {
String hql = "FROM TUser";
return this.getHibernateTemplate().find(hql);
}
public void insertUser(TUser user) {
this.getHibernateTemplate().save(user);
}
}
Service
package com.lzb.hibernate.service;
import java.util.List;
import com.lzb.hibernate.entity.TUser;
public interface TUserService {
public void insertUser(TUser user);
public List<TUser> findAllUsers();
}
ServiceImpl
package com.lzb.hibernate.service.impl;
import java.util.List;
import com.lzb.hibernate.dao.TUserDao;
import com.lzb.hibernate.dao.impl.TUserDaoImpl;
import com.lzb.hibernate.entity.TUser;
import com.lzb.hibernate.service.TUserService;
public class TUserServiceImpl implements TUserService {
private TUserDao tuserDao = new TUserDaoImpl();
public TUserDao getTuserDao() {
return tuserDao;
}
public void setTuserDao(TUserDao tuserDao) {
this.tuserDao = tuserDao;
}
public List<TUser> findAllUsers() {
// TODO Auto-generated method stub
return this.tuserDao.findAllUsers();
}
public void insertUser(TUser user) {
// TODO Auto-generated method stub
this.tuserDao.insertUser(user);
}
}
Action
package com.lzb.hibernate.action;
import java.util.List;
import com.lzb.hibernate.entity.TUser;
import com.lzb.hibernate.service.TUserService;
import com.opensymphony.xwork2.ActionSupport;
public class TUserAction extends ActionSupport {
private TUser user;
private List<TUser> userList;
private TUserService tuserServce;
public TUser getUser() {
return user;
}
public void setUser(TUser user) {
this.user = user;
}
public List<TUser> getUserList() {
return userList;
}
public void setUserList(List<TUser> userList) {
this.userList = userList;
}
public TUserService getTuserServce() {
return tuserServce;
}
public void setTuserServce(TUserService tuserServce) {
this.tuserServce = tuserServce;
}
public String userLogin() {
System.out.println("===============>JSP UserName: "+this.user.getUserName());
System.out.println("===============>JSP PassWord: "+this.user.getPassWord());
List<TUser> tempUser = this.tuserServce.findAllUsers();
for(TUser user : tempUser) {
if(user.getUserName().equals(this.user.getUserName()) && user.getPassWord().equals(this.user.getPassWord())) {
return "success";
}
}
return "input";
}
}
Structs.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<!--
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
<include file="example.xml"/>
-->
<package name="default" namespace="/" extends="struts-default">
<action name="user_userlogin" method="userLogin" class="tuserAction">
<result name="success">/main.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
-->
<!-- 配置dbcp形式的数据连接池 -->
<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://localhost:3306/SSH"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 配置Hibernate托管到Spring中 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/lzb/hibernate/entity/xml/TUser.hbm.xml</value>
</list>
</property>
</bean>
<!-- 业务逻辑层开始配置 -->
<bean name="tuserDaoImpl" class="com.lzb.hibernate.dao.impl.TUserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="tuserServiceImpl" class="com.lzb.hibernate.service.impl.TUserServiceImpl">
<property name="tuserDao" ref="tuserDaoImpl"></property>
</bean>
<bean name="tuserAction" class="com.lzb.hibernate.action.TUserAction">
<property name="tuserServce" ref="tuserServiceImpl"></property>
</bean>
</beans>
发布到tomcat就可以实现登录了。
- 大小: 106.7 KB
- 大小: 45 KB
分享到:
相关推荐
SSH(truts2+hibernate+spring)三大开源框架的参考文档
2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。 4.在服务端分页查询功能,优点:实时性:跳页才查询。数据量小:只加载当前页的记录...
SSH+Redis整合
Struts+Hibernate+Spring 开源框架集成讲解PPT档
Struts 2+Spring 3+Hibernate框架技术精讲与整合案例2Struts 2+Spring 3+Hibernate框架技术精讲与整合案例2Struts 2+Spring 3+Hibernate框架技术精讲与整合案例2Struts 2+Spring 3+Hibernate框架技术精讲与整合案例2
hibernate+struts+spring三大框架集成代码,个人学习时写的,希望对大家有用。
简单的struts+hibernate+spring框架集成实例下载,实现用户登录等功能、
ssh struts+hibernate+Spring 三个框架集成
完整的struts1.2+hibernate3.2+spring2.5框架集成 完整的dwr2.0+spring2.5集成 处理的dwr2.0+hibernate3.2集成的问题
本项目是集成了三大框架,struts+hibernate+spring,实现了权限的管理.......
本人自己动手集成的struts+spring+hibernate 网站后台管理系统,希望对刚学习struts的朋友带来一定的帮助,这是我上传此文件的最大愿望,里面有三个框架所需要的jar包和所有的配置文件,只要下载即可运行,不需要...
该项目是本人自己搭建的ssh2框架,集成ajax,dwr等(struts2,hibernate,spring)
Struts2+hibernate+Spring(Ajax JQurey) 集成采用JQurey框架无刷新状态
struts+hibernate+spring+blob 三个框架集成了一下,主要包括利用struts的上传附件功能,并把上传的图片存储到oracle的blob字段。并提供显示。 没做过多的限制,还有很多bug别见怪,功能跑得通!
ruts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架,上传的附件为struts2+spring+hibernate框架jar包,压缩包共81个文件
使用了框架是Struts1+hibernate+spring 集成了JQuery的FlexiGrid表格插件以及 jsmodal弹出框插件 一个增删改列表 数据库使用的是MYSQL 内有建表语句 数据库配置请自行配置本机数据库
三大框架集成及两两集成的jar包,spring集成hibernate的jar包以及spring集成struts2的jar包已经单独打包
SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架 现在的结构是,Struts负责显示层,Hibernate负责持久层,Spring负责中间的业务层,另外,由于Spring使用的依赖注射以及AOP...
struts+spring+hibernate框架集成