Myeclipse8.5配置SSH三大框架
1.新建web项目
2.添加Spring配置
(1)右击项目,Myeclipseà”add Spring Capabilities”
(2)选择spring2.5,添加选择spring类库。
Spring 2.5 AOP Libraries
Spring 2.5 Core Libraries
Spring 2.5 Persistence Core Libraries
Spring 2.5 Persistence JDBC Libraries
Spring 2.5 J2EE Libraries
Spring 2.5 Web Libraries
(3)next Finish,完成Spring的配置
3.配置hibernate
(1)切换到hibernate视图
(2)新建一个连接
(3)填写连接信息
(4)测试连接
点击”test Driver”按钮测试,可以点击Save Password复选框记住密码。
(5)nextàFinish结束。
返回java enterprise视图。
4.整合Spring和hibernate
(1)右击项目,Myeclipseà”add Hibernate Capabilities”
(2)默认所有项,点击下一步
(3)选择”Spring Configuration file”,点击下一步继续
(4)选择”Existing Spring Configuration file”,点击下一步继续
(5)选择第三大步配置的hibernate连接,点击下一步继续
(6)去掉复选框的勾选。Fisish结束
5.添加Struts2配置
(1)右击项目,Myeclipseà”add Struts Capabilities”
(2)选择Struts2.1,并选择/*。点击next,继续
(3)选择struts2的类库。点击finish,完成。
Struts 2 Core Libraries
Struts 2 Spring Libraries
6.修改Spring配置文件applicationContext.xml
(1)修改beans标签
修改为:
添加代码:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
(2)添加Spring注解
这样就可以使用注解的方式管理bean了
代码:
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com"></context:component-scan>
(3)配置Spring事务管理
这样在代码中就可以使用@Transactional注解管理事务了。
代码:
<bean name="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
(4)启用HibernateTemplate
这样就可以在代码中注入hibernateTemplate使用了。
代码:
<bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
7.修改web.xml文件
(1)添加Spring启动监听器
(使Spring框架随着tomcat启动而初始化)
代码:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Tips:
1) 打开web.xml的design视图
2) 选择Listeners标签。
3) 点击右边的Add按钮,然后点击“Browse”按钮。在打开的文本框中输入ContextLoaderListener。选择对应的类确定即可,代码会自动添加。
(2)配置指定applicationContext.xml文件的位置
代码:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
将代码复制到web.xml文件中即可
也可以操作:
1) 选择web.xml的design视图。选择Context Params,点击右面的”Add”按钮
2) 填写参数,完成
8.框架配置完成
9.框架使用的几点注意事项
(1)dao层的方便实现
1)BaseDao
BaseDao是接口:
public interface BaseDao<T> {
public void save(T t);
}
使用泛型定义。
2)BaseDaoImpl
BaseDaoImpl是BaseDao的实现。
public class BaseDaoImpl<T> implements BaseDao<T>{
private HibernateTemplate hibernateTemplate;
public void save(T t) {
hibernateTemplate.save(t);
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}
注入了hibernateTemplate。通过hibernateTemplate可以实现增删改查的方法。
3)UserDaoImpl
UserDaoImpl是对应的每个dao的实现,继承了BaseDaoImpl。
@Component("userDao")
public class UserDaoImpl extends BaseDaoImpl<User>{
}
交给Spring管理。
(2)Action交给Spring管理
注意:一定要加上Scope。防止bean单例在多线程下出现问题。
(3)struts配置文件
当action交给spring管理时,struts配置文件的action节点的class属性可以写为bean的name
(4)VO配置。
Action中要实现ModelDriven<T>。
重写方法:
VO也要交给Spring管理
(5)中文乱码问题。
Struts2.1.6之后版本的版本可以使用struts2的常量配置。
在struts配置文件中添加常量:
<constant name="struts.i18n.encoding" value="UTF-8" />
在Struts2.1.6(含)之前的版本,需要定义拦截器修改字符编码。
需要在web.xml文件配置
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
附录一 整合报错:java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
(1)出错现象
root cause
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testa' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) java.security.AccessController.doPrivileged(Native Method)
(2)问题原因
Hibernate3.3 Core Libraries 中的 cglib-2.2.jar 和 Spring 2.5 AOP Librariest 中的 cglib-nodep-2.1_3.jar 版本不一致 发生冲突。
(3)解决方法
删除Hibernate3.3 Core Libraries 中的 cglib-2.2.jar
1)操作步骤 为 windows->Preferences->MyEclipse->Project Capabilities ->Hibernate ->Hibernate 3.3
2)Library modules 选择 Hibernate3.3 Core Library
3)把cglib-2.2.jar remove
4)Project-->Clean 重新部署
posted on 2014-05-13 20:10 simpleJavaCoder 阅读(215) 评论(0) 收藏 举报
浙公网安备 33010602011771号