【追加】J2EE三大框架配置文件管理示例--加入hibernate

来源:互联网 发布:烟袋斜街10号网络剧 编辑:程序博客网 时间:2024/06/10 01:48

定义service接口:

package com.STRUTSFRAMEWORK2.web.test.service;public interface TestServiceIf {public void testGetAllInfomation() throws Exception;}


service接口实现:

package com.STRUTSFRAMEWORK2.web.test.service.impl;import org.apache.log4j.Logger;import com.STRUTSFRAMEWORK2.common.dao.CommonDaoIf;import com.STRUTSFRAMEWORK2.common.po.ChannelPO;import com.STRUTSFRAMEWORK2.web.test.service.TestServiceIf;public class TestServiceImpl implements TestServiceIf {private CommonDaoIf<ChannelPO> dao;// 定义loggerprivate static final Logger logger = Logger.getLogger("sysLog");@Overridepublic void testGetAllInfomation() throws Exception {logger.info("****** 进入Service实现操作 ******");// 接收查询结果long num = -1;String hql = "from ChannelPO";num = dao.count(hql);logger.info("****** 统计总个数 ****** 总个数为:" + num);}public CommonDaoIf<ChannelPO> getDao() {return dao;}public void setDao(CommonDaoIf<ChannelPO> dao) {this.dao = dao;}}


Action代码:

package com.STRUTSFRAMEWORK2.web.test.action;import org.apache.log4j.Logger;import com.STRUTSFRAMEWORK2.web.test.service.TestServiceIf;import com.opensymphony.xwork2.ActionSupport;public class TestAction extends ActionSupport {// 定义loggerprivate static final Logger logger = Logger.getLogger("sysLog");// 定义serviceprivate TestServiceIf testService;// 页面数据private String name;private String password;/*** serialVersionUID: ***/private static final long serialVersionUID = 6371668603224418654L;@Overridepublic String execute() {logger.info("****** 进入Action execute方法 ******");try {testService.testGetAllInfomation();} catch (Exception e) {logger.error("hibernate出现异常", e);}if ("success".equalsIgnoreCase(name)) {return "success";} else if ("fail".equalsIgnoreCase(name)) {return "fail";} else {return "error";}}@Overridepublic void validate() {logger.info("****** 进入Action验证程序 ******");logger.info("****** name: ******" + name);logger.info("****** password: ******" + password);}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public TestServiceIf getTestService() {return testService;}public void setTestService(TestServiceIf testService) {this.testService = testService;}}


使用spring管理,在“test模块”的配置文件struts-test.xml和applicationContext_test.xml文件中,应该做如下的修改:

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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><!-- 使用spring管理 --><!-- 注入service --><bean id="testAction" class="com.STRUTSFRAMEWORK2.web.test.action.TestAction"><property name="testService"><ref local="testService"/></property></bean><!-- 注入dao --><bean id="testService" class="com.STRUTSFRAMEWORK2.web.test.service.impl.TestServiceImpl"><property name="dao"><ref local="dao"/></property></bean><!-- 定义dao --><bean id="dao" class="com.STRUTSFRAMEWORK2.common.dao.impl.CommonDaoImpl"></bean></beans>


struts-test.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><package name="test" extends="struts-default" namespace="/sshframework/api"><action name="testCheck" class="testAction"><result name="success">/pages/testpages/success.jsp</result><result name="error">/pages/testpages/error.jsp</result><result name="fail">/pages/testpages/fail.jsp</result></action></package></struts>


使用hibernate以后,需要加入相关的jar包,贴上jar包的截图:

 

 

 

 

 

 

 

 

 

 

 

原创粉丝点击