【分享】基于全注解的SSH简单框架

aimsam 2012-04-20 05:18:25
以前用的框架太老了,而且是基于配置文件的,所以重新建了一个几乎是最新版本的SSH框架。除了没用Hibernate4.0以外

文字没变色,不知道怎么回事,想看变色的移步我的博客吧http://www.wuliaoji.com/2012/04/20/%E5%9F%BA%E4%BA%8E%E5%85%A8%E6%B3%A8%E8%A7%A3%E7%9A%84ssh%E7%AE%80%E5%8D%95%E6%A1%86%E6%9E%B6.html

整个框架25MB左右。


1,
hibernate版本hibernate-distribution-3.6.10.Final
struts版本struts-2.3.1.2
spring版本spring-framework-3.1.1.RELEASE
aspectj版本aspect-1.6.12(用来产生动态代理的,所谓面向切面编程)

2,
包的列表,这边就不分了吧。。spring是全导进去了的,hibernate复制了required文件下的,struts按需求导入,导多了要报错。
antlr-2.7.6.jar
aopalliance-1.0.jar
asm-3.3.jar
asm-all-3.2.jar
asm-commons-3.3.jar
aspectj-1.6.12.jar
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
commons-collections-3.1.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
freemarker-2.3.18.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-testing.jar
hibernate3.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
mysql-connector-java-5.1.19-bin.jar
ognl-3.0.4.jar
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.aspects-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.context.support-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.instrument-3.1.1.RELEASE.jar
org.springframework.instrument.tomcat-3.1.1.RELEASE.jar
org.springframework.jdbc-3.1.1.RELEASE.jar
org.springframework.jms-3.1.1.RELEASE.jar
org.springframework.orm-3.1.1.RELEASE.jar
org.springframework.oxm-3.1.1.RELEASE.jar
org.springframework.test-3.1.1.RELEASE.jar
org.springframework.transaction-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
org.springframework.web.portlet-3.1.1.RELEASE.jar
org.springframework.web.servlet-3.1.1.RELEASE.jar
org.springframework.web.struts-3.1.1.RELEASE.jar
slf4j-api-1.6.1.jar
struts2-config-browser-plugin-2.3.1.2.jar
struts2-convention-plugin-2.3.1.2.jar
struts2-core-2.3.1.2.jar
struts2-spring-plugin-2.3.1.2.jar
xwork-core-2.3.1.2.jar

3,
web.xml
<?xml version= "1.0" encoding ="UTF-8"?>
<web-app version= "2.5" xmlns ="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
<display-name></ display-name>
<welcome-file-list>
<welcome-file> index.jsp</welcome-file >
</welcome-file-list>

<!-- struts配置 -->
<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>

<!-- 加载spring监听器,用来在struts中注入 -->
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener</listener-class >
</listener>

<context-param>
<param-name> contextConfigLocation</param-name >
<param-value> classpath:applicationContext.xml</param-value >
</context-param>

</web-app>

4,
struts.xml
虽然说是全注解但是基本的struts还是要配置的
<?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.i18n.encoding" value ="UTF-8" />
<!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 <constant name="struts.action.extension"
value="action,do, htm"/> -->
<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name= "struts.configuration.xml.reload" value="true" />
<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name= "struts.devMode" value ="true" />
<!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name= "struts.serve.static.browserCache" value="false" />
<!-- 指定由spring负责action对象的创建 <constant name="struts.objectFactory" value="spring"
/> -->
<!-- 是否开启动态方法调用 -->
<constant name= "struts.enable.DynamicMethodInvocation" value="true" />
</struts>

5,
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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.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" >

<!-- 注解支持 -->
<context:annotation-config />
<!-- 扫描位置 -->
<context:component-scan base-package="com.kwgl" ></context:component-scan>

<!-- 配置数据源 -->
<bean id= "dataSource"
class= "org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name= "driverClassName">
<value> com.mysql.jdbc.Driver</value >
</property>

<property name= "url">
<value> jdbc:mysql://localhost:3306/kwgl?useUnicode=true&characterEncoding=utf8 </value>
</property>
<property name= "username">
<value> root</ value>
</property>
<property name= "password">
<value></ value>
</property>
</bean>

<!-- 配置sessionFactory -->
<bean id= "sessionFactory"
class= "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name= "dataSource">
<ref local= "dataSource" />
</property>
<!-- model class -->
<property name= "annotatedClasses">
<list>
<value> com.kwgl.po.TbColumn</value >
<value> com.kwgl.po.TbMan</value >
<value> com.kwgl.po.TbProject</value >
<value> com.kwgl.po.TbValue</value >
</list>
</property>
<property name= "hibernateProperties">
<props>
<!-- 配置Hibernate的方言 -->
<prop key= "hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key= "hibernate.hbm2ddl.auto" >update </prop>
<!-- 输入由Hibernate生成的SQL语句,如果在hibernate.cfg.xml中也指定的话,会生成两条语句,在产品中最好关闭,即设为false -->
<prop key= "hibernate.show_sql">true</prop >
</props>
</property>
</bean>

<bean id= "hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<property name= "sessionFactory" ref ="sessionFactory"></ property>
</bean>

<!-- 配置事务管理 -->
<bean id= "transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "sessionFactory" ref ="sessionFactory" />
</bean>

<!-- advice -->
<tx:advice id= "txAdvice" transaction-manager ="transactionManager">
<tx:attributes>
<tx:method name= "add" propagation ="REQUIRED"/>
</tx:attributes>
</tx:advice>

<!-- aop -->
<aop:config>
<aop:pointcut id= "userService" expression ="execution(public * com.kwgl.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref= "userService"/>
</aop:config>

</beans>


5,
配置完毕!就是这么简单,贴一点实现代码。只贴实现层了,jsp页面略
注解的使用方法搜索一下就有了
这一配置完可以去 http://127.0.0.1:8080/项目名/config-browser/actionNames.action这个地址查看当前你到底建立了哪些Action方便吧

action层




文字太长。。实现略。博客里有



...全文
344 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
aimsam 2012-04-28
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

楼主贴下你的博客链接吧。。
[/Quote]
地址我改了一下。。链接是这个http://www.wuliaoji.com/20120420.html
授捕鸟 2012-04-28
  • 打赏
  • 举报
回复
楼主贴下你的博客链接吧。。
悠悠-我心 2012-04-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

有点晕~~~
[/Quote]


慢慢看看
zqy880227 2012-04-21
  • 打赏
  • 举报
回复
谢谢lz
aa331730417 2012-04-21
  • 打赏
  • 举报
回复
mark正好也在练这个
aimsam 2012-04-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

有点晕~~~
[/Quote]
呀。。你都星星了还晕,我才晕。。我以前都没用过注解。。


分享一下省得新人到处翻文档配置了。。
菖蒲老先生 2012-04-20
  • 打赏
  • 举报
回复
有点晕~~~
llbupt 2012-04-20
  • 打赏
  • 举报
回复
正好想找个简单的系统进行学习,谢谢楼主了,顶一下

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧