您的位置:首页 > 其它

如何创建一个SSH项目

2016-07-31 11:04 417 查看

前言

    SSH框架作为现如今Java的流行框架,必然有它相应的理由。那么在如何创建一个SSH项目呢?创建项目时需要注意哪些情况呢?下面这篇博客就介绍一下如何创建一个SSH项目。

创建过程

1、创建一个WEB项目

     创建一个Web Project,并设置编码格式为UTF-8,创建Source Folder,src文件夹放置项目源码,config文件夹放置项目的配置文件,test文件夹放置项目的单元测试类。在WebRoot文件夹下创建style、css和images文件夹,分别放置js文件、css样式文件和所需的图片。如果按照模块分类,可以在各个模块的文件夹下分别创建这三个文件夹。最后在WEB-INF文件夹下创建jsp文件夹,放置项目的jsp页面。

2、考虑项目架构

    创建完项目之后,需要考虑项目采用什么样的架构,是用三层还是三层加接口,或者别的架构。然后在src 文件夹下,创建符合自己架构的包结构,例如实体类为cn.tgb.domain,service层为cn.tgb.service,dao层为cn.tgb.dao。

3、添加jar包

    1)、添加单元测试jar包

    junit4.9.jar

    2)、struts2

    利用struts-2.3.15.1.jar,将jar包中../apps/struts2-blank.war\WEB-INF\lib路径下的所有jar包*.jar添加到项目的WEB-INF/lib下。

    3)、spring

    首先是spring的核心jar包:../dist/spring.jar

    然后是aspect的jar包:../lib/aspectj/*.jar(共2个)

    第三个是CGLIB的jar包:../lib/cglib/cglib-nodep-2.1_3.jar

    第四个是spring的日志jar包:../lib/jakarta-commons/commons-logging.jar

    最后是工具jar包:commons-codec.jar,commons-lang.jar

    4)、Hibernate

    首先添加Hibernate的核心jar包:..//hibernate3.jar

    然后添加Hibernate的依赖jar包:../lib/required/*.jar、../lib/jpa/hibernate-jpa-2.0-api-1.0.0.Final.jar

    最后添加数据库驱动、日志和c3p0的jar包:mysql-connector-java-5.1.5-bin.jar、log4j-1.2.15.jar、c3p0-0.9.1.2.jar。

    添加日志jar包时,我们可能会采取不同的方式,就会需要不同的jar包。用jar包的话,会自动输出日志,但是可能也想在项目中会输出一些自己的日志,这时就涉及到另外一个jar包,slf4j-log4j12-1.5.8.jar,这个jar包是实现的log4j方式的jar包,同时引入它和log4j的jar包,就可以以log4j的方式输出你自己的日志。另外,它也有对其他日志类型实现的jar包。

4、配置文件

     1)、web.xml

     在web.xml中配置struts2的过滤器

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


    2)、struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 设置开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- 把action扩展名配置为action -->
<constant name="struts.action.extension" value="action" />
<!-- 指定主题使用simple(默认值为xhtml) -->
<constant name="struts.ui.theme" value="simple" />
<!-- 定义全局的国际化资源文件 -->
<constant name="struts.custom.i18n.resources" value="MessageResources"></constant>

<!-- 可以把相关的action都放在一个package中,以便于管理,形成模块 -->
<package name="default" extends="struts-default">

</package>
</struts>


    3)、hibernate.cfg.xml

<!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 其他配置 -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>


    4)、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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置bean的自动扫描与装配 -->
<context:component-scan base-package="cn.itcast.oa"></context:component-scan>


   

     5)、logfj.properties

    这些xml的配置,在各自的jar包中都有相应的例子,从例子里面拷贝一份出来,把公用部分留下,添加数据自己项目的个性化的内容就行了。

5、整合框架

    1)、继承spring和hibernate

    在applicationContext.xml文件中配置SessionFactory的相应内容,和jdbc连接数据库的信息,以及声明式事务的配置。

---------------------- applicationContext.xml ------------------------
<!-- 配置SessionFactory(整合Hibernate) -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库连接信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置 -->
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="5"></property>
<property name="minPoolSize" value="3"></property>
<property name="acquireIncrement" value="2"></property>
<property name="maxStatements" value="8"></property>
<property name="maxStatementsPerConnection" value="5"></property>
<property name="maxIdleTime" value="20"></property>
</bean>
</property>
<!-- 指定hibernate的配置文件的位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 导入外部的properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 配置声明式事务管理(采用注解的方式) -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>


外部的jdbc.properties文件中配置的是连接数据库的信息:

jdbcUrl		= jdbc:mysql:///itcast
driverClass	= com.mysql.jdbc.Driver
user		= root
password	= root
    

2)、集成struts和spring

    首先添加一个jar包:struts-2.1.8.1/lib/struts2-spring-plugin-2.1.8.1.jar,从相应版本下的struts的jar包的这个路径下找就可以。

    在web.xml中配置spring的监听器:

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext*.xml</param-value>
</context-param>


总结

    到此为止,SSH框架的项目就创建完成了,可以添加自己的类和页面,创建数据库进行测试了。我在测试时发现,页面内容在浏览器显示时乱码了,检查后各个地方的编码,jsp页面的编码是utf-8,创建类时编码同样也是utf-8,而且换了浏览器之后发现还是乱码。这时就有可能是tomcat的编码问题了,可以在tomcat的server.xml中,在相应的端口下配置编码格式,这样就不会乱码了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ssh