您的位置:首页 > 其它

基于全注解的SSH简单框架

2012-09-21 11:41 381 查看
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层

package com.kwgl.action;import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.ExceptionMapping;

import org.apache.struts2.convention.annotation.ExceptionMappings;

import org.apache.struts2.convention.annotation.Namespace;

import org.apache.struts2.convention.annotation.ParentPackage;

import org.apache.struts2.convention.annotation.Result;

import org.apache.struts2.convention.annotation.Results;

import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Component;

import com.kwgl.po.TbProject;

import com.kwgl.service.TbProjectService;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(“struts-default”)

// 父包

@Namespace(“”)

@Results({

@Result(name = “exception”, location = “/admin/page/comm/exception.jsp”),

@Result(name = “getListSuccess”, location=”/admin/page/project.jsp”)

})

// @ExceptionMappings 一级声明异常的数组

// @ExceptionMapping 映射一个声明异常

@ExceptionMappings({

@ExceptionMapping(exception = “java.lang.Exception”, result = “exception”)})

//命名action名字

@Action(“tbProject”)

@Component

@Scope(“prototype”)

public class TbProjectAction extends ActionSupport {

/**

*

*/

private static final long serialVersionUID = 1L;

private TbProjectService tbProjectService;

private TbProject tbProject;

private List<TbProject> tbProjectList;

private String message;

/**

* 新增项目

* @return

* @throws Exception

*/

public String add() throws Exception {

tbProjectService.add(tbProject);

message = “新增成功”;

return getList();

}

/**

* 获得项目列表

* @return

* @throws Exception

*/

public String getList() throws Exception {

try {

this.tbProjectList = tbProjectService.getList();

} catch (Exception e) {

e.printStackTrace();

throw e;

}

return “getListSuccess”;

}

/** setters & getters **/

public TbProjectService getTbProjectService() {

return tbProjectService;

}

@Resource

public void setTbProjectService(TbProjectService tbProjectService) {

this.tbProjectService = tbProjectService;

}

public TbProject getTbProject() {

return tbProject;

}

public void setTbProject(TbProject tbProject) {

this.tbProject = tbProject;

}

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

public List<TbProject> getTbProjectList() {

return tbProjectList;

}

public void setTbProjectList(List<TbProject> tbProjectList) {

this.tbProjectList = tbProjectList;

}

}

dao实现层

package com.kwgl.dao.impl;import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;

import org.springframework.stereotype.Component;

import com.kwgl.dao.TbProjectDao;

import com.kwgl.po.TbProject;

@Component(“tbProjectDao”)

public class TbProjectDaoImpl implements TbProjectDao {

private HibernateTemplate hibernateTemplate;

public void add(TbProject tbProject) throws Exception {

this.hibernateTemplate.save(tbProject);

}

public void delete(TbProject tbProject) throws Exception {

this.hibernateTemplate.delete(tbProject);

}

public TbProject query(long id) throws Exception {

return this.hibernateTemplate.get(TbProject.class, id);

}

/** setters & getters**/

public HibernateTemplate getHibernateTemplate() {

return hibernateTemplate;

}

@Resource

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {

this.hibernateTemplate = hibernateTemplate;

}

@SuppressWarnings(“unchecked”)

public List<TbProject> getList() throws Exception {

// TODO Auto-generated method stub

return this.hibernateTemplate.find(“from TbProject”);

}

}

service实现层

package com.kwgl.service.impl;import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.kwgl.dao.TbProjectDao;

import com.kwgl.po.TbProject;

import com.kwgl.service.TbProjectService;

@Component(“tbProjectService”)

public class TbProjectServiceImpl implements TbProjectService {

private TbProjectDao tbProjectDao;

public void add(TbProject tbProject) throws Exception {

try {

tbProjectDao.add(tbProject);

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

throw e;

}

}

public void delete(TbProject tbProject) throws Exception {

tbProjectDao.delete(tbProject);

}

public TbProject query(long id) throws Exception {

return tbProjectDao.query(id);

}

public List<TbProject> getList() throws Exception {

// TODO Auto-generated method stub

return tbProjectDao.getList();

}

public TbProjectDao getTbProjectDao() {

return tbProjectDao;

}

@Resource

public void setTbProjectDao(TbProjectDao tbProjectDao) {

this.tbProjectDao = tbProjectDao;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: