在MyEclipse 8.6环境使用 Spring3 Struts2 Hibernate3建立一个最简单的SSH工程
Mysql测试表
CREATE TABLE `person` ( `id` int(11) NOT NULL AUTO_INCREMENT, `firstName` varchar(45) DEFAULT NULL, `lastName` varchar(45) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) )
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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 指定Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Hibernate3配置 --> <filter> <filter-name>Spring OpenSessionInViewFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>Spring OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- struts2配置 --> <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> </web-app>
Spring3的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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql:///test"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> <property name="annotatedClasses"> <list> <value>study.db.Person</value> </list> </property> </bean> <!-- AOP配置 --> <aop:config proxy-target-class="true"> <aop:pointcut id="managerOperation" expression="execution(* study..*DAO.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="managerOperation" /> </aop:config> <!-- 基本事务定义 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save*" /> <tx:method name="update*" /> <tx:method name="remove*" /> <tx:method name="delete*" /> <tx:method name="create*" /> <!-- other methods are set to read only --> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- struts2使用的action --> <bean id="personAction" scope="prototype" class="study.web.action.PersonAction"> <constructor-arg ref="PersonDAO" /> </bean> <!-- hibernate的数据库DAO --> <bean id="PersonDAO" class="study.db.PersonDAO"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans>
struts.xml 配置定义
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.devMode" value="true" /> <package name="person" extends="struts-default"> <!-- personAction定义在spring中 --> <action name="list" class="personAction" method="execute"> <result>pages/list.jsp</result> <result name="input">pages/list.jsp</result> </action> <action name="remove" class="personAction" method="remove"> <result>pages/list.jsp</result> <result name="input">pages/list.jsp</result> </action> <action name="save" class="personAction" method="save"> <result>pages/list.jsp</result> <result name="input">pages/list.jsp</result> </action> </package> </struts>
PersonAction.java 源程序
package study.web.action; import java.util.List; import study.db.Person; import study.db.PersonDAO; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.Preparable; public class PersonAction implements Preparable { private PersonDAO service; private List<Person> persons; private Person person; private Integer id; public PersonAction(PersonDAO service) { this.service = service; } @SuppressWarnings("unchecked") public String execute() { this.persons = service.findAll(); return Action.SUCCESS; } public String save() { this.service.save(person); this.person = new Person(); return execute(); } public String remove() { // service.remove(id); return execute(); } public List<Person> getPersons() { return persons; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public void prepare() throws Exception { if (id != null) person = service.findById(id); } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
JSP网页表单调用Struts2中的action
register.jsp
<%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> </head> <body> <div style="width: 300px; border-style: solid"> <p> Person Data </p> <s:form action="save" > <s:textfield id="id" name="person.id" /> <s:textfield id="firstName" label="First Name" name="person.firstName" /> <s:textfield id="lastName" label="Last Name" name="person.lastName" /> <s:submit /> </s:form> </div> </body> </html>
list.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <p> Persons </p> <s:if test="persons.size > 0"> <table> <s:iterator value="persons"> <tr id="row_<s:property value="id"/>"> <td> <s:property value="firstName" /> </td> <td> <s:property value="lastName" /> </td> <td> <s:url id="removeUrl" action="remove"> <s:param name="id" value="id" /> </s:url> <s:a href="%{removeUrl}">Remove</s:a> <s:a id="a_%{id}">Edit</s:a> </td> </tr> </s:iterator> </table> </s:if>
Spring3整合Hibernate3时如果出现的org/objectweb/asm/Type异常
原因是Spring中的cglib-nodep-2.1.3.jar与Hibernate中的cglib-2.2.jar相冲突!
两种框架整合时Spring中的cglib-nodep-2.1.3.jar是必须的,取消Hibernate中的cglib-2.2.jar即可