Spring整合Struts2 注解版

1.jar包

        <!--spring配置-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <!--aop使用的jar-->
        <dependency>
            <groupId> org.aspectj</groupId >
            <artifactId> aspectjweaver</artifactId >
            <version> 1.8.7</version >
        </dependency>




        <!--SpringWeb-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.8.RELEASE</version>
        </dependency>

        <!--JavaEE-->
        <dependency>
            <groupId>javaee</groupId>
            <artifactId>javaee-api</artifactId>
            <version>5</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

        <!--c3p0-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

        <!--hibernate jar包-->
        <!--jta的jar包-->
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.6.Final</version>
        </dependency>

        <!--Spring-ORM-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version> 4.2.2.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts.xwork</groupId>
            <artifactId>xwork-core</artifactId>
            <version>2.3.4.1</version>
        </dependency>

        <!--Struts整合Spring的jar包-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.4.1</version>
        </dependency>

        <!--Struts2注解支持jar包-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.4.1</version>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.31</version>
        </dependency>

 

2.分层

 

 2.1DeptAction

@Controller("deptAction")
@ParentPackage("struts-default")
@Namespace("/")
@Scope("prototype")
public class DeptAction extends ActionSupport {
    private Dept dept;

    @Resource(name = "deptService")
    private IDeptService deptService;

    @Action(value = "add",results={@Result(name = "success",location = "/jsp/index.jsp")})
    public String add(){
        deptService.addDept(dept);
        return SUCCESS;
    }

    public Dept getDept() {
        return dept;
    }

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

    public IDeptService getDeptService() {
        return deptService;
    }

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

}

 

2.2Dept

@Entity
@Table(name = "Dept")
public class Dept {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer deptno;
    @Column
    private String deptname;

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    public String getDeptname() {
        return deptname;
    }

    public void setDeptname(String deptname) {
        this.deptname = deptname;
    }

}

 

2.3IDeptDAO

public interface IDeptDAO {
    //添加部门的方法
    public int addDept(Dept dept);
    public List findAll();
}

 

2.4DeptDAOImpl

@Repository("deptDAO")
public class DeptDAOImpl implements IDeptDAO{
    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

    public int addDept(Dept dept) {
        Serializable count = sessionFactory.getCurrentSession().save(dept);
        return (Integer) count;
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public List<Dept> findAll() {
        return sessionFactory.getCurrentSession().createQuery("from Dept").list();
    }

}

 

2.5 IDeptService

public interface IDeptService {
    public void addDept(Dept dept);
}

 

2.6 DeptServiceImpl

 

@Service("deptService")
public class DeptServiceImpl implements IDeptService {
    @Resource(name = "deptDAO")
    private IDeptDAO deptDAO;

    @Transactional
    public void addDept(Dept dept) {
        deptDAO.addDept(dept);
    }

    public IDeptDAO getDeptDAO() {
        return deptDAO;
    }

    public void setDeptDAO(IDeptDAO deptDAO) {
        this.deptDAO = deptDAO;
    }
}

3.映射文件

 

 3.1 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
    <context:component-scan base-package="cn.bdqn"></context:component-scan>
    <!--1.Datasource-->
    <!--1.配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!--2.SessionFactory         类:Local-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <!--hibernate.xxxxxx必须以hibernate-->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!--with current thread bind session-->
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
            </props>
        </property>
        <!--<property name="mappingDirectoryLocations" value="classpath:cn/bdqn/bean"></property>-->
        <property name="packagesToScan" value="cn.bdqn.bean"></property>
    </bean>

    <!--  6.事务管理器-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!--7.事务-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

</beans>

3.2 jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///y2165
jdbc.username=root
jdbc.password=1234

 

3.3 log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=f:\mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

### log4j.rootCategory=debug, stdout,file###
log4j.logger.cn.bdqn.dao=debug, stdout,file

 

 4.web.xml

 

 

  <!--有点东西-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <!--1.监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

 

 5 页面

 <form method="post" action="/add">
      部门名称: <input name="dept.deptname"/>
       <input type="submit" value="添加"/>
   </form>

 

posted on 2017-10-29 16:43  溜氓  阅读(3571)  评论(0编辑  收藏  举报

导航