SSH(Struts2+Spring+Hibernate)框架搭建流程

我先介绍的是MyEclipse9的自带框架支持搭建过程:(完全的步骤  傻瓜式的学习。。~)

首先我们来搭建一个Web项目:

一、Hibernate(数据层)的搭建:

相关描述

Ⅰ.服务器与数据库之间的交互

Ⅱ. Hibernate封装了一系列的JDBC代码,提供相应的方法我们使用,使我们的开发变得简单快捷,效率大大提高

实现流程

二、Struts2(MVC)的搭建:

相关描述

Ⅰ.本质上相当于一个Servlet

Ⅱ.不需要手动获取传递参数 (只需要定义声明属性名和设置get、set的方法)、也不需要手动跳转(只需要struts.xml配置相关的路径)

Ⅲ.对项目的分包(例如:dao、service、entity等等),实现MVC模式的开发

Ⅳ.MVC: Action属于Model,而JSP是View页面的展示,其中过滤器起到了Controller的作用

实现流程

这里注意一点问题:

  Struts2与Hibernate在一起搭建,antlr包,有冲突。MyEclipse9中有这样问题。朋友的MyEclipse10中没有这个问题。

 

  我选择把Struts中antlr包去掉。

三、Spring(注入实例)的使用:

相关描述

Ⅰ.每一层的代码之间的耦合改为模块(分离/解耦),代码之间互不影响

Ⅱ.不再关注具体的实现类的实例

Ⅲ.更换不同的技术(模块),不需要改动代码,只需要修改applicationContext.xml的相关配置信息

Ⅳ.主要功能IOC(控制反转)松耦合、AOP (面向切面)内聚性

实现流程

 

 
编辑web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name></display-name>
    
    <!-- 这是一只拦路虎 -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- 读取applicationContext.xml文件 不写默认Spring需要读取 WebContent\WEB-INF\applicationContext.xml -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 改变默认读取路径为src目录下的 applicationContext.xml 在改变的路径下还是没找到,便自动到默认路径查找 -->
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- 定义Spring监听器 class: spring 3.0 Web Libraries 下可以找到。 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 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>
</web-app> 

DB Browser中反转实体类:

数据访问层dao接口

package com.dao;

import com.entity.Users;

/**
 * 用户接口
 * @author asus
 *
 */
public interface UsersDao {

    /** 登陆验证 */
    Users loginCheck(Users user);
} 

猜你喜欢

转载自www.linuxidc.com/Linux/2016-11/137187.htm