[ssh新闻发布系统一]搭建开发环境

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 从零开始基于struts2.3、hibernate4.3、spring4.2实现新闻发布系统。下面开始搭建开发环境,主要包括安装eclipse插件下载jar包配置struts、spring、hibernate一、安装eclipse插件在eclipse导航栏依次找到help->eclipse market。

从零开始基于struts2.3、hibernate4.3、spring4.2实现新闻发布系统。下面开始搭建开发环境,主要包括

  1. 安装eclipse插件
  2. 下载jar包
  3. 配置struts、spring、hibernate

一、安装eclipse插件

在eclipse导航栏依次找到help->eclipse market。

  1. 输入hibernate搜索hibernate tools工具,点击按照步骤安装。(hibernate tools是jboss tools的一款,如果在eclipse market搜索不到hibernate tools,搜索jboss tool,下一步选择安装hibernate tools即可)
  2. 输入spring搜索spring tool suite,按照提示点击安装。

二、下载jar包

到下面的地址下载好struts 2.3.4、spring 4.2.3、hibernate4.3.11、commons-logging、mysql驱动、c3p0jar包。

  1. struts 2下载地址 struts 2
  2. spring下载地址spring
  3. hibernate下载地址hibernate
  4. commons-logging下载地址 commons-logging,下载commons-logging-1.2-bin.zip
  5. mysql驱动下载地址mysql驱动
  6. c3p0地址地址[c3p0][http://mvnrepository.com/artifact/c3p0/c3p0]

三、新建web工程

新建一个dynamic web project,取名为sshnews,dynamic web module version选择2.5.
这里写图片描述
新建index.jsp,然后配置好tomcat跑一下,这一步相信大家都会。

四、导入jar包

spring所需要的jar包

打开spring-framework-4.2.3.RELEASE-dist\spring-framework-4.2.3.RELEASE\libs文件夹,复制以下jar包:
spring-aop-4.2.3.RELEASE.jar
spring-aspects-4.2.3.RELEASE.jar
spring-beans-4.2.3.RELEASE.jar
spring-context-4.2.3.RELEASE.jar
spring-context-support-4.2.3.RELEASE.jar
spring-core-4.2.3.RELEASE.jar
spring-expression-4.2.3.RELEASE.jar
spring-instrument-4.2.3.RELEASE.jar
spring-instrument-tomcat-4.2.3.RELEASE.jar
spring-jdbc-4.2.3.RELEASE.jar
spring-jms-4.2.3.RELEASE.jar
spring-messaging-4.2.3.RELEASE.jar
spring-orm-4.2.3.RELEASE.jar
spring-oxm-4.2.3.RELEASE.jar
spring-test-4.2.3.RELEASE.jar
spring-tx-4.2.3.RELEASE.jar
spring-web-4.2.3.RELEASE.jar
spring-webmvc-4.2.3.RELEASE.jar
spring-webmvc-portlet-4.2.3.RELEASE.jar
spring-websocket-4.2.3.RELEASE.ja

struts所需要的jar包

asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
freemarker-2.3.22.jar
javassist-3.11.0.GA.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
ognl-3.0.6.jar
struts2-core-2.3.24.jar
xwork-core-2.3.24.jar

hibernate所需要的jar包

将hibernate-release-4.3.10.Final\lib\required目录下的jar包全部copy到工程lib目录下.
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.10.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar

其他jar包

c3p0-0.9.1.2.jar
commons-logging-1.2.jar
mysql-connector-java-5.0.8-bin.jar
然后build path。

五、配置spring

配置web.xml

打开web.xml,因为安装了spring的插件,可以自动提示,按自动提示快捷键(windows下alt+/),找到ContextLoaderListener,配置代码会自动补全。
spring配置

新建spring配置文件

点击工程名,新建source folder(注意:不是folder),命名为conf,用来存放配置文件.在conf目录下新建Spring Bean Definition file,名称为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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

</beans>

如果生成的applicationContent.xml没有xmlns:context这些,可以手动补充上去。

修改web.xml

将web.xml中spring配置中的location改为:classpath:applicationContext.xml.

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContent.xml</param-value>
    </context-param>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

目前的工作空间是这样的:
这里写图片描述
以上步骤我们完成了导入jar包、新建资源文件夹、修改web.xml、新增applicationContent.xml。

六、设置c3p0数据库连接池

在conf目录下新建db.properties文件, 加入以下数据库连接信息

jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///sshnews
jdbc.initPoolSize=5
jdbc.maxPoolSize=10

这里写图片描述
mysql数据库中的sshnews是我们使用的数据库名,可以提前建好。
然后在applicationContent.xml加入以下代码:

    <!-- 导入资源文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 数据库连接配置 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>

这里写图片描述

七、spring整合hibernate

在conf目录下新建hibernate配置文件,单击conf文件夹名,右键,new->others,键入hibernate,新增hibernate配置文件
这里写图片描述
加入以下配置:

<!-- 配置hibernate基本属性 -->
    <session-factory>
        <!-- 方言 ctrl+shift+t 输入mysql5,找到org.hibernate.dialect.MySQL5InnoDBDialect -->
        <property name="hibernate.dialect"> org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <!-- 是否显示格式化sql -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
         <!-- 生成数据表的策略 -->
        <property name="hbm2ddl.auto">update</property>
    </session-factory>

这里写图片描述
在src目录下新建model包,我的包名是cn.ac.ucas.form, 然后修改applicationContent.xml,加入以下配置:

<!--  整合hibernate -->
<bean id="sessionFactorys" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource"></property>
       <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
       <property name="mappingLocations" value="classpath:cn/ac/ucas/form/*.hbm.xml">    </property>
</bean>

八、整合struts

配置web.XML

打开web.xml,加入下面代码:

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

加入struts.xml

拷贝struts-2.3.24-all/struts-2.3.24/apps/struts2-blank/WEB-INF/src/java目录下的struts.xml到src目录下。

九、测试

在cn.ac.ucas.form包下新建News类:

package cn.ac.ucas.form;

public class News {
    private Integer id;
    private String title;//新闻标题
    private String author;//新闻作者
    private String source;//新闻来源
    private String posttime;//发布时间
    private String content;// 新闻内容
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getSource() {
        return source;
    }
    public void setSource(String source) {
        this.source = source;
    }
    public String getPosttime() {
        return posttime;
    }
    public void setPosttime(String posttime) {
        this.posttime = posttime;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }


}

点击包名->右键->new->other,键入hibernate,新增hibernate xml mapping文件,然后默认下一步,finish。会在包下生成News类对应的hibernate映射文件。至此工程目录如下:
这里写图片描述
运行工程,在数据库中查看是否有新的数据库表生成。
这里写图片描述
Success!

总结:1.把需要的jar包全部下载好备用,会很方便
2.安装好eclipse插件会很方便
3.db.properties配置容易出错,查看参数是否写正确
4.不要忘记在数据库中新建数据库
ssh新闻发布系统第一天就到这里,如有错误欢迎指出。转载请留言。

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
JavaScript 网络安全
[ssh新闻发布系统五]删除新闻
删除新闻相对比较简单,传入新闻id执行删除操作。 一、Dao public void delete(Integer id) { String hql="Delete FROM News n WHERE n.
630 0
|
Web App开发 存储 Java
[ssh新闻发布系统三]存储新闻
一、存储新闻dao方法 在NewsDao.java中新增存储新闻的saveOrupdate方法 public void saveOrupdate(News news){ getSession().
630 0
|
Web App开发 JavaScript 前端开发
[ssh新闻发布系统四]使用富文本编辑器发布新闻
使用表单不能编辑新闻格式,这篇博客会介绍如何使用ueditor富文本编辑器来编辑新闻。使用ueditor编辑的新闻存储到数据库中的是html代码,并且带有css样式,在后台可以像编辑word一样编辑新闻。
962 0
|
Java 网络安全 数据库
[ssh新闻发布系统二] 读取新闻
上一篇博客已经配置好ssh的开发环境, 并生成了数据库表,这篇博客实现从数据库里面读取新闻数据到前台这一功能。 一、修改applicationContext.
772 0
|
3月前
|
Linux 网络安全
Linux命令(124)之ssh
Linux命令(124)之ssh
33 2
|
5月前
|
监控 数据可视化 安全
Linux——怎样使用SSH服务实现远程UI界面本地显示
需求场景 最近几天需要实现软件的远程监控,但是实际场景又不能使用向日葵、VNC、AnyDesk、以及其他的监视软件,并且软件的整体设计也没有这块的数据上行设计。
176 0
|
5月前
|
Linux 网络安全 开发工具
Linux之ssh
Linux之ssh
39 0
|
4月前
|
安全 网络协议 Linux
Linux ssh 命令详解
Linux ssh 命令详解
115 0
|
1月前
|
网络协议 Ubuntu Linux
「远程开发」VSCode使用SSH远程linux服务器 - 公网远程连接
「远程开发」VSCode使用SSH远程linux服务器 - 公网远程连接
111 0
|
1月前
|
安全 Linux Shell