继续前两篇文章[在struts2中使用JFreeChart ]和[在struts2中使用JasperReports ],这次是要从数据库中读取数据,再用图表的形式来显示,所以就需要整合struts2,spring2,hibernate3 Annotation,现在把遇到的问题整理出来,以备查。
1、首先要在web.xml中添加:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
添加spring的ContextLoaderListener,还有OpenSessionInViewFilter到web.xml中。
2、在struts.xml中添加:
<constant name="struts.objectFactory" value="spring" />
添加一个constant,声明struts2使用的objectFactory是spring,不过好象struts2默认已经是使用spring作为objectFactory了,不加这一行也是可以的。不过我看在struts2-core-2.0.11.jar中的default.properties中的struts.objectFactory = spring是用#注释掉的,很奇怪???
所以我就把它加到struts.xml中了。
3、就是添加spring的配置文件了
spring-datasource.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/LIYAN" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
</beans>
我用的是mysql,可以替换成其他database的连接。
spring-hibernate.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.liyanframework.chart.domain.Fruit</value>
<value>com.liyanframework.chart.domain.Sales</value>
</list>
</property>
</bean>
<!--bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/liyanframework/chart/Fruit.hbm.xml</value>
</list>
</property>
</bean-->
</beans>
现在jdk早就支持Annotation了,所以我这里就是使用的hibernate的Annotation,省去了许多麻烦的hbm.xml文件了,同时也把普通的sessionFactory的配置晒出来了。
4、以下是domain object和sql script的相关文件
Sales.java代码:
package com.liyanframework.chart.domain;
import java.io.Serializable;
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="T_SALES")
public class Sales implements Serializable {
private Long id;
private String month;
private int amount;
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="SALES_AMOUNT")
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
@Column(name="SALES_MONTH")
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
}
数据库脚本:
CREATE TABLE IF NOT EXISTS T_SALES (
ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
SALES_MONTH VARCHAR(255) NOT NULL UNIQUE,
SALES_AMOUNT INTEGER NOT NULL
)ENGINE=INNODB;
ALTER TABLE T_SALES AUTO_INCREMENT = 20000;
5、最后就是要把相关的jar放到classpath里就可以了。
需要注意的一点是,一定要到struts2里面把struts2-spring-plugin-2.0.11.jar加进来,在struts2中,spring的引入采用了plugin的方式,不同与之前的webwork2,本人觉得这样更好呀。
除了jar以外的source code晒出来,给大家分享。
分享到:
相关推荐
struts2+spring2+hibernate3 Annotation整合例子
jquery easyUI+struts2+spring+hibernate基于annotation实现的CRM
整合S2SH+Freemarker+oscache,后台用Spring管理各个bean,Hibernate做数据库持久化,viewer用Freemarker。整合中对Struts2,Hibernate,Spring都采用Annotation进行注解类。
整合S2SH+Freemarker,后台用Spring管理各个bean,Hibernate做数据库持久化,viewer用Freemarker。整合中对Struts2,Hibernate,Spring都采用Annotation进行注解类。
Struts2+Spring2+Hibernate3+Annotation所需JAR包
Struts2.5.10+Spring4.3.4+Hibernate5.2.4整合所需要的jar包,包括xml配置方式和annotation配置方式所需要的jar包,本人已确认过,搭建起来实现简单的增删改查没有问题。本人也在学习阶段,若有问题请留言,互相学习...
struts2.18+hibernate3.3.2+spring2.56、annotation注解 全部打包 可以用
struts2+spring2.5+hibernate3.2 annotation配置完整eclipse项目,带数据库脚本
本Demo基于annotation的Struts2.1.8+Spring2.5.6+Hibernate3.3.2的整合
Struts1.3 + Hibernate3.3 + Spring3.0 Annotation整合
NULL 博文链接:https://wangmengbk.iteye.com/blog/839702
超级详细的SSH2项目实例详解,并且附带两个项目详解。两种注解实现方式。不同的生成数据脚本实现。 在JavaEE企业级开发中,以SSH2框架为核心的应用非常广,大象根据项目实践经验,通过二个实例,详细的为大家讲解...
struts2+spring+hibernate(实现XML和Annotation两种方式操作数据库) 项目描述: 框架及版本:struts2 + spring3.0 + hibernate3.3 所实现的功能说明 把两个对象存入到数据库中 1、一种方法采用XML文件映射的方式 2...
基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发
此项目是struts2 + hibernate3(annotation) + spring(annotation)整合后的项目雏形,基于MVC的设计思想,很适合初学ssh框架的童鞋参考
用struts2和spring3和hibernate实现的一个登录的示例