当前位置:编程学习 > 网站相关 >>

SSH Web工程环境搭建总结

SSH(Struts+Spring+Hibernate)框架是最常用的框架之一,在搭建SSH框架的时候总有人遇到这样,那样的问题。以前一直用的是Struts+Hibernate做项目,今天第一次搭建SSH环境,也遇到了很多问题,下面我介绍一下SSH框架搭建的全过程,以供后面学习的人少走点弯路,照我的做法一步一步的做就没有什么问题了。
第一步:准备工作:Eclspe的安装以及安装Hibernate、Spring插件在这里我就不多讲了,最简单的方式是在Eclspe的Help菜单中有个Eclspe Maketplace菜单项,点击进去就可以看到Spring、JBoss之类的Makets,单Install就会安装了,只要安提示来就好了。下载好Struts、Spring、Hibernate,目前最新的应该是Struts2.3.7、Spring3.1.2、Hibernate4.2.0,后面我也会将里面jar的包上传到资源库中,大家方便下。
第二步:项目配置
    1、新建Web项目,名字自己取 如:FirstSSHDemo
        2、在WebContent/WEB-INF目录下创建或修改(如果系统自动创建好的)web.xml文件,增加Struts2拦截器,如下代码:
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 
 
    <!-- 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> 
 
 
</web-app> 
    3、在Java Resources/src目录下建立Struts的配置文件struts.xml 内容如下:
 
<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
"http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
    <package namespace="/" name="ecshop_pro" extends="struts-default"> 
<!-- action根据自己的需求来配置 --> 
        <action name="index" > 
            <result name="input">index.jsp</result> 
        </action> 
    </package> 
<!-- 以下是我自己工程的东西搭环境时将include这些全部删除,预留下的意思是希望大家以后这样来配置,每个功能模块一个xml配置,然后将其include进来就好了  --> 
    <include file="front.xml" /> 
    <include file="login.xml" /> 
    <include file="user.xml" /> 
    <include file="goods.xml" /> 
    <include file="order.xml" /> 
    <include file="article.xml" /> 
    <include file="shopping.xml" /> 
</struts>  
   4、配置Spring环境,在这里我们先讲配置文件的写法,后面统一的引入三个框架所需要的Jar包。我们在前面的web.xml中增加Spring的监听器,代码如下
 
<!-- Spring Framework 说明一下:我这里是将applicationContext.xml放在/WEB-INF目录中,所以<param-value>是这种写法 如果把它放在Java Resources/src 下 将/WEB-INF改成classpath:即可 --> 
    <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
   5、在第四步中我们已经看到了applicationContext.xml,这个就是Spring的配置文件了,
在WebContent/WEB-INF目录下创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:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="  
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 
    <!-- Action 我这里配置了一个后台登陆的Action Bean 这里需要注意一下scope这个属性,
它有两个值:prototype和singleton,在使用prototype值是没有什么需注意的,
使用singleton时如果配置property,那么需要注意的是property 的name的值必须是Action中需要依赖注入的那个
变量,也就是指向Bo或Dao或Service层相应实现类的变量;ref指定了属性对BeanFactory中其他Bean的引用关系,
必须为其他Bean的id --> 
     <bean id="loginAction" scope="prototype" class="com.ecshop.action.LoginAction">  
          <property name="IAdminDao" ref="ado"></property>  
    </bean> 
     
    <!-- Dao --> 
        <!-- id为Action中的变量 --> 
    <bean id="ado" class="com.ecshop.dao.impl.HAdminDao"></bean> 
     
    <!-- sessionFactory --> 
    <bean id="sessionFactory" 
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
        <property name="configLocation"> 
            <value>classpath:/hibernate.cfg.xml</value> 
<!-- 这里注意一下上面的value标签内的值,这是配置Hibernate配置文件的地址和文件名, 我的hibernate.cfg.xml
是放在Java Resouses/src根目录中所以这样写,如果只写hibernate.cfg.xml 会报BeanCreationException和FileNotFondException --> 
        </property&g
补充:Web开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,