继续前两篇文章[在struts2中使用JFreeChart ]和[在struts2中使用JasperReports ],这次是要从数据库中读取数据,再用图表的形式来显示,所以就需要整合struts2,spring2,hibernate3 Annotation,现在把遇到的问题整理出来,以备查。
1、首先要在web.xml中添加:
添加spring的ContextLoaderListener,还有OpenSessionInViewFilter到web.xml中。
2、在struts.xml中添加:
添加一个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的代码:
我用的是mysql,可以替换成其他database的连接。
spring-hibernate.xml的代码:
现在jdk早就支持Annotation了,所以我这里就是使用的hibernate的Annotation,省去了许多麻烦的hbm.xml文件了,同时也把普通的sessionFactory的配置晒出来了。
4、以下是domain object和sql script的相关文件
Sales.java代码:
数据库脚本:
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>
<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 name="struts.objectFactory" value="spring" />
添加一个constant,声明struts2使用的objectFactory是spring,不过好象struts2默认已经是使用spring作为objectFactory了,不加这一行也是可以的。不过我看在struts2-core-2.0.11.jar中的default.properties中的struts.objectFactory = spring是用#注释掉的,很奇怪???
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>
<?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>
<?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;
- }
- }
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;
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晒出来,给大家分享。
- liyanFramework.rar (22 KB)
本文介绍如何在Struts2框架中整合Spring和Hibernate3Annotation,实现从数据库读取数据并展示的方法。主要步骤包括配置web.xml和struts.xml,设置数据源和SessionFactory,并提供相关实体类和数据库脚本。
1561

被折叠的 条评论
为什么被折叠?



