框架整合____Spring整合Struts2(主流整合方式,最精简整合方式)

来源:互联网 发布:tensorflow 编辑:程序博客网 时间:2024/06/01 22:40

//框架整合结构图


//创建项目添加Spring依赖添加struts依赖

配置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:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:c="http://www.springframework.org/schema/c"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"><!-- springconfigStart --><!-- 加载多个资源配置文件    --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:frame_jdbc.properties</value></list></property></bean>  <!-- 使用注解的方式装配置bean --><context:annotation-config /><context:component-scan base-package="com.frame"></context:component-scan><!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --><mvc:annotation-driven /><!-- 配置dbcp数据源  --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!-- 配置事物管理 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 使用annotation注解方式配置事务 --><tx:annotation-driven transaction-manager="transactionManager"/><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- springconfigEnd --></beans>
//配置jdbc文件

################Oracle_JDBC################jdbc.driverClassName=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcljdbc.username=oraclejdbc.password=123456

//创建一个BaseAction目睹就是封装谢东西 让其他模块的action集成baseaction

//存放request等 因为stuts获取request首先要集成actionsupport

package com.frame.base.action;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class BaseAction extends ActionSupport{/** * SERID */private static final long serialVersionUID = 3591490585451768338L;public HttpServletRequest request = ServletActionContext.getRequest();public HttpServletResponse response = ServletActionContext.getResponse();protected final Logger logger = Logger.getLogger(this.getClass());protected void writeToPage(String respData){response.setCharacterEncoding("UTF-8");response.setContentType("text/html;charset=UTF-8");try {response.getWriter().write(respData);} catch (IOException e) {e.printStackTrace();}}}

//创建basedao和实现类提供jdbc的操作

package com.frame.base.dao;import java.util.List;public interface BaseDao {public int insert(String sql,Object[] args); public <T> T findObject(String sql,Object[] args,Class<T>clazz);public int update(String sql,Object[] args);public int delete(String sql,Object[] args);public <E> List<E> findList(String sql,Object[] args,Class<E>clazz);}

package com.frame.base.dao;import java.util.List;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;public class BaseDaoImpl implements BaseDao{@Autowiredprivate JdbcTemplate template;//日志记录protected final Logger logger = Logger.getLogger(this.getClass());@Overridepublic int insert(String sql, Object[] args) {return template.update(sql, args);}@Overridepublic <T> T findObject(String sql, Object[] args, Class<T> clazz) {return template.queryForObject(sql, args, clazz);}@Overridepublic int update(String sql, Object[] args) {return template.update(sql, args);}@Overridepublic int delete(String sql, Object[] args) {return template.update(sql, args);}@Overridepublic <E> List<E> findList(String sql, Object[] args, Class<E> clazz) {// TODO Auto-generated method stubreturn null;}}

//创建student实体类

package com.frame.student.bean;public class Student {// POprivate String stuid;private String stuname;private String stutime;// Encappublic String getStuid() {return stuid;}public void setStuid(String stuid) {this.stuid = stuid;}public String getStuname() {return stuname;}public void setStuname(String stuname) {this.stuname = stuname;}public String getStutime() {return stutime;}public void setStutime(String stutime) {this.stutime = stutime;}}
//创建studentdao做个空接口就可以了集成父类接口方法不在重复定义方法因为service里还用重复定义方法

package com.frame.student.dao;import com.frame.base.dao.BaseDao;public interface StudentDao extends BaseDao {}

package com.frame.student.dao;import org.springframework.stereotype.Repository;import com.frame.base.dao.BaseDaoImpl;@Repositorypublic class StudentDaoImpl extends BaseDaoImpl implements StudentDao{}

//创建Student的service接口和实现类

package com.frame.student.service;import java.util.List;import com.frame.student.bean.Student;public interface StudentService {public int insertStudent(Student entity); public Student findObjectStudent(Student entity);public int updateStudent(Student entity);public int deleteStudent(Student entity);public List<Student> findListStudent(Student entity);}
package com.frame.student.service;import java.util.List;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.frame.student.bean.Student;import com.frame.student.dao.StudentDao;@Servicepublic class StudentServiceImpl implements StudentService{@Autowiredprivate StudentDao dao;//日志记录protected final Logger logger = Logger.getLogger(this.getClass());@Overridepublic int insertStudent(Student entity) {StringBuffer sb=new  StringBuffer();sb.append("insert into STUDENT(stuid,stuname,stutime) values(?,?,?) ");Object [] args={entity.getStuid(),entity.getStuname(),entity.getStutime()};return dao.insert(sb.toString(), args);}@Overridepublic Student findObjectStudent(Student entity) {return null;}@Overridepublic int updateStudent(Student entity) {return 0;}@Overridepublic int deleteStudent(Student entity) {return 0;}@Overridepublic List<Student> findListStudent(Student entity) {return null;}}
//创建student的action层

package com.frame.student.action;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import com.frame.base.action.BaseAction;import com.frame.student.bean.Student;import com.frame.student.service.StudentService;@Controller@Scope("prototype")public class StudentAction extends BaseAction{/** * SERID */private static final long serialVersionUID = 2365184082552013332L;@Autowiredprivate StudentService service;//日志记录protected final Logger logger = Logger.getLogger(this.getClass());public void inserStudent(){Student entity=new Student();entity.setStuid("id_"+System.currentTimeMillis());entity.setStuname(request.getParameter("stuname"));  entity.setStutime(request.getParameter("stutime")); int result = service.insertStudent(entity);if(result==1){writeToPage("插入成功!!!");}}}
//写一个新增student的页面index.jsp


//配置struts.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><package name="struts-student" extends="struts-default" namespace="/student"><action name="inserStudent" class="com.frame.student.action.StudentAction" method="inserStudent"></action></package></struts>    


发布项目到tomcat启动访问








//源码:

http://pan.baidu.com/s/1qYuhNmW