这篇文章主要讲述SSH框架下如何整合AngularJS, JQuery Datatable 以及Bootstrap。
废话少说,进入主题,本篇主要以代码粘贴为主。
第一步:新建Maven项目,pom文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.boyi.web</groupId> <artifactId>ssh-datatabase-example</artifactId> <version>1.0.0</version> <packaging>war</packaging> <name>ssh-datatabase-example</name> <description>ssh-datatable-example</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.1.0.RELEASE</spring.version> <struts.version>2.3.15</struts.version> <hibernate.version>4.2.0.Final</hibernate.version> <mysql.version>5.1.6</mysql.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${struts.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>${struts.version}</version> </dependency> <!-- JASON Plug-in --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>${struts.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>${struts.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <!-- for JPA, use hibernate-entitymanager instead of hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.12</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.12</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.8.8</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.8.8</version> </dependency> </dependencies> <build> <finalName>ssh</finalName> </build> </project>
可以看出SSH的版本:
Spring : 3.1.0.RELEASE
Struts2: 2.3.15
Hibernate : 4.2.0.Final
第二步: 设置web.xml
<?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_3_0.xsd" version="3.0"> <display-name>ssh-datatable-example</display-name> <filter> <filter-name>CharacterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/dispatcher-servlet.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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>*.action</url-pattern> </filter-mapping> </web-app>
第三步:设置dispatcher-servlet.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:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:sec="http://www.springframework.org/schema/security" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>text/plain;charset=UTF-8</value> </list> </property> </bean> <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> </mvc:message-converters> </mvc:annotation-driven> <context:component-scan base-package="com.boyi.web"></context:component-scan> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3307/ssh?characterEncoding=UTF-8&characterSetResults=UTF-8" /> <property name="user" value="root" /> <property name="password" value="admin" /> <property name="maxPoolSize" value="100" /> <property name="minPoolSize" value="20" /> <property name="initialPoolSize" value="10" /> <property name="maxIdleTime" value="1800" /> <property name="acquireIncrement" value="10" /> <property name="idleConnectionTestPeriod" value="600" /> <property name="acquireRetryAttempts" value="30" /> <property name="breakAfterAcquireFailure" value="false" /> <property name="preferredTestQuery" value="SELECT NOW()" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg ref="dataSource"></constructor-arg> </bean> <!-- hibernate 需要的信息 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.boyi.web.entity</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <aop:aspectj-autoproxy expose-proxy="true" /> <tx:annotation-driven transaction-manager="txManager" /> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com..service..*.*(..))" /> </aop:config> </beans>第四步,准备css和js目录
1. 从JQuery官网下载最新的jquery.min.js ,copy到js目录,这里我下载的是jQuery v1.11.1
2. 从Bootstrap官网下载最新的bootstrap.min.js, bootstrap.min.css,分别copy到js和css目录中,这里我下载的是Bootstrap v3.3.0
3. 从Datatables官网下载最新的jquery.dataTables.min.js, dataTables.bootstrap.min.js,dataTables.bootstrap.css, 分别copy到js和css目录中,
目录结构如下:
第五步:写hibernate实体类
package com.boyi.web.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "PERSON") public class Person { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Column(name = "NAME") private String name; @Column(name = "BIRTHDAY") private String birth; @Column(name = "GENDER") private String gender; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBirth() { return birth; } public void setBirth(String birth) { this.birth = birth; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }
第六步:写hibernateDAO超类
package com.boyi.web.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.criterion.Criterion; public interface HqlBaseDao<T, PK extends Serializable> { int count(); /** * Persist the newInstance object into database. * @param newInstance to save * @return The identifier **/ PK save(T newInstance); /** * Save or update. * @param transientObject to save */ void saveOrUpdate(T transientObject); /** * Retrieve a persisted object with a given id from the database. * @param id to load * @return An object of type T */ T load(PK id); /** * Retrieve a persisted object with a given id from the database. * @param id to get * @return An object of type T */ T get(PK id); /** * Save changes made to a persistent object. * @param transientObject object to update **/ void update(T transientObject); /** * Remove the given object from persistent storage in the database. * @param persistentObject object to delete. **/ void delete(T persistentObject); /** * Remove the given object from persistent storage in the database. * @param s Query to execute * @return A query object **/ Query getQuery(String s); /** Deletes an object of a given Id. Will load the object internally so * consider using delete (T obj) directly. * @param id Id of record */ void delete(PK id); /** Delete object from disk. * @param persistentObject to delete * @param session to use * */ void delete(T persistentObject, Session session); /** Deletes an object of a given Id. Will load the object internally so consider using delete (T obj) directly. * @param id to delete * @param session to use */ void delete(PK id, Session session); /** * Loads the given Object. * @param id to load * @param session to use * @return an object of type T */ T load(PK id, Session session); /** * Loads the given Object. * @param id Id to load * @param session to use * @return An object of type T */ T get(PK id, Session session); /** Save object to disk using given session. * @param o to save * @param session to use * @return the id of the saved object * */ PK save(T o, Session session); /** Save or update given object. * @param o item to save. * @param session to use * */ void saveOrUpdate(T o, Session session); /** Update given object. * @param o item to update * @param session to use * */ void update(T o, Session session); /** Refreshes the object of type T. * @param persistentObject to refresh */ void refresh(T persistentObject); /** * Get a query handle. * @param s Query to use * @param session to use * @return Query object */ Query getQuery(String s, Session session); /** FindByExample. * @param exampleInstance to use * @param excludeProperty to exclude * @return A list of objects */ List<T> findByExample(T exampleInstance, String... excludeProperty); /** Returns a list of objects. * @return list of objects */ List<T> findAll(); List<T> findListForPage(String hql, Integer page, Integer size, Object... objects); /** Flushes the cache of the currently-used session. * */ void flush(); /** Object to evict from cache. * @param obj Object to evict */ void evict(Object obj); /** Hibernate wrapper. * @param criterion to filter. * @return list of objects */ List<T> findByCriteria(Criterion... criterion); }
package com.boyi.web.dao.impl; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Example; import org.springframework.beans.factory.annotation.Autowired; import com.boyi.web.dao.HqlBaseDao; public class HqlBaseDaoImpl<T, PK extends Serializable> implements HqlBaseDao<T, PK>{ /** Class type. */ Class<T> type; @Autowired SessionFactory sessionFactory; /** * Default bean constructor for spring. */ public HqlBaseDaoImpl() { // default constructor for spring } /** * Constructor. * * @param type * class type */ public HqlBaseDaoImpl(Class<T> type) { this.type = type; } /** * Helper functions. * * @return the currently set class */ public Class<T> getPersistentClass() { return this.type; } /** * Delete persistentObject from DB. * * @param persistentObject * object to delete. */ public void delete(T persistentObject) { getSession().delete(persistentObject); } /** * Deletes an object of a given Id. Will load the object internally so * consider using delete (T obj) directly * * @param id * Delete key */ public void delete(PK id) { getSession().delete(load(id)); } /** * Loads the given Object. * * @param id * to load * @return T Loaded object */ @SuppressWarnings("unchecked") public T load(PK id) { return (T) getSession().load(this.type, id); } /** * Loads the given Object. * * @param id * Id to load * @return An object of type T */ @SuppressWarnings("unchecked") public T get(PK id) { return (T) getSession().get(this.type, id); } /** * Item to save. * * @param o * object to save * @return PK */ @SuppressWarnings("unchecked") public PK save(T o) { return (PK) getSession().save(o); } /** * Item to refresh. * * @param o * object to refresh */ public void refresh(T o) { getSession().refresh(o); } /** * Item to saveOrUpdate. * * @param o * item to save. */ public void saveOrUpdate(T o) { getSession().saveOrUpdate(o); } /** * Update object. * * @param o * object to update */ public void update(T o) { getSession().update(o); } /** * Gets the current session in use (creates one if necessary). * * @return Session object */ public Session getSession() { return sessionFactory.getCurrentSession(); } /** * Get query. * * @param s * Query to execute. * @return Query object */ public Query getQuery(String s) { return getSession().createQuery(s); } /** * Get Session factory. * * @return SessionFactory Object */ public SessionFactory getSessionFactory() { return sessionFactory; } /** * Set session factory. * * @param sessionFactory * object */ @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } /** * Delete object. * * @param persistentObject * to delete * @param session * to use * */ public void delete(T persistentObject, Session session) { session.delete(persistentObject); } /** * Deletes an object of a given Id. Will load the object internally so * consider using delete (T obj) directly. * * @param id * to delete * @param session * to use */ public void delete(PK id, Session session) { session.delete(load(id)); } /** * Loads the given Object. * * @param id * to load * @param session * to use * @return an object of type T */ @SuppressWarnings("unchecked") public T load(PK id, Session session) { return (T) session.load(this.type, id); } /** * Loads the given Object. * * @param id * Id to load * @param session * Session to use * @return An object of type T */ @SuppressWarnings("unchecked") public T get(PK id, Session session) { return (T) session.get(this.type, id); } /** * Save object. * * @param o * to save * @param session * to use * @return the id of the saved object * */ @SuppressWarnings("unchecked") public PK save(T o, Session session) { return (PK) session.save(o); } /** * Save Or Update object. * * @param o * to save * @param session * to use. * */ public void saveOrUpdate(T o, Session session) { session.saveOrUpdate(o); } /** * Update record. * * @param o * to update * @param session * to use * */ public void update(T o, Session session) { session.update(o); } /** * GetQuery. * * @param s * to return * @param session * to use * @return Query object */ public Query getQuery(String s, Session session) { return session.createQuery(s); } /** * Wrapper around hibernate functions. * * @param criterion * to use * @return A list of matching objects */ @SuppressWarnings("unchecked") public List<T> findByCriteria(Criterion... criterion) { Session session = getSession(); Criteria crit = session.createCriteria(getPersistentClass()); for (Criterion c : criterion) { crit.add(c); } return crit.list(); } /** * FindAll. * * @return A list of all the objects */ public List<T> findAll() { return findByCriteria(); } /** * Flushes the cache of the currently-used session. * */ public void flush() { getSession().flush(); } /** * Object to evict from cache. * * @param obj * Object to evict */ public void evict(Object obj) { getSession().evict(obj); } /** * FindByExample. * * @param exampleInstance * to use * @param excludeProperty * to use * @return List of matching objects */ @SuppressWarnings("unchecked") public List<T> findByExample(T exampleInstance, String... excludeProperty) { Criteria crit = getSession().createCriteria(getPersistentClass()); Example example = Example.create(exampleInstance); for (String exclude : excludeProperty) { example.excludeProperty(exclude); } crit.add(example); return crit.list(); } public List<T> findListForPage(String hql, Integer page, Integer size, Object... objects) { Query query = getSession().createQuery(hql); if (objects != null) { for (int i = 0; i < objects.length; i++) { query.setParameter(i, objects[i]); } } if (page != null && size != null) { query.setFirstResult((page - 1) * size).setMaxResults(size); } @SuppressWarnings("unchecked") List<T> list = query.list(); return list; } @Override public int count() { Query query = getSession().createQuery("select count(*) from " + this.type.getName()); return ((Long)query.iterate().next()).intValue(); } }第七步:写hibernateDAO类
相关推荐
在IT行业中,"Structs2+Spring+Hibernate"(SSH)是经典的Java Web开发框架组合,这三大框架的整合能够构建高效、灵活且易于维护的企业级应用。下面将详细讲解这三个框架以及它们如何协同工作。 Struts2是MVC(模型...
2. **Action和结果配置**:Action是Structs2中的业务处理单元,开发者可以定义Action类来处理用户请求。通过配置文件或注解设定Action和结果(Result)的映射,实现业务逻辑和视图呈现的分离。 3. **自动数据绑定**...
下面我们将详细探讨`hibernate+structs2整合实例`的相关知识点。 首先,让我们了解Hibernate。Hibernate允许开发者用Java对象来表示数据库中的表,通过配置文件和注解来定义对象与表之间的映射关系。这使得开发者...
在"structs2拦截器实例计算action执行的时间"这个主题中,我们关注的是如何使用拦截器来测量Action执行的时间,这有助于优化代码性能和诊断问题。以下是实现这个功能的一般步骤: 1. **创建拦截器**:首先,你需要...
5. **JQuery与DOM转换** 在Structs2中,JQuery是一个常用的JavaScript库,用于处理DOM操作和AJAX请求。例如,你可以使用JQuery选择器获取元素,然后进行样式更改或事件绑定。与Structs2标签库配合使用,可以实现...
2. **Action与结果映射**:在Structs2中,Action类代表一个业务操作,而结果映射定义了Action执行后跳转到的视图或进一步的动作。Action的结果可以是另一个Action、URL、页面或者其他可配置的结果类型。 3. **拦截...
《Hibernate/Spring/Structs2 In Action》是一个涵盖了三个核心Java EE框架的综合学习资源,包括Spring、Hibernate和Struts2。这些框架是企业级Java应用程序开发中的关键组件,广泛用于构建可扩展、高效且易于维护的...
Structs2的核心理念是通过Action和Result来处理用户请求和响应,而配置文件(如struts.xml)则定义了这些Action的路由和行为。Structs2的拦截器机制允许开发者在Action执行前后插入自定义逻辑,如日志记录、权限检查...
这里我们将深入探讨Structs2与Servlet的差异,以及如何通过简单的入门程序来理解MVC模式。 首先,让我们了解两者的基本概念。Servlet是Java EE规范的一部分,它提供了一个接口,用于扩展服务器的功能,处理HTTP请求...
在Web开发中,jQuery的AJAX功能经常被用于与服务器进行异步数据交换,而Struts2作为一款流行的Java Web框架,提供了处理前端请求的能力。本文将深入探讨如何使用jQuery的AJAX方法发送JSON对象数组到Struts2的Action...
2. **Action与Result**:在Structs2中,Action是处理用户请求的实体,它接收请求参数并执行相应的业务逻辑。Result则是Action执行后的结果,负责将处理结果导向到指定的视图或者进行其他操作。 3. **Interceptor...
Structs2是Apache Struts框架的一个版本,这是一个基于MVC(Model-View-Controller)设计模式的Java Web应用开发框架。Struts2的核心目标是简化开发过程,提高代码的可维护性和可测试性。版本2.1.8是在Struts2系列中...
在编程领域,`structs2`通常指的是一个与数据结构和对象封装相关的概念,尤其是在Go语言中。Go语言中的`struct`是一种复合数据类型,它允许我们将多个不同的数据类型组合成一个单一的类型。`structs2`可能是指对这种...
这个"Structs2 Jar资源包"包含了77个jar文件,这些文件是Struts2框架的核心组件、插件和其他依赖库,用于实现完整的功能。 1. **Struts2核心框架**: Struts2的核心框架提供了一个处理HTTP请求和响应的基础架构。`...
9. **插件体系**:Struts2拥有丰富的插件支持,例如Struts2-dojo-plugin用于与Dojo JavaScript库集成,提供丰富的AJAX功能;Struts2-convention-plugin简化了Action的配置,基于类名和方法名自动映射。 10. **异常...
通过学习本入门教程,你应该已经掌握了Structs2的基本用法,包括路由、控制器、模板、中间件、数据绑定与验证、错误处理等方面。实践是最好的老师,现在你可以开始动手创建自己的Structs2 Web应用了。祝你在Go Web...
总的来说,这份"Structs2框架2016学习资料"将引导学习者逐步了解并掌握Structs2的配置、Action设计、数据绑定、拦截器机制、结果处理、视图渲染等多个方面,是提升Java Web开发技能的重要参考资料。同时,结合传智...
这个"Structs2jar包"很显然是Struts2框架的核心库文件,包含了许多用于构建和运行Struts2应用程序的类和资源。 在Struts2中,有几个关键概念和组件值得深入理解: 1. **Action**:Action是业务逻辑的载体,负责...
在"structs2资源包"中,我们关注的是Struts2的2.1.6版本,这是一个稳定且广泛使用的版本。 Struts2的核心在于它的Action类,它是业务逻辑处理的主要组件。Action类接收到用户的请求,执行相应的业务逻辑,并通过...