SSH框架整合基本步骤以及简单测试

1.导包

  1. Struts2框架

    • struts-2.3.24\apps\struts2-blank\WEB-INF\lib*.jar – Struts2需要的所有jar包
    • struts2-spring-plugin-2.3.24.jar —Struts2整合Spring的插件包
  2. Hibernate框架

    • hibernate-release-5.0.7.Final\lib\required*.jar – Hibernate框架需要的jar包
    • slf4j-api-1.6.1.jar – 日志接口
    • slf4j-log4j12-1.7.2.jar – 日志实现
    • mysql-connector-java-5.1.7-bin.jar – MySQL的驱动包
  3. Spring框架

    • IOC核心包
    • AOP核心包
    • JDBC模板和事务核心包
    • Spring整合JUnit测试包
    • Spring整合Hibernate核心包
    • Spring整合Struts2核心包

基本结构

2.配置web.xml

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" 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">
  <display-name>sshDemo01</display-name>

  <!-- 延迟加载问题 -->
    <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 配置Struts2框架的核心的过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 配置Spring框架整合WEB的监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

3.配置struts.xml

<?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>
    <package name="users" extends="struts-default" namespace="/">
        <action name="user_*" class="userAction" method="{1}">
            <result name="success">/show.jsp</result>
        </action>
    </package> 
</struts>

4.配置applicationContext.xml

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: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">

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.fjut"/>

    <!-- 先配置C3P0的连接池 -->
    <bean id="dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql:///sshdemo01" />
        <property name="user" value="root" />
        <property name="password" value="root" />
    </bean>


    <!-- LocalSessionFactoryBean加载配置文件 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 先加载连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加载方言,加载可选 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>

        <!-- 引入JavaBean映射的配置文件 -->
        <property name="mappingResources">
            <list>
                <!-- <value>com/itheima/domain/Customer.hbm.xml</value> -->
                <value>com/fjut/entity/User.hbm.xml</value>
            </list>
        </property>
    </bean>

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

    <!-- 开启事务的注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

5.jsp界面

index.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>
    <a href="${pageContext.request.contextPath }/user_findAll">查看成员</a>
</body>
</html>

show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
    <c:forEach items="${ulist }" var="user">
        <p>${user.userid} == ${user.username} == ${user.password}</p>
    </c:forEach>
</body>
</html>

6.Entity

User

package com.fjut.entity;

public class User {
    private String userid;
    private String username;
    private String password;

    public User() {
    }
    public User(String userid, String username, String password) {
        this.userid = userid;
        this.username = username;
        this.password = password;
    }
    public String getUserid() {
        return userid;
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }
    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;
    }





}

User.hbm.xml

<?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">

<hibernate-mapping>

    <class name="com.fjut.entity.User" table="User">
        <id name="userid" column="userid">
            <generator class="uuid"/>
        </id>

        <property name="username" column="username"/>
        <property name="password" column="password"/>

    </class>

</hibernate-mapping>    

7.Controller

UserAction


@Controller(value="userAction")
@Scope(value="prototype")
public class UserAction extends ActionSupport{

    @Resource(name="userService")
    private UserService userService;

    public String findAll() {
        List<User> list = userService.findAll();
        ActionContext.getContext().put("ulist", list);
        return SUCCESS;
    }
}

Service

UserService

package com.fjut.service;

import java.util.List;

import com.fjut.entity.User;

public interface UserService {

    void save(User user);

    List<User> findAll();

}

UserServiceImpl

@Transactional
@Service(value="userService")
public class UserServiceImpl implements UserService {

    @Resource(name="userDao")
    private UserDao userDao;

    @Override
    public void save(User user) {
        userDao.save(user);
    }

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

}

8.Dao

UserDao

public interface UserDao {

    void save(User user);

    List<User> findAll();

}

UserDaoImpl

@Repository(value="userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

    @Resource(name="sessionFactory")
    public void setSessionFacotry(SessionFactory sessionFacotry) {
             super.setSessionFactory(sessionFacotry); 
    }  

    @Override
    public void save(User user) {
        this.getHibernateTemplate().save(user);
    }

    @Override
    public List<User> findAll() {
        return (List<User>) this.getHibernateTemplate().find("from User");
    }
}

9.Test

package com.fjut.text;

import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.fjut.entity.User;
import com.fjut.service.UserService;

/**
 * Spring整合Junit单元测试
 * @author LGX
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDemo01 {

    @Resource(name="userService")
    private UserService userService;

    @Test
    public void addUser() {
        User user = new User();
        user.setUsername("lisi");
        user.setPassword("11245312313");
        userService.save(user);
    }

    @Test
    public void findAll() {
        List<User> list = userService.findAll();
        for (User user : list) {
            System.out.println(user.getUserid() + ":" + user.getUsername() + ":" + user.getPassword());
        }
    }

}

猜你喜欢

转载自blog.csdn.net/l1336037686/article/details/80405195
今日推荐