Strusts2Hibernate4Spring3的框架整合.docx

上传人:b****1 文档编号:2397310 上传时间:2022-10-29 格式:DOCX 页数:13 大小:187.15KB
下载 相关 举报
Strusts2Hibernate4Spring3的框架整合.docx_第1页
第1页 / 共13页
Strusts2Hibernate4Spring3的框架整合.docx_第2页
第2页 / 共13页
Strusts2Hibernate4Spring3的框架整合.docx_第3页
第3页 / 共13页
Strusts2Hibernate4Spring3的框架整合.docx_第4页
第4页 / 共13页
Strusts2Hibernate4Spring3的框架整合.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

Strusts2Hibernate4Spring3的框架整合.docx

《Strusts2Hibernate4Spring3的框架整合.docx》由会员分享,可在线阅读,更多相关《Strusts2Hibernate4Spring3的框架整合.docx(13页珍藏版)》请在冰豆网上搜索。

Strusts2Hibernate4Spring3的框架整合.docx

Strusts2Hibernate4Spring3的框架整合

Strusts2+Hibernate4+Spring3的框架整合

搭建Spring3.2.1环境

1、把用到的Spring包拷贝到lib目录下

   

2、新建配置文件 applicationContext.xml

 

加入

 compontent-scan>标签

-- 使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入 -->

component-scan base-package="pan.dao,pan.service" /> 

3、编写测试类

 

3-1新建 UserServiceI.java接口

public interface UserServiceI {

public void test();

3-2新建 UserServiceImpl 实现类

//使用 annotation 自动注册bean,并检查@Service已被注入

@Service(value="userService")

public class UserServiceImpl implements UserServiceI {

@Override

public void test() {

System.out.println("Success!

");

}

}

3-3新建测试类SpringTest.java

public class SpringTest {

@Test

public void test(){

ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"resources/spring/applicationContext.xml"});

UserServiceI user=(UserServiceI) ac.getBean("userService"); 

user.test();

}

}

 

 

 

4、Spring在Web.xml中的配置

  

-- Spring的配置 -->

contextConfigLocation

classpath:

resources/spring/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

 

到此Spring配置完毕 

 

搭建Struts2环境(Struts2和Spring的整合)

 

1、添加struts依赖的jar包

 

2、配置struts.xml

-- 开启使用开发模式,详细错误提示 -->

-- 指定资源编码 -->

-- 默认后缀名 -->

-- 将对象交给spring管理 -->

-- 设置Web应用的默认Locale为zh_CN -->

-- struts配置文件更改后是否重新加载 -->

-- 设置浏览器是否缓存静态内容 -->

namespace=”/”>

 

3、配置web.xml

-- 配置struts -->

   struts2

   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

   

   config

   struts-default.xml,struts-plugin.xml,resources/struts/struts.xml

   

   struts2

   *.action

   

 

4、编写测试方法(测试web)

 

public class UserAction {

public String test(){

return "success";

}

}

 

在struts.xml里添加 

-- action测试 -->

/WEB-INF/admin/test.jsp

 

测试结果:

  

 

 

5.1、编写测试方法(测试Spring 和 Struts的结合是否成功 方法1)

public String test2(){

WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext、(ServletActionContext.getServletContext());

UserServiceI userService = (UserServiceI)ac.getBean("userService");

userService.test();

return "success";

 

}

 

5.2、(测试Spring 和 Struts的结合是否成功 方法2)

增加属性 UserServiceI

private UserServiceI userService;

public UserServiceI getUserService() {

return userService;

}

@Autowired 

public void setUserService(UserServiceI userService) {

this.userService = userService;

}

 

测试结果 :

 

 

Hibernate4的配置(Hibernate和Spring的整合)

1、导入Hibernate4的核心jar包

 

2、配置applicationContext.xml(因为我的Hibernate和Spring的配置都写在了该文件中)

-- Hibernate数据源的配置 -->

destroy-method="close">

-- 指定连接数据库的驱动 -->

-- 指定连接数据库的URL -->

value="jdbc:

mysql:

//localhost:

3306/examonline" />

-- 指定连接数据库的用户名 -->

-- 指定连接数据库的密码 -->

-- 指定连接数据库的连接池的初始化大小 -->

-- 指定连接数据库的连接池最大连接数 -->

-- 指定连接数据库的连接池最大空闲时间 -->

-- 指定连接数据库的连接池最大等待时间 -->

-- 配置hibernate session工厂 -->

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

org.hibernate.cache.EhCacheProvider

true

true

thread

org.hibernate.dialect.MySQLDialect

update

true

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 面试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1