SSH(一):环境搭建

      SSH为struts+spring+hibernate的一个集成框架,是目前较流行的一种Web应用程序开源框架。大多数公司都在使用。

     集成SSH框架的系统从职责上分为三层:表示层业务逻辑层数据持久层,以帮助开发人员在短期内搭建结构清晰、可复用性好、维护方便的Web应用程序。其中使用Struts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转;利用Hibernate框架对持久层提供支持,处理请求数据并返回结果;Spring则是做管理,管理struts和hibernate。

     不管是什么项目,当然,前提是一个新的项目,我们想要使用SSH框架进行开发,需得做以下五件事。其中前三个是必须要做的,后两个则是为了快速且清晰开发,方便管理而可选加入的。

     主要步骤:

一、新建web工程

二、搭建框架环境

三、整合SSH

四、资源分类

五、配置日志

 

一、新建web工程

     通过不同的IDE新建一个web工程,这里需要注意的是养成一个很好的习惯,新建完项目后首先去设置器编码格式为UTF-8,从一定程度上避免乱码问题。

二、搭建框架环境

1.struts2


     Struts作为系统的整体基础架构,负责MVC的分离。那么它到底是如何实现MVC分离的呢?

     M(模型):这个一般不由Struts来做;V(视图):视图也不算struts的强项,但是struts提供优秀的标签来支持视图的展示,利用标签,可以将数据合理的展示给用户;C(控制器):struts 的重要功能,提供struts的过滤器,拦截用户的请求,查找struts配置文件,为其匹配一个对应的Action,这个Action负责调用模型,获得数据,然后对数据做部分处理,接着Action再将处理后的数据,为其选择一个视图进行输出。

     知道原理了,再来看看如何配置struts吧。


(1)所需jar包



(2)配置文件

struts.xml

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.1.7.dtd">  
  5. <struts>  
  6.   
  7.     <!-- 配置为开放模式:配置文件改了以后不用重新启动 -->  
  8.     <constant name="struts.devMode" value="true" />  
  9.   
  10.     <!-- 扩展名配置为action -->  
  11.     <constant name="struts.action.extension" value="action" />  
  12.   
  13.     <!-- 把主题配置为simple -->  
  14.     <constant name="struts.ui.theme" value="simple" />  
  15.   
  16.   
  17.     <package name="default" namespace="/" extends="struts-default">  
  18.   
  19.         <!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 -->  
  20.         <action name="test" class="cn.itcast.oa.PersonAction">  
  21.             <result name="success">/test.jsp</result>  
  22.         </action>  
  23.   
  24.     </package>  
  25.   
  26. </struts>  

Web.xml

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.   
  7.     <!-- 配置struts2 :注意配置struts2的filter标签要放到所有filter标签的最下面,否则会有问题。-->  
  8.     <filter>  
  9.         <filter-name>struts2</filter-name>  
  10.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  11.     </filter>  
  12.   
  13.     <filter-mapping>  
  14.         <filter-name>struts2</filter-name>  
  15.         <url-pattern>*</url-pattern>  
  16.     </filter-mapping>  
  17.   
  18. </web-app>  

     struts配置完成了之后,我们可以对其先进行一下测试。personAction类中代码如下:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package cn.itcast.oa;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5.   
  6. public classpersonAction extends ActionSupport {  
  7.   
  8.   
  9.     @Override  
  10.    
  11.     public String execute() throws Exception {  
  12.   
  13.         System.out.println("调到action了");  
  14.      
  15.         return"success";  
  16.   
  17.     }  
  18.   
  19. }  

     通过在浏览器地址栏直接访问action:http://localhost:8080/ItcastOA/test.action,我们可看到浏览器的成功页面:


     控制台也打印出相应信息:

 


2. Hibernate


     说完struts的配置,接下来说说Hibernate吧。大家都知道,Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得程序员可以随心所欲的使用对象编程的思维来操纵数据库。它的配置也很简单:


(1)所需jar包

 


(2)配置文件

hibernate.cfg.xml

*.hbm.xml

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5. <hibernate-configuration>  
  6.   
  7.     <session-factory>  
  8.         <!--1. 数据库连接信息-->  
  9.         <property name="dialect">  
  10.             org.hibernate.dialect.MySQLDialect  
  11.     </property>  
  12.         <property name="connection.driver_class">  
  13.             com.mysql.jdbc.Driver  
  14.         </property>  
  15.         <property name="connection.url">  
  16.             jdbc:mysql://localhost:3306/oa0909  
  17.         </property>  
  18.         <property name="connection.username">admin</property>  
  19.         <property name="connection.password">123456</property>  
  20.   
  21.         <!--2. 其他配置-->  
  22.         <property name="myeclipse.connection.profile">mysql</property>  
  23.         <property name="show_sql">true</property>  
  24.         <property name="hbm2ddl.auto">update</property>  
  25.   
  26.         <!--3. 导入映射文件-->  
  27.         <!-- <mapping resource="Person.hbm.xml" />-->  
  28.         <mapping resource="cn/itcast/oa/domain/Role.hbm.xml" />  
  29.         <mapping resource="cn/itcast/oa/domain/User.hbm.xml" />  
  30.     </session-factory>  
  31.   
  32. </hibernate-configuration>  

     关于Hibernate的应用,和使用c3p0配置数据库连接池等在此不做过多介绍,后面会有专门的文章进行讲解。


3. Spring


     Struts和Hibernate都已经搭建完成了,现在就剩下起到管理作用的Spring。因为spring基于IoC(Inversion of Control,反向控制)和AOP构架的多层j2ee系统的框架,但它不强迫你必须在每一层中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;采用IoC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现事务管理(TranscationManagment)。关于Spring的配置如下:


(1)所需jar包



(2)配置文件

applicationContext.xml

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.                            http://www.springframework.org/schema/aop   
  8.                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  9.                            http://www.springframework.org/schema/context   
  10.                            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11.                            http://www.springframework.org/schema/tx   
  12.                            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  13.     <!-- 自动扫描与装配bean -->  
  14.     <context:component-scan base-package="cn.itcast.oa"></context:component-scan>  
  15.   
  16. </beans>  

     在这里,我们采用注解配置Bean的方式。在applicationContext.xml中加入自动扫描后,我们还需在那些我们想交给Spring管理的类上加上相应的注解,如@Component,@Service,@Controller,@Repository。

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package cn.itcast.oa.view.action;  
  2.   
  3. import org.springframework.context.annotation.Scope;  
  4. import org.springframework.stereotype.Controller;  
  5.   
  6. import com.opensymphony.xwork2.ActionSupport;  
  7.   
  8. @Controller  
  9. @Scope("prototype")  
  10. public class PersonAction extends ActionSupport {  
  11.   
  12.     @Override  
  13.     public String execute() throws Exception {  
  14.         System.out.println("调到action了");  
  15.         return "success";  
  16.     }  
  17. }  

     对Spring的测试,内容如下:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package cn.itcast.oa.test;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. import cn.itcast.oa.view.action.PersonAction;  
  8.   
  9.   
  10. public class PersonActionTest {  
  11.   
  12.     private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");  
  13.       
  14.     @Test  
  15.     public void testBean(){  
  16.         PersonAction personAction= (PersonAction) ac.getBean("personAction");  
  17.         System.out.println(personAction);  
  18.     }  
  19.       
  20. }  


运行该测试类后,结果为:



    

    至此,三个环境全部搭建完成了,但是现在他们还是彼此独立的存在,如何将他们整合起来,更好的进行开发呢?下篇再继续~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值