SSHに基づく単純なWebプロジェクト全体の確立(構成ファイルを含む)[使用されるソフトウェアはIntelliJ IDEA 2018.3.3 x64、動作環境はTomcatです]

                       **
empのSQLデータベース;
ここに画像の説明を挿入

2.プロジェクト全体のおおよそのファイルディレクトリは図
ここに画像の説明を挿入
2.1のようになります。まず、上記のファイルディレクトリに従っていくつかの特別なファイルを追加し、必要に応じて追加します。
2.2 SSHの思考プロセス全体を整理するには、以下に示すようにSSHでプロセス全体を使用します。[プロセス全体は、sshプロセスと組み合わされたプロジェクトの順序で記述されます]
ここに画像の説明を挿入
      *** ~~

2.2.1

~~ ***プロジェクトの開始時には、プロジェクトにコンテンツがあるかどうかに関係なく、web.xml構成ファイルが必要です。
      web.xml構成ファイルの内容は次のとおりです。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

  <!--用监听器启动spring-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--加载spring配置文件-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/ApplicationContext.xml</param-value>
  </context-param>
  <!--启动struts2-->
  <filter>
    <filter-name>struts2</filter-name>
    <!--过滤器的全类限定名-->
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <!--要拦截的目标-->
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

      ****

2.2.2

**ユーザーはWebレイヤー、つまりWebプロジェクトのWebページを通過します。このプロジェクトでは、index.htmlおよびjsp(javaサーバーページ)です。*注:プロジェクト全体の入り口はindex.htmlです。 tomcatの実行中のページ
     
index.htmlコードは次のとおりです。

  <%--
  Created by IntelliJ IDEA.
  User: xionghaizi
  Date: 2019/6/21
  Time: 14:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>页面</title>
</head>
<body>
   <a href="${pageContext.request.contextPath}/empAction!findEmpAll.action">查询全部</a>
</body>
</html>

注:$ {pageContext.request.contextPath}は、コンテキストを取得するためのものです
$ {pageContext.request.contextPath} /empAction!findEmpAll.actionは、アクションを介して操作するためにサービスレイヤーに移動するためのものです。//わからない場合は、最初に読んでください。影響はありません。

      emp / list.jspのコード:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>显示所有员工</title>
</head>
<body>
      <%--${empList}--%>
    <table border="1" align="center">
           <tr>
               <th>编号</th>
               <th>姓名</th>
               <th>年龄</th>
               <th>地址</th>
               <th>生日</th>
               <th>操作</th>
           </tr>

        <c:forEach items="${empList}" var="emp">
            <tr>
                <td>${emp.id}</td>
                <td>${emp.name}</td>
                <td>${emp.age}</td>
                <td>${emp.address}</td>
                <td>${emp.birthday}</td>
                <td>
                    <a href="${pageContext.request.contextPath}/empAction!editPage.action?id=${emp.id}">修改</a>
                    <a href="${pageContext.request.contextPath}/empAction!deleteEmp.action?id=${emp.id}">删除</a>
                </td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

注:<c:forEach> </ c:forEach>を使用する場合は、<%@ taglib prefix =“ c” uri =“ http://java.sun.com/jsp/jstl/core ”%>を導入する必要があります。

      emp / edit.jspコード:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/6/21
  Time: 11:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>修改员工信息</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/empAction!editEmp.action" method="post">
    <p>编号:<input type="text" name="emp.id" value="${emp.id}"> </p>
    <p>姓名:<input type="text" name="emp.name" value="${emp.name}"> </p>
    <p>年龄:<input type="text" name="emp.age" value="${emp.age}"> </p>
    <p>地址:<input type="text" name="emp.address" value="${emp.address}"> </p>
    <p>生日:<input type="date" name="emp.birthday" value="${emp.birthday}"> </p>
    <p><input type="submit"  value="修改保存"> </p>
</form>
</body>
</html>

インターフェイス操作のスクリーンショット1:
ここに画像の説明を挿入
[すべてクエリ]をクリックすると、すべてのコンテンツが表示され
ここに画像の説明を挿入
     ます。スクリーンショット2と同様に、[変更]をクリックします。スクリーンショット3:変更を
ここに画像の説明を挿入
      保存すると、すべてのコンテンツを表示するページにリダイレクトされます。これは、スクリーンショット1.、クリック削除操作は同じです。
ページ操作の一般的な機能について説明しました。次のステップは、サービスレイヤーのようにコードを進めることです。
****

      2.2.3

**
クリックしてindex.htmlのすべての情報を表示してからジャンプします。この操作は、サービスレイヤーを通過して、このプロジェクトに表示する必要があります。これは、次のJava操作です。
ここに画像の説明を挿入
     これには、アクション、サービス、dao、およびエンティティが含まれます。このシーケンスは、Webページのボタンをクリックした後にサービスレイヤーで実行されるシーケンスでもあります。

名前が説明するように、あまり紹介せずに、より重要なことはアクションです。これは、Webがサービスレイヤーに転送できるキーと同等であり、セキュリティやその他の多くの考慮事項のために、検出およびフィルタリングされます。 sshのStruts(フィルター)です。
構成ファイルstruts.xml [構成ファイルに関する具体的な説明が必要な場合は、Strutsに関する以前のブログを参照してください]

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

<struts>
    <!--常亮-->
    <constant name="struts.devMode" value="true"/>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <constant name="struts.i18n.encoding" value="UTF-8"/>

    <package name="emp" extends="struts-default">
        <global-allowed-methods>regex:.*</global-allowed-methods>
        
        <action name="empAction" class="com.whpu.k16035.action.EmpAction" >
            <result name="list">/jsp/emp/list.jsp</result>
            <result name="edit">/jsp/emp/edit.jsp</result>
            <!--重定向-->
            <result name="requert" type="redirect">empAction!findEmpAll.action</result>
        </action>
    </package>
</struts>

     コードに直接サービスレイヤーについて以下を示します。

     アクション/エンパクションのコード:

package com.whpu.k16035.action;

import com.whpu.k16035.entity.Emp;
import com.whpu.k16035.service.EmpService;

import java.util.List;

/**
 * 控制器:处理请求
 */
public class EmpAction {
    //服务层
    private EmpService empService;
    //传递数据
    private List<Emp> empList;
    //获取id
    private Integer id;
    //对象
    private Emp emp;

    //查询全部
    public String findEmpAll(){
        //调用服务层的查询全部方法
        empList=empService.selectEmpAll();
        return "list";
    }

    //删除
    public String deleteEmp(){
        //调用服务层的删除方法
        empService.deleteEmp(id);
        //重定向到查询全部
        return "requert";
    }

    //    修改查询 editPage
    public String editPage(){
        //调用服务层的查询单挑的方法
        emp=empService.findEmpById(id);
        //跳转到修改页面
        return "edit";
    }

    //    修改保存数据 editEmp
    public String editEmp(){
        //调用服务层的修改方法
        empService.editEmp(emp);
        //重定向到查询全部的方法去
        return "requert";
    }

    // --------------------------------------
    public List<Emp> getEmpList() {
        return empList;
    }

    public void setEmpList(List<Emp> empList) {
        this.empList = empList;
    }

    public EmpService getEmpService() {
        return empService;
    }

    public void setEmpService(EmpService empService) {
        this.empService = empService;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Emp getEmp() {
        return emp;
    }

    public void setEmp(Emp emp) {
        this.emp = emp;
    }
}

注:struts.xmlおよびEmpAction.javaで対処する必要のあるいくつかの場所:[太字でマーク]

<result name = "list" > /jsp/emp/list.jsp
<result name = "edit" > / jsp / emp / edit.jsp

<result name = "requert" type = "redirect"> empAction!findEmpAll.action

dao / EmpDaoコード:

package com.whpu.k16035.dao;
import com.whpu.k16035.entity.Emp;
import java.util.List;
/**
 *  持久层的接口
 */
public interface EmpDao {
    //查询全部
    List<Emp> selectEmpAll();
  //删除
    void deleteEmp(Emp emp);
  //查找单条数据
    Emp findEmpById(Integer id);
  //修改
    void editEmp(Emp emp);
}

dao / impl / EmpDaoImplコード:

package com.whpu.k16035.dao.impl;
import com.whpu.k16035.dao.EmpDao;
import com.whpu.k16035.entity.Emp;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import java.util.List;

/**
 *  持久层  操作数据库
 *
 */
public class EmpDaoImpl extends HibernateDaoSupport implements EmpDao {
    //查询全部
    @Override
    public List<Emp> selectEmpAll() {
        return (List<Emp>)this.getHibernateTemplate().find("From Emp");
    }
    //删除数据
    @Override
    public void deleteEmp(Emp emp) {
        getHibernateTemplate().delete(emp);
    }
    //查询单条
    @Override
    public Emp findEmpById(Integer id) {
        return getHibernateTemplate().get(Emp.class,id);
    }
    //修改保存
    @Override
    public void editEmp(Emp emp) {
        getHibernateTemplate().update(emp);
    }
}

service / EmpServiceコード:

package com.whpu.k16035.service;
import com.whpu.k16035.entity.Emp;

import java.util.List;
/**
 * 服务层 处理业务逻辑的
 *        事物管理
 */
public interface EmpService {
    //查询全部
    List<Emp> selectEmpAll();
    //删除数据
    void deleteEmp(Integer id);
    //查询单条
    Emp findEmpById(Integer id);
    //修改
    void editEmp(Emp emp);
}

service / impl.EmpServiceImplコード:

package com.whpu.k16035.service.impl;

import com.whpu.k16035.dao.EmpDao;
import com.whpu.k16035.dao.impl.EmpDaoImpl;
import com.whpu.k16035.entity.Emp;
import com.whpu.k16035.service.EmpService;

import java.util.List;

/**
 *  服务层的实现类
 */
public class EmpServiceImpl implements EmpService {
    //创建持久层对象
    //private EmpDao empDao =new EmpDaoImpl();
    private EmpDao empDao;
    //查询全部
    @Override
    public List<Emp> selectEmpAll() {
        return empDao.selectEmpAll();
    }
    //删除
    @Override
    public void deleteEmp(Integer id) {
        Emp emp = new Emp();
        emp.setId(id);
        empDao.deleteEmp(emp);
    }
    @Override
    public Emp findEmpById(Integer id) {
        return empDao.findEmpById(id);
    }
    @Override
    public void editEmp(Emp emp) {
        empDao.editEmp(emp);
    }
    //-------------------
    public EmpDao getEmpDao() {
        return empDao;
    }

    public void setEmpDao(EmpDao empDao) {
        this.empDao = empDao;
    }
}

エンティティ/ Empコード:

package com.whpu.k16035.entity;

import java.util.Date;

public class Emp {
    //创建与表对应的属性
    private Integer id;
    private String name;
    private Integer age;
    private String address;
    private Date birthday;
    //构造方法  一定要有无参构造
    public Emp() {
    }
    //setter getter
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                ", birthday=" + birthday +
                '}';
    }
}

****

      2.2.4

**
Hibernateマッピングファイル、hibernate.xmlファイル:これは永続層であり、データベースに接続する最後の層です。Hibernateはデータベースの操作を簡素化できます。
     1.エンティティクラスマッピングファイル
            Emp.hbm.xmlを
      作成します。2。Hibernateのメイン構成ファイル
            hibernate.cfg.xmlを作成します。

Emp.hbm.xml構成ファイルコード:

<?xml  version="1.0" encoding="UTF-8"  ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.whpu.k16035.entity">
     <class name="Emp" table="emp">
         <id name="id" column="id" type="java.lang.Integer">
             <generator class="native"/>
         </id>
         <property name="name" type="java.lang.String">
             <column name="name" length="64"/>
         </property>
         <property name="age" type="java.lang.Integer" column="age"/>
         <property name="address" type="java.lang.String" column="address"></property>
         <property name="birthday" type="java.sql.Date" column="birthday"></property>
     </class>
</hibernate-mapping>

hibernate.cfg.xml構成コード:

<?xml  version="1.0" encoding="UTF-8"  ?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!--方言-->
        <!--<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>-->
        <!--&lt;!&ndash;连裤四要素&ndash;&gt;-->
        <!--<property name="connection.url">jdbc:mysql://localhost:3306/test4?characterEncoding=utf-8</property>-->
        <!--<property name="connection.driver_class">com.mysql.jdbc.Driver</property>-->
        <!--<property name="connection.username">root</property>-->
        <!--<property name="connection.password">123</property>-->
        <!--现实sql语句-->
        <!--<property name="show_sql">true</property>-->
        <!--&lt;!&ndash;启动是建表的类型&ndash;&gt;-->
        <!--<property name="hbm2ddl.auto">update</property>-->
        <!--&lt;!&ndash;映射文件&ndash;&gt;-->
        <!--<mapping resource="mapper/Emp.hbm.xml"/>-->
    </session-factory>
</hibernate-configuration>

注:これについて質問がある人もいるかもしれませんが、SSHがStrutsとHibernateについて話しているのに、Hibernateの構成ファイルには何もありません。これは私が話したい春です。最初から、 SSHは明らかです。Springはプロセス全体を実行し、Hibernateのメイン構成のコンテンツはSpringに関連する構成ファイルにあるため、注釈を付けることができます。
****

      2.2.4:SpringのContextApplicationContext.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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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">

    <!--1.加载连库四要素-->
    <context:property-placeholder location="classpath:dbconfig/dbmysql.properties" />
    <!--2.创建数据库连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--创建sessionFactroy-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!--数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--加载hibernate主配置文件-->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <!--加载实体类映射文件-->
        <property name="mappingLocations" value="classpath*:mapper/*.hbm.xml"/>
        <!--hibernate的其他配置-->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="show_sql">true</prop>
                <prop key="format_sql">true</prop>
                <prop key="hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <!--事物管理-->
    <!--事物管理器-->
    <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!--事物传播特性-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*"/>
            <!--select*  方法设置只-->
            <tx:method name="select*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!--事物切面-->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.whpu.k16035.service.impl.*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>

    <!--dao层-->
    <bean id="empDao" class="com.whpu.k16035.dao.impl.EmpDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!--服务层-->
    <bean id="empService" class="com.whpu.k16035.service.impl.EmpServiceImpl">
        <property name="empDao" ref="empDao"/>
    </bean>

    <!--控制器-->
    <bean id="empAction" class="com.whpu.k16035.action.EmpAction" scope="prototype">
        <property name="empService" ref="empService"/>
    </bean>

</beans>

この時点で、SSH実装プロセス全体が基本的に完了しており、環境の一部の構成やその他の構成のみを自分で完了する必要があります。
私の組織があなたのお役に立てば幸いです。^-^
                                                                                                Xiongxyiによる。

おすすめ

転載: blog.csdn.net/qq_43479839/article/details/93200640