Spring+Struts2+IBatis 项目整合(三)

7.在com.dbke.hrms.spring.logic 包下 建立外观接口 HRMSFacade.java :

 

package com.dbke.hrms.spring.logic;

import java.util.List;

import com.dbke.hrms.ibatis.domain.TDept;

public interface HRMSFacade {

	/**
	 * 显示所有的部门
	 * @return
	 */
	public List<TDept> findAllDept();
	
	/**
	 * 根据部门Id查看部门
	 * @param deptId
	 * @return
	 */
	public TDept findDeptById(Integer deptId);
	
	/**
	 * 增加一个部门
	 * @param dept	部门对象
	 * @return
	 */
	public Integer insertOneDept(TDept dept);
	
	/**
	 * 增加一个部门
	 * @param vcDeptCode
	 * @param vcDeptName
	 * @param vcDeptAddress
	 * @param vcDeptTelphone
	 * @param vcDeptDesc
	 * @return
	 */
	public Integer insertOneDept(String vcDeptCode,String vcDeptName,String vcDeptAddress,String vcDeptTelphone,String vcDeptDesc);
	
	/**
	 * 一次增加若干部门
	 * @param deptList
	 * @return
	 */
	public Integer insertDepts(List<TDept> deptList);
	
	/**
	 * 验证部门名称是否有效
	 * @param deptName
	 * @return true为有效可用,false为无效
	 */
	public boolean validateDeptName(String deptName);
	
	/**
	 * 验证部门编号是否有效
	 * @param deptCode
	 * @return true为有效可用,false为无效
	 */
	public boolean validateDeptCode(String deptCode);
	

}

  8.在 com.dbke.hrms.spring.logic 实现外观接口 HRMSFacadeImpl.java:

 

package com.dbke.hrms.spring.logic;

import java.util.List;

import com.dbke.hrms.ibatis.domain.TDept;
import com.dbke.hrms.struts.service.DeptService;

public class HRMSFacadeImpl implements HRMSFacade{
	
	private DeptService deptService;

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

	@Override
	public Integer insertDepts(List<TDept> deptList) {
		// TODO Auto-generated method stub
		return this.deptService.insertDepts(deptList);
	}

	@Override
	public Integer insertOneDept(String vcDeptCode, String vcDeptName,
			String vcDeptAddress, String vcDeptTelphone, String vcDeptDesc) {
		// TODO Auto-generated method stub
		return this.deptService.insertOneDept(vcDeptCode, vcDeptName, vcDeptAddress, vcDeptTelphone, vcDeptDesc);
	}

	@Override
	public Integer insertOneDept(TDept dept) {
		// TODO Auto-generated method stub
		return this.deptService.insertOneDept(dept);
	}

	@Override
	public List<TDept> findAllDept() {
		// TODO Auto-generated method stub
		return this.deptService.findAllDept();
	}

	@Override
	public TDept findDeptById(Integer deptId) {
		// TODO Auto-generated method stub
		return this.deptService.findDeptById(deptId);
	}

	@Override
	public boolean validateDeptCode(String deptCode) {
		// TODO Auto-generated method stub
		return this.deptService.validateDeptCode(deptCode);
	}

	@Override
	public boolean validateDeptName(String deptName) {
		// TODO Auto-generated method stub
		return this.deptService.validateDeptName(deptName);
	}
	
	
	
}

   到此,spring就暂时结束了.接下来配置struts2

 

三.struts2:

    1.导入 backport-util-concurrent-3.0.jar freemarker-2.3.12.jar struts2-core-2.0.12.jar struts2-spring-plugin-2.0.12.jar  xwork-2.0.6.jar 到WEB-INF/lib

    2.修改WEB-INF/下的 web.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">
	
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<!-- FilterDispatcher 用来初始化 strtus2 并且处理所有的 web 请求 -->
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	 
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
 

    3.在src目录下 建立配置文件 struts.properties:

 

struts.devMode = true
struts.enable.DynamicMethodInvocation = false
struts.objectTypeDeterminer = notiger
struts.objectFactory = spring

    4.在src目录下 建立配置文件 struts.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>
     <constant name="struts.objectTypeDeterminer" value="notiger" />      
     <constant name="struts.objectFactory" value="spring" />   
     <constant name="struts.objectFactory.spring.autoWire" value="name" />   
        
     <include file="struts-default.xml"/>
     
     <!-- struts2的Action配置 -->
     <package name="hrms" extends="struts-default">
     
        <action name="showEmployee" class="showEmployeeClass">
            <result name="success">/index.jsp</result>
        </action>
        
        <action name="showDept" class="showDeptClass">
            <result name="success">/index.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
        
        <!-- Add your actions here -->
    </package>

</struts>

  5.在com.dbke.hrms.struts.service 编写业务逻辑组件的接口 DeptService.java

package com.dbke.hrms.struts.service;

import java.util.List;

import com.dbke.hrms.ibatis.domain.TDept;

public interface DeptService {
	
	/**
	 * 显示所有的部门
	 * @return
	 */
	public List<TDept> findAllDept();
	
	/**
	 * 根据部门Id查看部门
	 * @param deptId
	 * @return
	 */
	public TDept findDeptById(Integer deptId);
	
	/**
	 * 增加一个部门
	 * @param dept	部门对象
	 * @return
	 */
	public Integer insertOneDept(TDept dept);
	
	/**
	 * 增加一个部门
	 * @param vcDeptCode
	 * @param vcDeptName
	 * @param vcDeptAddress
	 * @param vcDeptTelphone
	 * @param vcDeptDesc
	 * @return
	 */
	public Integer insertOneDept(String vcDeptCode,String vcDeptName,String vcDeptAddress,String vcDeptTelphone,String vcDeptDesc);
	
	/**
	 * 一次增加若干部门
	 * @param deptList
	 * @return
	 */
	public Integer insertDepts(List<TDept> deptList);
	
	/**
	 * 验证部门名称是否有效
	 * @param deptName
	 * @return true为有效可用,false为无效
	 */
	public boolean validateDeptName(String deptName);
	
	/**
	 * 验证部门编号是否有效
	 * @param deptCode
	 * @return true为有效可用,false为无效
	 */
	public boolean validateDeptCode(String deptCode);
}

 6.在com.dbke.hrms.struts.service.impl 编写业务逻辑组件的实现 DeptServiceImpl.java

 

package com.dbke.hrms.struts.service.impl;

import java.util.List;

import com.dbke.hrms.ibatis.dao.TDeptDao;
import com.dbke.hrms.ibatis.domain.TDept;
import com.dbke.hrms.struts.service.DeptService;

public class DeptServiceImpl implements DeptService {
	
	/*spring注入的dao*/
	private TDeptDao deptDao;
	
	public void setDeptDao(TDeptDao deptDao) {
		this.deptDao = deptDao;
	}

	@Override
	public Integer insertDepts(List<TDept> deptList) {
		// TODO Auto-generated method stub
		
		for (TDept dept : deptList) {
			this.insertOneDept(dept);
		}
		
		return 0;
	}

	@Override
	public Integer insertOneDept(TDept dept) {
		// TODO Auto-generated method stub
		if (!this.validateDeptName(dept.getVcDeptName())) {
			throw new RuntimeException("部门名称重复");
		}
		
		if (!this.validateDeptCode(dept.getVcDeptCode())) {
			throw new RuntimeException("部门编号名称重复");
		}
		
		return deptDao.insertDept(dept);
	}

	@Override
	public Integer insertOneDept(String vcDeptCode, String vcDeptName,
			String vcDeptAddress, String vcDeptTelphone, String vcDeptDesc) {
		// TODO Auto-generated method stub
		
		TDept dept=new TDept();
		dept.setVcDeptAddress(vcDeptAddress);
		dept.setVcDeptCode(vcDeptCode);
		dept.setVcDeptDesc(vcDeptDesc);
		dept.setVcDeptName(vcDeptName);
		dept.setVcDeptTelphone(vcDeptTelphone);
		
		return this.insertOneDept(dept);
	}

	@Override
	public List<TDept> findAllDept() {
		// TODO Auto-generated method stub
		return deptDao.findAllDept();
	}
	
	
	@Override
	public TDept findDeptById(Integer deptId) {
		// TODO Auto-generated method stub
		return deptDao.findDeptById(deptId);
	}

	@Override
	public boolean validateDeptName(String deptName) {
		// TODO Auto-generated method stub
		if (deptDao.findDeptByName(deptName)==null) {
			return true;
		}else {
			return false;
		}	
	}

	@Override
	public boolean validateDeptCode(String deptCode) {
		// TODO Auto-generated method stub
		if (deptDao.findDeptByCode(deptCode)==null) {
			/*验证部门编号长度是否是3的整数倍*/
			if (deptCode.length()%3!=0) {
				return false;
			}
			return true;
		}else {
			return false;
		}
	}	
	
}

  7.在com.dbke.hrms.struts.action 编写action类 ShowDeptAction.java:

 

package com.dbke.hrms.struts.action;

import java.util.ArrayList;
import java.util.List;

import com.dbke.hrms.ibatis.domain.TDept;
import com.dbke.hrms.spring.logic.HRMSFacade;
import com.opensymphony.xwork2.Action;

public class ShowDeptAction implements Action{

private HRMSFacade hrmsFacade;
	
	public void setHrmsFacade(HRMSFacade hrmsFacade) {
		this.hrmsFacade = hrmsFacade;
	}
	
	/*Struts2的输入参数值*/
	private int di;		//deptID
	
	public int getDi() {
		return di;
	}

	public void setDi(int di) {
		this.di = di;
	}

	/*Struts2的返回值*/
	private TDept dept;
	
	public TDept getDept() {
		return dept;
	}

	public void setDept(TDept dept) {
		this.dept = dept;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		dept=hrmsFacade.findDeptById(di);
		
		
		
		TDept dept1=new TDept();
		dept1.setVcDeptName("重庆分公司");
		dept1.setVcDeptCode("001002");
		dept1.setVcDeptAddress("重庆");
		
//		hrmsFacade.addOneDept(dept1);
		
		TDept dept2=new TDept();
		dept2.setVcDeptName("广州分公司");
		dept2.setVcDeptCode("001003");
		dept2.setVcDeptAddress("广州");
		
		List<TDept> deptList=new ArrayList<TDept>();
		deptList.add(dept2);
		deptList.add(dept1);
		
		try {
			hrmsFacade.insertDepts(deptList);
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ERROR;
		}
		
		return SUCCESS;
	}
	
}

 

8.在 WEB-INF 下编写配置文件 Struts2ActionConfig.xml:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

	<!-- Struts2 action类 -->
	<bean id="showEmployeeClass" class="com.dbke.hrms.struts.action.ShowEmployeeAction" >
		<property name="hrmsFacade" ref="hrmsFacade" />
	</bean>
	
	<bean id="showDeptClass" class="com.dbke.hrms.struts.action.ShowDeptAction" >
		<property name="hrmsFacade" ref="hrmsFacade" />
	</bean>
</beans>

   到此,struts2配置就完成了.下面来做个测试.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值