您的位置:首页 > 编程语言 > Java开发

Struts2.3+spring3.1+hibernate4.0.1 搭建SSH框架

2012-06-26 13:17 288 查看
所谓的框架:就是把一些常用的功能封装好,省去重复的开发;
损失惨重啊,一个不小心,把硬盘分区表丢了,以前的数据全丢了,重头来,做个框架出来,留着以后用吧。

预计的功能:

用户管理
组织机构管理
模块管理
权限管理
工作流,jbpm,
cxf服务
oscache缓存

struts

(一) jar包

1. xwork-core-2.3.1.1.jar
2. struts2-json-plugin-2.3.1.1.jar
3. struts2-core-2.3.1.1.jar
4. struts2-convention-plugin-2.3.1.1.jar
5. struts2-config-browser-plugin-2.3.1.1.jar
6. ognl-3.0.3.jar
7. javassist-3.11.0.GA.jar
8. freemarker-2.3.18.jar
9. commons-logging-api-1.1.jar
10. commons-lang-2.5.jar
11. commons-io-2.0.1.jar
12. commons-fileupload-1.2.2.jar
13. asm-commons-3.3.jar
14. asm-3.3.jar

说明:
struts2-convention-plugin-2.3.1.1.jar为struts零配置所需的包;
struts2-config-browser-plugin-2.3.1.1.jar为struts协助开发需要的包:可以输入http://127.0.0.1:8686/config-browser/actionNames.action查看系统所有已经存在的action,配置不正确就可以在这里看出来;
freemarker-2.3.18.jar:模板需要的支持包;
struts2-json-plugin-2.3.1.1.jar:json插件包

(二) 配置文件

/src/default.properties

struts.i18n.reload = true
struts.devMode = true
struts.configuration.xml.reload = false
# struts.custom.i18n.resources = globalMessages
struts.action.extension = action,act,
struts.convention.package.locators.basePackage = com.gwtjs.struts.action
# struts.convention.action.packages = com.gwtjs.struts.action
## struts.Convention.exclude.packages = com.gwtjs.struts.action
struts.convention.action.suffix = Action
struts.convention.result.path = /
struts.convention.action.mapAllMatches=true

# struts.objectFactory = spring

web.xml

<!-- filter group -->
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
</filter>

<filter>
<filter-name>struts-execute</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>struts-execute</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
</listener>

(三) 测试

/struts/TestAction.java

public class TestAction extends ActionSupport {

private static final long serialVersionUID = -7938509668708524339L;
private String result;

@Override
public String execute() throws Exception {
result = "action success";
return SUCCESS;
}

public String getResult() {
return result;
}

}

/src/struts.xml

<package name="com.gwtjs.test" extends="struts-default" namespace="/test">
<action name="test1" class="com.gwtjs.struts.TestAction">
<result name="success">/test-content/test1.jsp</result>
</action>
</package>

/web/test-content/test1.jsp

<%@ taglib prefix="ss" uri="/struts-tags" %>
<body>
<h2><ss:property value="result"/></h2>
</body>

以上是有配置文件的测试,以下是无配置action测试

struts2-convention 测试、JSON返回测试:
ConventionTestAction.java

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("json-default") @Namespace(value="/test")
public class ConventionTestAction extends ActionSupport {

private static final long serialVersionUID = 7250554290060921960L;
public String success = "true";
public List<String> list = new ArrayList<String>();

@Override @Action(value="test2",results={@Result(name="success",type="json")})
public String execute() throws Exception {
return SUCCESS;
}

Freemarker返回测试:

Web.xml

<servlet>
<servlet-name>sitemesh-freemarker</servlet-name>
<servlet-class>org.apache.struts2.sitemesh.FreemarkerDecoratorServlet</servlet-class>
<init-param>
<param-name>default_encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>sitemesh-freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>

Struts.xml
<action name="test3" class="com.gwtjs.struts.TestAction" method="freeTest">
<result name="success" type="freemarker">/test-content/test1.ftl</result>
</action>
Test.java
public String freeTest() throws Exception {
result = "freemarker action success";
return SUCCESS;
}
Test.ftl
${result}

spring + hibernate

(一) jar包

增加spring 和Hibernate让我小折腾了一下,既然是新配置框架,就全用最新的,Hibernate我用的4.x,Spring 用的是3.1 ;Hibernate4.x的包是加进去了,但数据源配置等,还是用的3.x的,所以,狂报错,最后把hibernate3改成hibernate4就可以了,具体有两个地方需要调整:
第一个就是:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

第二个:
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
完整配置文件,下面会贴出来。

Hibernate的jar包还是差不多,把hibernate-release-4.0.1.Final\lib\required目录的jar 包,还有c3p0加进去;

spring就稍麻烦点:

org.springframework.aop-3.1.0.RELEASE.jar
org.springframework.asm-3.1.0.RELEASE.jar
org.springframework.aspects-3.1.0.RELEASE.jar
org.springframework.beans-3.1.0.RELEASE.jar
org.springframework.context-3.1.0.RELEASE.jar
org.springframework.core-3.1.0.RELEASE.jar
org.springframework.expression-3.1.0.RELEASE.jar
org.springframework.instrument-3.1.0.RELEASE.jar
org.springframework.jdbc-3.1.0.RELEASE.jar
org.springframework.jms-3.1.0.RELEASE.jar
org.springframework.orm-3.1.0.RELEASE.jar
org.springframework.oxm-3.1.0.RELEASE.jar
org.springframework.transaction-3.1.0.RELEASE.jar
org.springframework.web-3.1.0.RELEASE.jar

另外还有
ojdbc14.jar -- oracle数据库驱动
mysql-connector-java-5.1.8-bin.jar -- mysql驱动
aopalliance-1.0.jar
aspectjweaver.jar
asm-commons-3.3.jar

及spring测试需要的:
org.springframework.test-3.1.0.RELEASE.jar
可能还缺包,不过小事,这个时候缺包会有明确的提示的:一般都是NoClassDefFoundError: XXX按名称添加就可以了,详细稍后上传;

(一) Spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- ============================================== -->
<!-- =========== Spring 公用配置 =================== -->
<!-- ============================================== -->
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:com/gwtjs/config/hibernate.properties</value>
</property>
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 安全的路径jdbc:mysql://127.0.0.1:3306/va66?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&mysqlEncoding=utf8 -->
<property name="jdbcUrl" value="${hibernate.connection.url}" />
<property name="driverClass" value="${hibernate.connection.driver_class}" />
<property name="user" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
<property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
<property name="initialPoolSize" value="${c3p0.initialPoolSize}" />
<property name="minPoolSize" value="${c3p0.minPoolSize}" />
<property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
<property name="maxStatements" value="${c3p0.maxStatements}" />
<property name="numHelperThreads" value="${c3p0.numHelperThreads}" />
<property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<!--<property name="mappingResources">
<list>
<value>com/gwtjs/model/ACL.hbm.xml</value>
</list>
</property>
--><property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.autoReconnect">true</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" no-rollback-for="java.lang.Throwable" />
<!-- <tx:method name="*" read-only="true" /> -->
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.gwtjs.dao.impl.*.*(..))"
advice-ref="txAdvice" />
</aop:config>

<context:annotation-config />
<context:component-scan base-package="com.gwtjs" />

</beans>
hibernate.properties
######################
### Query Language ###
######################

## define query language constants / function names

hibernate.query.substitutions yes 'Y', no 'N'
hibernate.show_sql true

## select the classic query parser
#hibernate.query.factory_class org.hibernate.hql.classic.ClassicQueryTranslatorFactory

## MySQL
hibernate.dialect org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc\:mysql\://127.0.0.1\:3306/NewTest
hibernate.connection.username root
hibernate.connection.password 860748

## Oracle
## hibernate.dialect org.hibernate.dialect.OracleDialect
## hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
## hibernate.connection.username FoundationDB
## hibernate.connection.password FoundationDBPwd
## hibernate.connection.url jdbc:oracle:thin:@localhost:1521:orcl

## set the maximum depth of the outer join fetch tree

hibernate.connection.pool_size 1
hibernate.proxool.pool_alias pool1
hibernate.format_sql false

## add comments to the generated SQL
hibernate.use_sql_comments true

## set the maximum depth of the outer join fetch tree
hibernate.max_fetch_depth 1
hibernate.jdbc.batch_versioned_data true
hibernate.jdbc.use_streams_for_binary true
hibernate.cache.region_prefix hibernate.test
hibernate.cache.provider_class org.hibernate.cache.HashtableCacheProvider

c3p0.acquireIncrement=3
c3p0.idleConnectionTextPeriod=900
c3p0.minPoolSize=2
c3p0.maxPoolSize=50
c3p0.maxStatements=100
c3p0.numHelperThreads=10
c3p0.maxIdleTime=600
c3p0.initialPoolSize=3

(二) web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
这就是所谓的SSH框架了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: