1、配置web的服务器编码:在配置文件my.ini中
2、create databse itcastoa_2014 default character set utf8;
一、新建工程:
1、右键--new --web project--ItcastOA(工程--右键--properties--others:utf-8)
二、添加ssh并整合:
1、junit4:
ItcastOA--Build path--Add Libraries--JUnit4(不建议)
2、struts2:
web.xml中:
1、拦截器是基于java反射机制的,而过滤器是基于函数回调的。
2、过滤器依赖于servlet容器,而拦截器不依赖于servlet容器。
3、拦截器只能对Action请求起作用,而过滤器则可以对几乎所有请求起作用。
4、拦截器可以访问Action上下文、值栈里的对象,而过滤器不能。
5、在Action的生命周期中,拦截器可以多次调用,而过滤器只能在容器初始化时被调用一次。
<!-- 配置Struts2的主过滤器 -->
<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>
主配置文件:struts.xml:
<!-- 配置为开发模式:好处是显示错误信息,而且配置文件修改之后不需要重启 , value="true"是开发模式-->
<constant name="struts.devMode" value="true" />
<!-- 配置扩展名为action -->
<constant name="struts.action.extension" value="action" />
3、hibernate
hibernate.cfg.xml中:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库信息(连接信息写到spring的配置文件中) -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--
<property name="connection.url">jdbc:mysql:///itcastoa_20120216</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
-->
<!-- 其他配置 -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- 导入映射配置 -->
<mapping resource="cn/itcast/oa/domain/User.hbm.xml" />
<mapping resource="cn/itcast/oa/domain/Department.hbm.xml" />
<mapping resource="cn/itcast/oa/domain/Role.hbm.xml" />
<mapping resource="cn/itcast/oa/domain/Privilege.hbm.xml" />
</session-factory>
</hibernate-configuration>
///代表3306
4、spring
commons-codoc.jar用于md5加密
applicationContext.xml中:
<!-- 自动扫描与装配bean -->
<context:component-scan base-package="cn.itcast.oa"></context:component-scan>
基于注解的方式来自动将配bean
知识点: <context:component-scan>使用说明 :
在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean
注意:如果配置了<context:component-scan>那么<context:annotation-config/>标签就可以不用再xml中配置了,因为前者包含了后者。另外<context:annotation-config/>还提供了两个子标签
(1) <context:include-filter>
(2) <context:exclude-filter>
在说明这两个子标签前,先说一下<context:component-scan>有一个use-default-filters属性,改属性默认为true,这就意味着会扫描指定包下的全部的标有@Component的类,并注册成bean.也就是@Component的子注解@Service,@Reposity等。所以如果仅仅是在配置文件中这么写
<context:component-scan base-package="tv.huan.weisp.web"/>
Use-default-filter此时为true那么会对base-package包或者子包下的所有的进行java类进行扫描,并把匹配的java类注册成bean。
5、整合spring+hibernate
管理sessionFactory(),管理事物
<span style="font-size:12px;"><!-- 加载外部的properties配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据库连接池(c3p0) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基本信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean></span>
<!-- 加载外部的properties配置文件 -->是针对下面的:6数据库连接池
知识点:格式化的时候按什么模板来调整: window--preferences--xml.source--Line width
快捷键Ctrl+Shift+F
6、配置数据库连接池
src--右键--new File--jdbc.properties(放一些基本的配置)
**********************************************
jdbcUrl = jdbc:mysql:///itcastoa_20120216
driverClass = com.mysql.jdbc.Driver
username = root
password = root
***********************************************
这样hibernate.cfg.xml就可以注释掉了
<span style="font-size:12px;"><!-- 数据库信息(连接信息写到spring的配置文件中) -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--
<property name="connection.url">jdbc:mysql:///itcastoa_20120216</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
--></span>
7、配数据库连接池是为了配置sessionFactory 的:
知识点:
/**
*@param str
*@param int
*更新String类型的字段
*/
public int updateDm_bm(String str){
int resu=0;
//获取会话工厂
SessionFactory sf=this.getSessionFactory();
//获取SessionFactory的会话
Session session=(Session)this.getSessionFactory().getCurrentSession();
sf.openSession();
//开始事务
Transaction t=session.beginTransaction();
Query query =session.createQuery(str);
//提交事务
resu=query.executeUpdate();
// Query.executeUpdate()方法返回的整型值表明了受此操作影响
return resu;
}
实现:
struts.xml中:
<!-- 测试用的action,-->
<action name="test" class="cn.itcast.oa.TestAction">
<result name="success">/test.jsp</result>
</action>
TestAction.java:
在applicationContext.xml中:
spring配置SessionFactory ,transactionManager
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置声明式的事务管理(采用基于注解的方式) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
8、Test,在springTest.java中:
<span style="font-size:12px;">public class SpringTest {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 测试SessionFactory
@Test
public void testSessionFactory() throws Exception {
SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
System.out.println(sessionFactory);
}
// 测试事务
@Test
public void testTransaction() throws Exception {
TestService testService = (TestService) ac.getBean("testService");
testService.saveTwoUsers();
}
</span>
ClassPathXmlApplicationContext是spring读取xml最常用的类。而我们一般操作的是她的接口ApplicationContext。
其中测试事务,要借助于service,新建个TestService.java;新建个user.java和映射user.hbm.xml(cn.itcast.oa.domain包)类:
user.java:
<span style="font-size:14px;">public class User {
private Long id;
private String name; // 真实姓名
//生成get.set方法
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
</span>
user.hbm.xml:
<span style="font-size:12px;"><?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 package="cn.itcast.oa.domain">
<class name="User" table="itcast_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
</span>
TestService.java:
<span style="font-size:12px;">package cn.itcast.oa.test;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.oa.domain.User;
/*
* @Service("testService")保证必须是容器里面管理的一个bean
* */
@Service("testService")
public class TestService {
@Resource //
private SessionFactory sessionFactory;
@Transactional //加这个是为了有事务
public void saveTwoUsers() {
/*
* 不用openSession,而用getCurrentSession(),
* openSession是新的,没人会给管理事务
* getCurrentSession()才有人管理事务
* @Transactional 这个注解就是开事务
* */
Session session = sessionFactory.getCurrentSession();
session.save(new User());
// int a = 1 / 0; // 这行会抛异常
session.save(new User());
}
}
//因为之前applicationContext.xml扫描了"cn.itcast.oa"</span>
在struts.xml中:
<!-- 测试用的action-->
<action name="test" class="cn.itcast.oa.test.TestAction">
<result name="success">/test.jsp</result>
</action>
TestAction.java:
public class TestAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("--------> TestAction.execute()");
return "success";
}
}
9、加入jar包,就算整合完成。
(1)struts2-spring-plugin-2.1.8.1.jar
因为它自动替换了对象工厂,由spring管理
(2)在struts.xml中,改下
<!-- 测试用的action,当与Spring整合后,class属性写的就是Spring中bean的名称 -->
<action name="test" class="testAction">
<result name="success">/test.jsp</result>
</action>
(3)TestAction.java,中加入
@Controller
@Scope("prototype")
这两个来引入bean 。
(4)web.xml中引入:
<!-- 配置Spring的监听器,用于初始化ApplicationContext对象 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
10、现在三个框架一起用:
在TestAction.java里面引入service,
@Resource
private TestService testService;
testService.saveTwoUsers();
package cn.itcast.oa.test;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class TestAction extends ActionSupport {
@Resource
private TestService testService;
@Override
public String execute() throws Exception {
System.out.println("--------> TestAction.execute()");
testService.saveTwoUsers();
return "success";
}
}