您的位置:首页 > 编程语言 > Java开发

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

2017-08-04 16:52 495 查看
//框架整合结构图





//创建项目添加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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://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.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.username=oracle
jdbc.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{

@Autowired
private JdbcTemplate template;

//日志记录
protected final Logger logger = Logger.getLogger(this.getClass());

@Override
public int insert(String sql, Object[] args) {
return template.update(sql, args);
}

@Override
public <T> T findObject(String sql, Object[] args, Class<T> clazz) {
return template.queryForObject(sql, args, clazz);
}

@Override
public int update(String sql, Object[] args) {
return template.update(sql, args);
}

@Override
public int delete(String sql, Object[] args) {
return template.update(sql, args);
}

@Override
public <E> List<E> findList(String sql, Object[] args, Class<E> clazz) {
// TODO Auto-generated method stub
return null;
}

}


//创建student实体类

package com.frame.student.bean;

public class Student {

// PO
private String stuid;
private String stuname;
private String stutime;

// Encap
public 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;

@Repository
public 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;

@Service
public class StudentServiceImpl implements StudentService{

@Autowired
private StudentDao dao;

//日志记录
protected final Logger logger = Logger.getLogger(this.getClass());

@Override
public 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);
}

@Override
public Student findObjectStudent(Student entity) {
return null;
}

@Override
public int updateStudent(Student entity) {
return 0;
}

@Override
public int deleteStudent(Student entity) {
return 0;
}

@Override
public 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;

@Autowired
private 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: