当前位置: 代码迷 >> Eclipse >> 手工搭建eclipse环境上的struts2开发环境
  详细解决方案

手工搭建eclipse环境上的struts2开发环境

热度:71   发布时间:2016-04-23 02:26:35.0
手工搭建eclipse环境下的struts2开发环境
最近Myeclipse 8.0推出GA版,但感觉其安装文件越来越大了,很浪费系统资源,所以就尝试手工配置SSH2的开发环境。
一、配置STRUTS2
1、在apache网站上下载struts2的安装包。我使用的版本是struts-2.1.8.1
2、eclipse环境是eclipe-jee-galileo-win32,这具版本就是面向JAVA EE开发的。
3、解开struts2安装包,找到下面7个文件,拷贝到项目的lib目录下即可。
commons-io-1.3.2.jar
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
xwork-core-2.1.6.jar
freemarker-2.3.15.jar
struts2-core-2.1.8.1.jar
ognl-2.7.3.jar

这两个文件commons-io-1.3.2.jar、commons-fileupload-1.2.1.jar必须有,否则报错。

4、按照struts2要求,配置struts2.xml(这个文件结构在struts2安装包的样例文件中的例子程序中拷贝即可)
5、配置web.xml
<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>


6、此时就可以按照struts2语法,构建一个简单的struts2项目了。

二、配置SPRING2
1、将下列包拷贝到lib目录
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-web-2.5.6.jar
struts2-spring-plugin-2.1.8.1.jar
2、将log4j.properties拷贝到src目录下
3、在web.xml中配置spring的listener,用于启动spring的支持
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

到这步就可以用spring的功能了,具体的做法是在struts.xml和applicationContext.xml中配置相关的bean及action等等就可以了。

三、集成hibernate

1、将hibernate-distribution-3.3.2.GA-dist.zip压缩包中的hibernate3.jar以及lib目录中required目录中的jar包:
javassist-3.9.0.GA.jar
commons-collections-3.1.jar
antlr-2.7.6.jar
dom4j-1.6.1.jar
slf4j-api-1.5.8.jar

拷贝到项目的lib目录中。
2、在项目的src目录下创建hibernate.cfg.xml文件,具体内容类似如下配置:
<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>......</hibernate-configuration>

注:如果要在spring中集成hibernate,则不用配置hibernate.cfg.xml,直接在spring的applicationContext.xml中配置hibernate的相关内容。
类似如下代码:
<?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  <beans>   <bean id="mySessionFactory"    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">    <property name="configLocation">     <value>classpath:hibernate.cfg.xml</value>    </property>   </bean>   <bean id="myTransactionManager"    class="org.springframework.orm.hibernate3.HibernateTransactionManager">    <property name="sessionFactory">     <ref bean="mySessionFactory" />    </property>   </bean> 

然后配置mode.hbm.xml等实体关系,通过调用相关的hibernate的相关api即可。

以上配置均为手工配置,没有用myeclipse方式下的自动配置选项,主要是通过这些配置能够使我们更清晰的了解开源框架中各种包的具体作用。平时我们可以保留一份各框架的jar包集,遇到新建工程的时候,直接加入相关的支持即可。




  相关解决方案