框架整合____Spring整合Struts2(主流整合方式,最精简整合方式)
2017-08-04 16:52
495 查看
//框架整合结构图
![](http://img.blog.csdn.net/20170804162307588?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE4MjcyNDU1NjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20170804162316200?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE4MjcyNDU1NjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
//创建项目添加Spring依赖添加struts依赖
配置applicationContext.xml
//创建一个BaseAction目睹就是封装谢东西 让其他模块的action集成baseaction
//存放request等 因为stuts获取request首先要集成actionsupport
//创建basedao和实现类提供jdbc的操作
//创建student实体类
//创建Student的service接口和实现类
![](http://img.blog.csdn.net/20170804164145164?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE4MjcyNDU1NjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
//配置struts.xml配置文件
![](http://img.blog.csdn.net/20170804164245071?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE4MjcyNDU1NjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
发布项目到tomcat启动访问
![](http://img.blog.csdn.net/20170804164835945?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE4MjcyNDU1NjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20170804164903271?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE4MjcyNDU1NjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20170804164946126?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE4MjcyNDU1NjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
//源码: http://pan.baidu.com/s/1qYuhNmW
//创建项目添加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
相关文章推荐
- 框架整合____Spring整合Mybatis(主流,最精简整合方式,)
- 框架整合____SSM框架整合(主流整合方式,最精简整合方式)
- Spring-Struts2两大框架整合过程介绍(基于注解方式)
- struts2、spring、hibernate整合jquery的json传值接收值的两种方式
- [置顶] spring2.5 + struts2 + ibatis2.3.4 框架整合开发
- Struts2,Spring,Hibernate三大框架的整合(SSH)
- spring,hibernate和struts2三大框架整合初印象
- Spring 框架整合Struts2 框架和 Hibernate 框架
- SSI(Struts2, Spring, iBatis)框架整合小结
- 边做边学小型封装--利用主流框架进行Dao层通用化,Spring3.0+Hibernate3.3.2通用Dao层整合(一)
- Struts2和Spring3.2 整合时。Struts2 Action 无法通过get,set方式获取前台form传的值
- 关于struts2验证框架,在与spring整合后重复输出错误信息的问题
- 整合框架 javaweb开发平台ssmy_m(与代码生成) java struts2 mybatis spring maven jquery
- Spring整合Struts2的两种方式
- Struts2,Hibernate和Spring之间的框架整合关系
- 边做边学小型封装--利用主流框架进行Dao层通用化,Spring3.0+Hibernate3.3.2通用Dao层整合(四)
- struts2和spring的两种整合方式
- struts2和spring的两种整合方式 action的自动装配
- SSH(spring、struts2、hibernate)三大框架整合
- 框架学习之Spring 第五节 SSH整合开发[Spring2.5+Hibernate3.3+Struts2]