1.1 需求

在web工程的基础上实现ssh工程的创建,规范依赖管理。

1.2 数据库环境

使用之前学习hibernate创建的数据库:

  

1.3 创建父工程

选择创建Maven Project

点击next,进入下一个页面。在这个页面中选中create a simple project,这样创建的工程就会给我们自动生成目录。

点击next,进入下一个页面。注意此处:packaging类型为pom类型。

点击finish,完成父工程的创建。

1.4 定义父工程的pom.xml

maven工程首先要识别依赖,web工程实现SSH整合,需要依赖struts2.3.24spring4.2.4hibernate5.0.7等,在pom.xml添加工程如下依赖:

分两步:

1)锁定依赖版本

2)添加依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.huida.crm</groupId>
  <artifactId>ssh-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
      <module>ssh-dao</module>
      <module>ssh-service</module>
      <module>ssh-web</module>
  </modules>
  <!-- 为了确定每个框架的版本号 -->

  <!-- 锁定版本 -->
      <properties>
         <spring.version>4.2.4.RELEASE</spring.version>
        <struts2.version>2.3.24</struts2.version>
        <hibernate.version>5.0.7.Final</hibernate.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <shiro.version>1.2.3</shiro.version>
        <cxf.version>3.0.1</cxf.version>
        <c3p0.version>0.9.1.2</c3p0.version> 
        <mysql.version>5.1.6</mysql.version>
    </properties>
      <!-- 锁定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>${hibernate.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-core</artifactId>
                <version>${struts2.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-spring-plugin</artifactId>
                <version>${struts2.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>
  
  <dependencies>
      <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.37</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.1</version>
        </dependency>
        <!-- <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency> -->
        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.1</version>
        </dependency>
    
        <!-- jstl -->
         <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
        
        <!-- shiro -->
         <!-- apache shiro dependencies -->
         <!-- <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-all</artifactId>
            <version>${shiro.version}</version>
         </dependency> -->

      
       <!-- spring -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>
        
         <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
        </dependency>
        
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>${spring.version}</version>
        </dependency>
        
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${spring.version}</version>
        </dependency>

        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-orm</artifactId>
          <version>${spring.version}</version>
        </dependency>
        
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>${spring.version}</version>
        </dependency>
        
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring.version}</version>
        </dependency>
        
    
        
        <!-- struts2  begin -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts2.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>javassist</artifactId>
                    <groupId>javassist</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>
        <dependency>
              <groupId>org.apache.struts</groupId>
              <artifactId>struts2-convention-plugin</artifactId>
              <version>${struts2.version}</version>
          </dependency>
        <!-- struts2  end -->
        
        <!-- hibernate begin -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.1.Final</version>
        </dependency>
        <!-- hibernate end -->
        
        <dependency>
              <groupId>c3p0</groupId>
              <artifactId>c3p0</artifactId>
              <version>${c3p0.version}</version>
          </dependency>
        
        <!-- <dependency> 
            <groupId>org.apache.cxf</groupId> 
            <artifactId>cxf-rt-frontend-jaxws</artifactId> 
            <version>${cxf.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>org.apache.cxf</groupId> 
            <artifactId>cxf-rt-transports-http</artifactId> 
            <version>${cxf.version}</version> 
        </dependency> -->
        
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- log end -->
        
        <!-- Javamail -->
    <!--     <dependency>
          <groupId>javax.mail</groupId>
          <artifactId>mail</artifactId>
          <version>1.4.4</version>
        </dependency> -->
        
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
    
        <dependency>
            <groupId>org.codehaus.xfire</groupId>
            <artifactId>xfire-core</artifactId>
            <version>1.2.6</version>
        </dependency> 
        
        <dependency>
           <groupId>dom4j</groupId>
           <artifactId>dom4j</artifactId>
           <version>1.6</version>
        </dependency>
    
        <!-- <dependency> 
            <groupId>org.apache.poi</groupId> 
            <artifactId>poi</artifactId> 
            <version>3.11</version> 
        </dependency>
        <dependency> 
            <groupId>org.apache.poi</groupId> 
            <artifactId>poi-ooxml</artifactId> 
            <version>3.11</version> 
        </dependency>
        <dependency> 
            <groupId>org.apache.poi</groupId> 
            <artifactId>poi-ooxml-schemas</artifactId> 
            <version>3.11</version> 
        </dependency> -->
    
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
       
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
       <!--  <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>10.2.0.4.0</version>
        </dependency> -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.6.6</version>
        </dependency>
  </dependencies>
  
   <build>
     <finalName>crmssh</finalName>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
  </build>
</project>

注意:此处的pom.xml的内容读者可以直接复制使用便可以满足ssh框架所需要的全部jar包,但是这里面的<modules>标签的内容应该根据用户创建的子模块来定。读者可以不用将<modules>标签的内容粘贴进去,在创建子模块的时候,pom.xml会自动填写<modules>标签中的值。

1.5 dao

1.5.1 创建ssh-dao子模块

创建Maven Module:

点击next进入下一个页面。选中create a simple project,子模块的名称为ssh-dao,父工程通过点击Browse按钮,然后选择ssh-parent.

点击next,进入下一个页面。这里由于dao层并不是web项目,所以packaging类型为jar类型。

 

点击finish即完成子模块的创建。

1.5.2 需求

src/main/java中定义dao接口,实现查询全部用户的信息。

1.5.3 包结构

基本方法放在src/main/java中:

配置文件放在src/main/resources中:

 

test文件放在src/test/java中:

1.5.4 domain模型类

src/main/java创建模型类:

 

package com.huida.domain;

import java.io.Serializable;

public class User implements Serializable{

    /*
     * 持久层规范
     * po对象:(Persisent Object 持久化对象)
     * 1.共有类
     * 2.私有属性
     * 3.公有的getter和Setter
     * 4.不能使用final修饰
     * 5.提供默认的无参构造
     * 6.如果是基本类,就写它对应的包装类
     * 7.一般都要事先java.io.Serialiable
     */
    Integer uid;
    String username;
    String password;
    public Integer getUid() {
        return uid;
    }
    public void setUid(Integer uid) {
        this.uid = uid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
}

 

持久层规范
* po对象:(Persisent Object 持久化对象)
* 1.共有类
* 2.私有属性
* 3.公有的getter和Setter
* 4.不能使用final修饰
* 5.提供默认的无参构造
* 6.如果是基本类,就写它对应的包装类
* 7.一般都要事先java.io.Serialiable

1.5.5 dao接口函数

package com.huida.dao;

import java.util.List;

import com.huida.domain.User;

public interface UserDao {

    /*
     * 查询出user表中的所有记录
     */
    public List<User> findAll();
}

1.5.6 dao实现函数

package com.huida.daoimpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.huida.dao.UserDao;
import com.huida.domain.User;

public class UserDaoImpl implements UserDao {

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

    

    @Override
    public List<User> findAll() {
        
        Session session=sessionFactory.openSession();
        Query query=session.createQuery("from User");
        return query.list();
    }

}

1.5.7 配置文件

1.5.7.1 User.hbm.xml

src/main/resources创建com.huida.domain包,在改包下创建user对应的配置文User.hbm.xml。(因为User.hbm.xml要与我们的user类在同一包下,所以我们也就需要在src/main/resources创建com.huida.domain包)

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
约束的两个作用:dtd  schema
限制标签
提示 -->
<hibernate-mapping>
    <!-- catalog数据库名称,一般省略不写 -->
    <class name="com.huida.domain.User" table="cst_user">
        <!-- 表结构已经创建完毕,表没有创建的时候,length会生效  type-->
        <id name="uid" column="uid">
            <!-- 生成策略 -->
            <!-- identity:设置主键的生成策略为自增 -->
            <generator class="identity"></generator>
        </id>
        <!-- property中的name值与pojo中对应的属性名相同,column的值与数据库对应的字段名11 -->
        <property name="username" column="username"></property>
        <property name="password" column="password"></property>
        
    </class>    

</hibernate-mapping>

 

1.5.7.2  hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <!-- 会话工厂 -->
    <session-factory>
        <!-- 数据库方言,根据数据库选择 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!--为了方便调试是否在运行hibernate时在日志中输出sql语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 是否对日志中输出的sql语句进行格式化 -->
        <property name="hibernate.format_sql">true</property>

        <property name="hibernate.hbm2ddl.auto">none</property>
        
        <!-- 懒加载的校验模式 :配合web.xml中配置的openSessionInViewFilter -->
        <property name="hibernate.enable_lazy_load_no_tran">true</property>
        <!-- 校验模式:JPA  java persistent api -->
        <property name="javax.persistence.validation.mode">none</property>
        <!-- 加载映射文件 -->
        <mapping resource="com/huida/domain/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

1.5.7.3  applicationContext-dao.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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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">

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hibernate-day01?characterEncoding=utf-8" />
        <property name="user" value="root" />
        <property name="password" value="root" />
    </bean>

    <!-- 配置sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 依赖dataSource -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 创建工厂需要加载hibernate映射文件 -->
        <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
    </bean>
    
    <!-- dao -->
    <bean id="userDao" class="com.huida.daoimpl.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

</beans>

1.5.7.4  log4j.properties

src/main/resources配置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

### set log levels - for more verbose logging change 'info' to 'debug' ###
#\u5728\u5F00\u53D1\u9636\u6BB5\u65E5\u5FD7\u7EA7\u522B\u4F7F\u7528debug
log4j.rootLogger=debug, stdout
### \u5728\u65E5\u5FD7\u4E2D\u8F93\u51FAsql\u7684\u8F93\u5165\u53C2\u6570 ###
log4j.logger.org.hibernate.type=TRACEs

1.5.8 单元测试

src/test/java创建单元测试类

 

package com.huida.test;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.huida.dao.UserDao;
import com.huida.domain.User;

public class UserTest {

    @Test
    public void testFindAll() {
        ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:applicationContext-dao.xml");
        UserDao userDao=(UserDao) ac.getBean("userDao");
        List<User> list=userDao.findAll();
        System.out.println(list.size());
    }
}

1.6 service

1.6.1  创建ssh-service子模块

创建过程见ssh-dao的创建过程,同样由于service模块不是web工程,所以packaging类型也应该为jar类型。

1.6.2  在pom.xml中以jar的形式引入dao层

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.huida.crm</groupId>
    <artifactId>ssh-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>ssh-service</artifactId>
  <dependencies>
    <dependency>
        <groupId>com.huida.crm</groupId>
        <artifactId>ssh-dao</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>
  </dependencies>
</project>

1.6.3  包结构

 

1.6.4  service接口函数

package com.huida.service;

import java.util.List;

import com.huida.domain.User;

public interface UserService {

    public List<User> findAll();
}

1.6.5  service实现函数

package com.huida.serviceimpl;

import java.util.List;

import com.huida.dao.UserDao;
import com.huida.domain.User;
import com.huida.service.UserService;

public class UserServiceImpl implements UserService{

    
    private UserDao userDao;
    
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }


    @Override
    public List<User> findAll() {
    
        return userDao.findAll();
    }

}

1.6.6  配置文件

applicationContext-service.xml中配置service

 在这里注意一定要写:<import resource="classpath:applicationContext-dao.xml"></import>才可以将dao层的配置文件引入到service层。

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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">

    <import resource="classpath:applicationContext-dao.xml"></import>
    <!-- service -->
    <bean id="userService" class="com.huida.serviceimpl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

</beans>

1.6.7  测试文件

package com.huida.test;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.huida.domain.User;
import com.huida.service.UserService;

public class UserTest {

    @Test
    public void testUser() {
        ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:applicationContext-service.xml");
        UserService userService= (UserService) ac.getBean("userService");
        List<User> list=userService.findAll();
        System.out.println(list.size());
    }
}

1.7 action

1.7.1 创建ssh-web子模块

 创建Maven Module

 

点击next进入下一个页面。选中create a simple project,父工程选为ssh-parent.

点击next,进入下一个页面。由于ssh-web是一个Web工程,所以packaging类型应该选为war。

点击finish完成工程的创建。

1.7.2 创建web.xml

由于这是一个web工程,所以需要创建web.xml。在Deployed Resources文件夹下的webapp包下创建WEB-INF包,在这个包下创建web.xml。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <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>
     <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-action.xml</param-value>
    </context-param>
</web-app>

1.7.3  在pom.xml中以jar的形式引入service层

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.huida.crm</groupId>
    <artifactId>ssh-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>ssh-web</artifactId>
  <packaging>war</packaging>
  <dependencies>
      <dependency>
          <groupId>com.huida.crm</groupId>
          <artifactId>ssh-service</artifactId>
          <version>0.0.1-SNAPSHOT</version>
      </dependency>
  </dependencies>
</project>

1.7.4 包结构

 

1.7.5 action函数

package com.huida.action;

import java.util.List;

import com.huida.domain.User;
import com.huida.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public String execute() throws Exception {
        List<User> list=userService.findAll();
        System.out.println(list.size());
        return SUCCESS;
        
    }
    
}

1.7.6 配置文件

1.7.6.1 applicationContext-action.xml

applicationContext-action.xml中配置action

 

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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">

    <import resource="classpath:applicationContext-service.xml"></import>
    <!-- action -->
    <bean id="userAction" class="com.huida.action.UserAction">
        <property name="userService" ref="userService"/>
    </bean>

</beans>

 

1.7.6.2 struts.xml

struts.xml中配置action

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <!--     如果想要动态访问Action,必须先要开启如下常量 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
    <constant name="struts.deMode" value="true"></constant>
    <!-- 常量的编写,会产生覆盖 -->
    <!-- <constant name="struts.action.extension" value="aaa,,"></constant> -->
    <!-- 包结构
        name:任意名称,但是不能重复使用
        namespace:命名空间     访问路径
        extends:继承
     -->
   <package name="default" namespace="/" extends="struts-default">
           <action name="userAction_*" class="com.huida.action.UserAction" method="{1}">
               <result name="success">/suc.jsp</result>
           </action>
   </package>

</struts>

1.7.7 JSP文件

在Deployed Resources文件夹下的webapp包下创建suc.jsp。

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
执行成功了
</body>
</html>

1.7.8 测试与调试

将ssh-Web子模块部署到tomcat服务器上,然后启动服务器,在浏览器中输入:http://localhost:8080/ssh-web/userAction_execute。

可以在浏览器上看到页面跳转到sus.jsp页面,并输出"执行成功了",同时在控制台输出“2”,也就是访问数据库成功,数据库中记录条数为2条。这样三层便打通了。

 

posted on 2019-01-18 11:06  wyhluckydog  阅读(338)  评论(0编辑  收藏  举报