基于Struts2和hibernate的WebSocket聊天室的实现教程一:环境搭建

来源:互联网 发布:linux编译android源码 编辑:程序博客网 时间:2024/06/05 15:17

这篇文章,对,就是写给她看的

开发环境

  • OS:Windows 10
  • JDK:1.8
  • IDE:Intellij IDEA 2016.02
  • Jars:struts2-2.3.8,hibernate-5.2.2.final,json-lib-2.2.1-jdk15.jar,mysql驱动,javax.websocket-api…..
  • Web中间件:Tomcat-8.0.30
  • Mysql:mysql-5.5

创建工程

  • New Project 选择Java Enterprise,然后选择项目本地存储路径
    创建工程

  • 创建工程之后,搭建目录结构
    工程目录结构
    Java的包名都是由小写单词组成的,类名的首字母都是大写,也就是我们常用的驼峰写法。而包路径一般都是对系统模板的定义与归类。
    在Java开发的过程中,每名Java开发人员都可以编写属于自己的java package,为了在编写中保证每一个java package命名的唯一性,要求开发人员在自己定义的包名前加上唯一的前缀。所以多数开发人员采用自己公司的名称.项目名.模块名...在互联网上的域名称作为自己程序包的唯一前缀。例如: com.sun.xml...,更多细节请点击

配置struts2,hibernate依赖

  • 将下载好的jar包复制进web-inf下的lib文件夹中,如果没有lib则创建这个文件夹。
    这里写图片描述

  • 配置依赖
    这里写图片描述
    图中出现的hibernate是后面的操作添加的
    这里写图片描述
    勾选添加进去的lib和tomcat,点击apply,等待IDE自动建立索引文件(此时IDE下方会出现indexing字样)

  • 编写配置文件
    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>    <constant name="struts.enable.DynamicMethodInvocation" value="false" />    <constant name="struts.devMode" value="true" /></struts>

hibernate.cfg.xml:配置hibernate具体操作传送门

<!--  ~ Hibernate, Relational Persistence for Idiomatic Java  ~  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.  --><!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.connection.driver_class">com.mysql.cj.jdbc.Driver</property>        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/chatroom?serverTimezone=Hongkong&amp;useUnicode=true&amp;characterEncoding=utf-8</property>        <!--<property name="hibernate.connection.url">jdbc:mysql://139.199.168.234:3306/labor?serverTimezone=Hongkong&amp;useUnicode=true&amp;characterEncoding=utf-8</property>-->        <property name="hibernate.connection.username">root</property>        <property name="hibernate.connection.password">root</property>        <!--配置hibernate信息-->        <!-- 输出底层sql语句 -->        <property name="hibernate.show_sql">false</property>        <!-- 输出底层sql语句格式 -->        <property name="hibernate.format_sql">false</property>        <!-- hibernate帮创建表,需要配置之后            update: 如果已经有表,更新,如果没有,创建        -->        <property name="hibernate.hbm2ddl.auto">update</property>        <!-- 配置数据库方言            在mysql里面实现分页 关键字 limit,只能使用mysql里面            让hibernate框架识别不同数据库的自己特有的语句         -->        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>    </session-factory></hibernate-configuration>

添加完hibernate.cfg.xml之后,IDEA给我们这样的提示:
这里写图片描述
我们点击create,即可添加hibernate模块。也就是modules中会出现之前没有的hibernate:
这里写图片描述
配置struts还需要在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_3_1.xsd"         version="3.1">    <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>
阅读全文
0 0
原创粉丝点击