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

来源:互联网 发布:压弹钳子淘宝怎么搜 编辑:程序博客网 时间:2024/05/22 17:39

从零开始基于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=rootjdbc.password=123456jdbc.driverClass=com.mysql.jdbc.Driverjdbc.jdbcUrl=jdbc:mysql:///sshnewsjdbc.initPoolSize=5jdbc.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新闻发布系统第一天就到这里,如有错误欢迎指出。转载请留言。

2 0
原创粉丝点击