目前三大框架版本都更新的很快,strut2到了2.3,hibernate到了hibernate4,spring也早就3.0以上了,但是原先版本的和现在的大概没啥却别
以就版本的来做,配置的主要原则是先搭架子后添肉,先写配置文件,别的至于类啥的都好说。
1 首先导入各种各样的jar包
在配置前先总结下:
三个框架尽量相互独立,降低耦合,但是他们又有关联,关联的地方有两点:
A:struts和spring整合,在Action里面 直接调用spring里面的bean就可以了,因为他们之间有个 插件做关联,就是 struts2-spring-pluingxxx.jar
B:hibernate和spring,在spring中会引用到hibernate.cfg.xml文件,只是这点做关联,耦合很低,很容易配置
当然了,整合会碰到各种问题,但是有个谷歌和度娘还怕啥呢
整合的方式有多种,光事物处理就有三种方法,但是 宗旨和目标都不变的,只不过是换了一中形式罢了
还有 有jdbctemplate/hibernatetemplate/hibernatedaosupport等多种方式,本例以 hibernatetemplate方式为例
2 配置web.xml
此次配置了加载struts和spring两个主要配置
加载struts时候也加载了strtus.xml文件,这个文件默认放在src目录下
加载spring的时候会指定applicationContext.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>classpath: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>
3 主要配置 struts相关的东西
配置struts.xml文件 主要是个注册的Action的配置
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="netdisk" extends="struts-default">
<action name="register" class="com.user.action.RegisterAction">
<result name="success">/WEB-INF/jsp/result.jsp</result>
<result name="input" >/WEB-INF/jsp/register.jsp</result>
</action>
<action name="skip">
<result>/WEB-INF/jsp/register.jsp</result>
</action>
</package>
</struts>
4 配置 hibernate配置文件,hibernate.cfg.xml,放在src目录下 ,这个很简单,就是一个 数据源,一个是映射文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.url">jdbc:mysql://localhost/disk?characterEncoding=UTF8</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- <property name="hibernate.connection.autocommit">true</property> -->
<mapping resource="com\user\pojo\User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5 配置 spring的配置文件 applicationContext.xml,放在src目录下
这个配置稍复杂,有以下几个部分组成
A:配置sessionFactory,引用了Hibernate。cfg。xml这个配置
B:配置了hibernateTemplate,这个用在后面写java类时用到,都是必须的,整合的方式就是hibernatetemplate
C:配置事物,事物和aop切面编程不分家的,需要配置三个bean,一个是事物管理器,一个是这个事物的管理内容(是否新建事务,可读等),第三个是范围,指定了
那个bean
D: 书写用户后面要写 的Bean,这里的是userDao
E: 剩下那个是加载属性文件的,不重要,可以忽略
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>userDao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<bean id="userDao" class="com.user.dao.UserDaoImpl">
<constructor-arg>
<ref bean="hibernateTemplate"/>
</constructor-arg>
</bean>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>WEB-INF\netdisk.properties</value>
</property>
</bean>
</beans>
5 架子打完了,开始学java类
一共写两个类,非常的简单,一个是UserDaoImpl类,为例简单,连接口省略了,为了减少干扰项,另外一个类就是注册Action 到了这里,基本核心的东西都写完了,剩下的
可以自己发挥了,怎么写都可以。
1)
package com.user.dao;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.user.pojo.User;
public class UserDaoImpl {
private HibernateTemplate hibernateTemplate;
public UserDaoImpl() {
}
public UserDaoImpl(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public void save(User user)
{
this.hibernateTemplate.save(user);
}
}
package com.user.action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.user.dao.UserDaoImpl;
import com.user.pojo.User;
public class RegisterAction extends ActionSupport implements ModelDriven<User>{
private UserDaoImpl userDao;
public UserDaoImpl getUserDao() {
return userDao;
}
public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute()
{
userDao.save(user);
return "success";
}
User user = new User();
@Override
public User getModel() {
return user;
}
}
6 刚刚忽略了一点 POJO类,继续加上,这些都是hibernate单独的东西,很简单
package com.user.pojo;
public class User {
private String username;
private String password;
private String qq;
// public int getId() {
// return id;
// }
// public void setId(int 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;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
}
对应的配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.user.pojo.User" table="t_users">
<id name="username" column="user"></id>
<property name="password" column="password_md5"></property>
<property name="qq" column="qq"></property>
</class>
</hibernate-mapping>
7 还有就是几个jsp页面了
有index.jsp
register.jsp
result.jsp
最主要的是register.jsp很简单
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="register.action" method="post">
<s:textfield name="username"></s:textfield>
<s:password name="password"></s:password>
<s:textfield name="qq"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
8 在数据要建一个表,看user也知道咋建了,
查询结果
可见,数据都已经加进去了