前言
确实好久没有写过了,一直以来把写博文当作自己学习的总结,当作做过的笔记,随时都可以拿出来看看。不过最近习惯了用OneNote和印象笔记,所以就很少在论坛写博文。但是偶尔看到几篇被转载了,也小小的虚荣了下。这种虚荣大神们已经习以为常,他们是不能理解的。
好吧,转入正题。
参加工作从事Java Web开发将近两年,也没有完整的搭建出一个SSH框架的项目,说出来确实有点不好意思,并且前一段一个朋友问我搭建过SSH框架没,我都没有好意思说没有。所以才有了搭建一个SSH框架项目的想法,因此有了今天的这篇博文。
这篇博文的目的
- 尝试搭建一个完整的SSH框架项目。
- 给以后的自己,也给别人一个参考。
读博文前应该注意:
- 本文提纲:本文通过一个用户注册的实例讲解SSH的整合。创建Struts项目,整合Hibernate,整合Spring。最后总结如何熟练创建SSH项目。
- 仅是创建SSH项目,对于其他的扩展例如Struts的国际化,Hibernate的缓存优化,Spring的AOP等,本博文涉及不到。想学习更多的东西请搜索其他博文。
- 本项目的环境:Windows 8-64位,Eclipse Indigo Service Release 2,Tomcat 7.0,Struts-2.2.3.1,spring-framework-3.2.3.RELEASE(本来使用Spring3.1,但是整合Spring的时候启动服务器报错。错误信息竟然是Spring框架中的方法参数空指针。。。对比Spring3.2版本后,发现在3.2种增加了判断此参数是否为空,所以最终改为用Spring3.2.3的版本。详细错误信息见本目录下5),hibernate-release-4.0.0.CR6(整合Spring3时,可以启动服务器,但是点击注册后调用UserDao类的getHibernateTemplate().save(Obj)时浏览器页面报错,但是服务器没有反应。提示org.hibernate.SessionFactory中有没有openSession(Interceptor interceptor)
方法
,但是Spring框架中的代码Session session = (entityInterceptor != null ? sessionFactory.openSession(entityInterceptor) : sessionFactory.openSession())显示用到该方法,虽然调用这段代码的方法传进来的entityInterceptor就为null。。。网上搜索无果,所以最终在整合Spring的时候使用Hibernate-3.6.10.Final。详细错误信息见本目录下6),MySQL 5.5.28下的project数据库。
- user表的创建脚本。
- create table user(
- userId int auto_increment,
- userName varchar(16) not null,
- password varchar(16) not null,
- gender int not null,
- primary key(userId)
- );
- 使用Spring3.1时出现错误的详细信息。
- SEVERE: Context initialization failed
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1445)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:459)
- at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
- at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
- at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
- at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
- at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
- at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
- at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
- at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
- at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
- at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)
- at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
- at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
- at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
- at java.util.concurrent.FutureTask.run(FutureTask.java:166)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
- at java.lang.Thread.run(Thread.java:722)
- Caused by: java.lang.NullPointerException
- at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.scanPackages(LocalSessionFactoryBuilder.java:138)
- at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:294)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1504)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1442)
- ... 20 more
-
- 七月 13, 2013 6:48:44 下午 org.apache.catalina.core.StandardContext listenerStart
- SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1445)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:459)
- at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
- at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
- at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
- at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
- at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
- at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
- at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
- at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
- at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
- at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)
- at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
- at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
- at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
- at java.util.concurrent.FutureTask.run(FutureTask.java:166)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
- at java.lang.Thread.run(Thread.java:722)
- Caused by: java.lang.NullPointerException
- at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.scanPackages(LocalSessionFactoryBuilder.java:138)
- at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:294)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1504)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1442)
- ... 20 more
- 整合Spring时候,使用Hibernate时点击注册报错信息。
创建Struts项目
创建工程
- 在Eclipse中创建一个Dynamic Web Project,本文项目名称为SSHProject。
- 把Struts内apps文件夹下的struts2-blank war包用压缩工具解压,然后把WEB-INF/lib内的jar包和WEB-INF下的web.xml文件分别复制到SSHProject项目下的WEB-INF/lib内和WEB-INF下。
- 在src目录下创建相应package。
- 在WebContent下创建Register.jsp文件,并分别创建注册成功和注册失败的Success.jsp和Fail.jsp。
操作完成后的结构如图所示:
编写代码
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
-
- <display-name>SSH Project</display-name>
-
- <filter>
- <filter-name>SSH</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>SSH</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
-
- </web-app>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>User Register Page</title>
- </head>
- <body>
-
- <s:form action="register">
- <s:textfield name="user.username" label="用户名"></s:textfield>
- <s:password name="user.password" label="密码"></s:password>
- <s:select list="#{'1':'男','0':'女'}" listKey="key" listValue="value"
- name="user.gender" label="性别" value="1"></s:select>
- <s:submit value="注册"></s:submit>
- </s:form>
-
- </body>
- </html>
- package com.ynwi.ssh.beans;
-
- public class UserForm {
-
- private String username;
- private String password;
- private int gender;
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public int getGender() {
- return gender;
- }
-
- public void setGender(int gender) {
- this.gender = gender;
- }
-
- }
- 注册Action类RegisterAction.java的内容。
- package com.ynwi.ssh.action;
-
- import com.opensymphony.xwork2.ActionSupport;
- import com.ynwi.ssh.beans.UserForm;
- import com.ynwi.ssh.service.UserManager;
- import com.ynwi.ssh.serviceImpl.UserManagerImpl;
-
- public class RegisterAction extends ActionSupport {
-
- private static final long serialVersionUID = 1L;
-
- private UserForm user;
-
- private UserManager userManager;
-
- public UserForm getUser() {
- return user;
- }
-
- public void setUser(UserForm user) {
- this.user = user;
- }
-
- public UserManager getUserManager() {
- return userManager;
- }
-
- public void setUserManager(UserManager userManager) {
- this.userManager = userManager;
- }
-
- public String execute() {
- try {
- this.setUserManager(new UserManagerImpl());
- userManager.regUser(user);
- return SUCCESS;
-
- } catch (Exception e) {
- e.printStackTrace();
- return ERROR;
- }
- }
-
- }
- 验证文件RegisterAction-validation.xml的内容。该xml文件为RegisterAction的验证文件,命名格式固定,为< ActionName >-validation.xml。
- <?xml version="1.0" encoding="GBK"?>
- <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
- "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
-
- <validators>
-
- <field name="user.username">
- <field-validator type="requiredstring">
- <param name="trim">true</param>
- <message>用户名不能为空</message>
- </field-validator>
- <field-validator type="regex">
- <param name="expression"><![CDATA[(\w{6,16})]]></param>
- <message>用户名输入不合法,必须为长度在6~16中间的数字或字母</message>
- </field-validator>
- </field>
-
-
- <field name="user.password">
- <field-validator type="requiredstring">
- <param name="trim">true</param>
- <message>密码不能为空</message>
- </field-validator>
- <field-validator type="regex">
- <param name="expression"><![CDATA[(\w{6,16})]]></param>
- <message>密码输入不合法,必须为长度在6~16之间的数字或者字母</message>
- </field-validator>
- </field>
- </validators>
- 业务逻辑接口UserManaer.java的内容。
- package com.ynwi.ssh.service;
-
- import com.ynwi.ssh.beans.UserForm;
-
- public interface UserManager {
-
- public void regUser(UserForm user);
-
- }
- 业务逻辑实现类UserManagerImpl.java的内容
- package com.ynwi.ssh.serviceImpl;
-
- import com.ynwi.ssh.beans.UserForm;
- import com.ynwi.ssh.service.UserManager;
-
- public class UserManagerImpl implements UserManager {
-
- @Override
- public void regUser(UserForm user) {
-
- }
-
- }
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
-
- <struts>
-
- <include file="struts-default.xml" />
- <package name="ynwi" extends="struts-default">
- <action name="register" class="com.ynwi.ssh.action.RegisterAction">
- <result name="input">/Register.jsp</result>
- <result name="success">/Success.jsp</result>
- <result name="error">/Fail.jsp</result>
- </action>
- </package>
-
- </struts>
Success.jsp和Fail.jsp仅仅只是一句提示注册成功和失败的话,这里就不在列出。
运行效果
- 输入访问Register.jsp的地址http://localhost:9090/SSHProject/Register.jsp后。
在Struts基础上整合Hibernate
整合Hibernate
- 将Hibernate内的lib/required下的jar包和lib/jpa下的jar包复制到SSHProject项目的lib目录下。
- 在上面Struts项目的基础上添加com.ynwi.ssh.dao和com.ynwi.ssh.daoImpl包。分别创建BaseDao接口和UserDao类。
- 在数据库中创建对应的表user来写入要注册的用户。创建表的脚本见前言说明部分。
- 创建user表的Hibernate映射文件。因为这里要创建user表对应的POJO类,所以我这里把之前com.ynwi.ssh.beans包改成com.ynwi.ssh.forms包,用来存放页面传过来的表单数据,新创建com.ynwi.ssh.beans包,用来创建POJO对象来对应数据库中的表。路径:POJO类同目录。
- 建立Hibernate配置文件。路径:src目录下。
- 编写Session工厂类。整合Spring后,数据源的创建和SessionFactory的创建都交给Spring去管理,就无需这个工厂类了,所以这里我临时创建在daoImpl包里面。
- 修改业务逻辑实现类。
操作完成后的结构如图所示:
编写代码
- 接口类BaseDao.java的内容。注意:接口里的两个get/set方法是为了让实现类覆盖。这样使用BaseDao dao = new UserDao()的时候,dao实体中有这两个方法。
- package com.ynwi.ssh.dao;
-
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
-
- public interface BaseDao {
-
- public void saveObject(Object obj) throws HibernateException;
-
- public Session getSession();
-
- public void setSession(Session session);
- }
- package com.ynwi.ssh.daoImpl;
-
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
-
- import com.ynwi.ssh.dao.BaseDao;
-
- public class UserDao implements BaseDao {
-
- private Session session;
-
- @Override
- public Session getSession() {
- return session;
- }
-
- @Override
- public void setSession(Session session) {
- this.session = session;
- }
-
- @Override
- public void saveObject(Object obj) throws HibernateException {
- session.save(obj);
- }
-
- }
- 表user的POJO类User.java的内容,因为跟UserForm.java内容差不多,所以不再列出。这个比UserForm.java多一个userId属性,因为在表user里面有userId的column。在POJO类里需要跟这个column有映射。
- POJO类User.java的Hibernate映射文件User.hbm.xml的内容。代码<generator class="native"></generator>说明将根据本地数据库的设置来创建userId,如MySQL中将表user的userId设置为自动增长,则在保存记录是,userId将自动增长。对于需要在程序中指定主键的值,则将generator的class属性设置为assigned。
- <?xml version="1.0" encoding='UTF-8'?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
-
- <hibernate-mapping package="com.ynwi.ssh.beans">
- <class name="User" table="user">
- <id name="userId" column="userId">
- <generator class="native"></generator>
- </id>
- <property name="username" column="userName" type="java.lang.String"
- not-null="true" length="16"></property>
- <property name="password" column="password" type="java.lang.String"
- not-null="true" length="16" />
- <property name="gender" column="gender" type="java.lang.Integer" length="1" />
- </class>
- </hibernate-mapping>
- Hibernate配置文件Hibernate.cfg.xml的内容。
- <?xml version='1.0' encoding='utf-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
-
- <hibernate-configuration>
- <session-factory>
-
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://localhost:3306/project</property>
- <property name="connection.username">root</property>
- <property name="connection.password">root</property>
-
-
-
-
-
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
-
-
-
-
-
-
-
-
- <property name="show_sql">true</property>
-
-
-
- <mapping resource="com/ynwi/ssh/beans/User.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
- 工厂类SessionFactory的内容。(这是使用Hibernate4时候的SessionFactory,Hibernate3时候sessionFactory的创建估计会不一样,请自行查阅。)
- package com.ynwi.ssh.daoImpl;
-
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.service.ServiceRegistry;
- import org.hibernate.service.ServiceRegistryBuilder;
-
- public class HibernateSessionFactory {
-
- private static final String CFG_FILE_LOCATION = "/Hibernate.cfg.xml";
-
- private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
-
- private static final Configuration cfg = new Configuration()
- .configure(CFG_FILE_LOCATION);
-
- private static ServiceRegistryBuilder builder = new ServiceRegistryBuilder()
- .applySettings(cfg.getProperties());
-
- private static ServiceRegistry registry;
-
- private static SessionFactory sessionFactory;
-
- public static Session currentSession() throws HibernateException {
- Session session = threadLocal.get();
-
- if (session == null || session.isOpen() == false) {
-
- if (sessionFactory == null) {
- try {
- registry = builder.buildServiceRegistry();
- sessionFactory = cfg.buildSessionFactory(registry);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- session = sessionFactory.openSession();
- threadLocal.set(session);
-
- }
-
- return session;
- }
-
- public static void closeSession() throws HibernateException {
- Session session = threadLocal.get();
- threadLocal.set(null);
- if (session != null) {
- session.close();
- }
- }
-
- }
- package com.ynwi.ssh.serviceImpl;
-
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
-
- import com.ynwi.ssh.beans.User;
- import com.ynwi.ssh.dao.BaseDao;
- import com.ynwi.ssh.daoImpl.HibernateSessionFactory;
- import com.ynwi.ssh.daoImpl.UserDao;
- import com.ynwi.ssh.forms.UserForm;
- import com.ynwi.ssh.service.UserManager;
-
- public class UserManagerImpl implements UserManager {
-
- private BaseDao dao;
-
- private Session session;
-
- public UserManagerImpl() {
- dao = new UserDao();
- }
-
- @Override
- public void regUser(UserForm userForm) throws HibernateException {
- session = HibernateSessionFactory.currentSession();
- dao.setSession(session);
-
- Transaction ts = session.beginTransaction();
-
- User user = new User();
- user.setUsername(userForm.getUsername());
- user.setPassword(userForm.getPassword());
- user.setGender(userForm.getGender());
-
- dao.saveObject(user);
-
- ts.commit();
-
- HibernateSessionFactory.closeSession();
- }
-
- }
运行效果
- 输入访问Register.jsp的地址http://localhost:9090/SSHProject/Register.jsp后。
在Struts + Hibernate项目中整合Spring
整合Spring
- 将Spring内libs目录下包含所有的jar包(不需要复制结尾为sources和javadoc的jar包)到SSHProject项目的lib目录下。(文章后面有spring的直接下载路径)
- 编写Spring的配置文件applicationContext.xml。路径:src目录下,需要在web.xml配置context-param指定路径,或者把该文件放在WEB-INF下,跟web.xml同目录。这里由于Spring配置数据源的需要,需要把Hibernate内lib/optional/c3p0下的c3p0-0.9.1.jar复制到lib不目下。
- 修改BaseDao和UserDao。在引入Spring后,需要用Spring进行统一的事务管理,数据源和sessionFactory都交给Spring去生成,因此接口类和实现类BaseDao和UserDao都需要做相应的修改。Spring提供了HibernateDaoSupport类来完成对数据的操作,因此UserDao在实现BaseDao的同时还需要继承HibernateDaoSupport类。并将先前session的操作修改成HibernateTemplate(可通过getHibernateTemplate()方法来获得)的操作。
- 修改业务逻辑实现类。在没有加入Spring之前,业务逻辑实现类的Session的获得,dao的实例化,以及事务的管理都是该类执行管理的。加入Spring后,这些都交给Spring去管理。该类的dao的实例化由Spring注入。
- 修改用户注册的RegisterAction类。同样,RegisterAction类中的userManager的实例化也由Spring注入。
- 删除Hibernate的配置文件Hibernate.cfg.xml和工厂类HibernateSesseionFactory类。他们的工作已经交给Spring去做,已经不再有用。
- 修改web.xml,加载Spring。要想启动时加载Spring的配置文件,需要在web.xml中配置对应的监听器(listenser),并制定Spring的配置文件。
- 修改Struts的配置文件struts.xml。把原来指定的名为register的action的class由原来的路径变为applicationContext.xml文件中该Action的id。
操作完成后的结构如图所示:
编写代码
- Spring的配置文件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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
-
-
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="jdbcUrl">
- <value>jdbc:mysql://localhost/project</value>
- </property>
- <property name="user">
- <value>root</value>
- </property>
- <property name="password">
- <value>root</value>
- </property>
- <property name="maxPoolSize">
- <value>80</value>
- </property>
- <property name="minPoolSize">
- <value>1</value>
- </property>
- <property name="initialPoolSize">
- <value>1</value>
- </property>
- <property name="maxIdleTime">
- <value>20</value>
- </property>
- </bean>
-
-
-
-
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref local="dataSource" />
- </property>
- <property name="mappingResources">
- <list>
- <value>com/ynwi/ssh/beans/User.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
- <prop key="show_sql">true</prop>
- <prop key="hibernate.jdbc.batch_size">20</prop>
- </props>
- </property>
- </bean>
-
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
-
- <bean id="baseDao" class="com.ynwi.ssh.daoImpl.UserDao">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
-
-
- <bean id="userManager" class="com.ynwi.ssh.serviceImpl.UserManagerImpl">
- <property name="dao">
- <ref bean="baseDao" />
- </property>
- </bean>
-
-
- <bean id="regAction" class="com.ynwi.ssh.action.RegisterAction">
- <property name="userManager">
- <ref bean="userManager" />
- </property>
- </bean>
-
-
-
- </beans>
- 修改后的接口类Dao和实现类UserDao的内容。
BaseDao.java
- package com.ynwi.ssh.dao;
-
- import org.hibernate.HibernateException;
-
- public interface BaseDao {
-
- public void saveObject(Object obj) throws HibernateException;
-
- }
UserDao.java
- package com.ynwi.ssh.daoImpl;
-
- import org.hibernate.HibernateException;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
-
- import com.ynwi.ssh.dao.BaseDao;
-
- public class UserDao extends HibernateDaoSupport implements BaseDao {
-
- @Override
- public void saveObject(Object obj) throws HibernateException {
- getHibernateTemplate().save(obj);
- }
-
- }
- 修改后的业务逻辑实现类UserManagerImpl的内容。
- package com.ynwi.ssh.serviceImpl;
-
- import org.hibernate.HibernateException;
- import org.springframework.beans.BeanUtils;
-
- import com.ynwi.ssh.beans.User;
- import com.ynwi.ssh.dao.BaseDao;
- import com.ynwi.ssh.forms.UserForm;
- import com.ynwi.ssh.service.UserManager;
-
- public class UserManagerImpl implements UserManager {
-
- private BaseDao dao;
-
- public void setDao(BaseDao dao) {
- this.dao = dao;
- }
-
- @Override
- public void regUser(UserForm userForm) throws HibernateException {
- User user = new User();
- BeanUtils.copyProperties(userForm, user);
- dao.saveObject(user);
- }
-
- }
- 修改后的用户注册Action类RegisterAction的内容。
- package com.ynwi.ssh.action;
-
- import com.opensymphony.xwork2.ActionSupport;
- import com.ynwi.ssh.forms.UserForm;
- import com.ynwi.ssh.service.UserManager;
-
- public class RegisterAction extends ActionSupport {
-
- private static final long serialVersionUID = 1L;
-
- private UserForm user;
-
- private UserManager userManager;
-
- public UserForm getUser() {
- return user;
- }
-
- public void setUser(UserForm user) {
- this.user = user;
- }
-
- public void setUserManager(UserManager userManager) {
- this.userManager = userManager;
- }
-
- public String execute() {
- try {
- userManager.regUser(user);
- return SUCCESS;
-
- } catch (Exception e) {
- e.printStackTrace();
- return ERROR;
- }
- }
-
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
-
- <display-name>SSH Project</display-name>
-
- <filter>
- <filter-name>SSH</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>SSH</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
-
- </web-app>
- 修改后的Struts配置文件struts.xml的内容。
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
-
- <struts>
-
- <include file="struts-default.xml" />
- <package name="ynwi" extends="struts-default">
- <action name="register" class="regAction">
- <result name="input">/Register.jsp</result>
- <result name="success">/Success.jsp</result>
- <result name="error">/Fail.jsp</result>
- </action>
- </package>
-
- </struts>
运行结果
- 输入访问Register.jsp的地址http://localhost:9090/SSHProject/Register.jsp后。
总结
本来想创建Struts2 + Spring3 + Hibernate4的项目,结果行动之后才发现版本之间不兼容的现象挺严重(我自己是这么认为的,如果有大神看出来是我开发中出的问题导致麻烦告知下),不得不把Hibernate4换成Hibernate3。因为中间换框架,所以导致此博文中整合Hibernate部分是否会报错,不过最终的项目是可以执行的。我会把项目打包上传到csdn资源上。可以点击这里下载。
如何快速开发SSH框架项目
- 创建Dynamic Web Project并且导入jar包。
- 我习惯上把classes文件夹由build文件夹内转移到WEB-INF下。在项目上右键Build Path——〉Configure Build Path。设置Default output folder。
- 在WEB_INF下面创建web.xml并配置Struts的Filter和Spring的Listener。
- 在WEB-INF下面创建applicationContext.xml并配置数据源,sessionFactory,Transaction以及各Beans信息。
- 在src目录下创建struts.xml用来配置action信息,模板可以从Struts空项目里面拿。这里的action对应的class应该为applicationContext.xml内对应的class的bean的id。
该文章转自:http://blog.csdn.net/yeohcooller/article/details/9316923
SPRING官方网站改版后,下载spring非常麻烦,下给出官方直接下载路径:
http://repo.springsource.org/libs-release-local/org/springframework/spring/
Spring Framework 4.0.0下载
http://repo.springsource.org/libs-release-local/org/springframework/spring/4.0.0.RELEASE/spring-framework-4.0.0.RELEASE-dist.zip
spring 3.2.x(Spring Framework 3.2.6)下载:
http://repo.springsource.org/libs-release-local/org/springframework/spring/3.2.4.RELEASE/spring-framework-3.2.4.RELEASE-dist.zip
http://repo.springsource.org/libs-release-local/org/springframework/spring/3.2.5.RELEASE/spring-framework-3.2.5.RELEASE-dist.zip
http://repo.springsource.org/libs-release-local/org/springframework/spring/3.2.6.RELEASE/spring-framework-3.2.6.RELEASE-dist.zip
在此基础上加入自己的体会
1、配置struts时,主要是jsp和action的操作控制,根据struts.xml里的配置,决定action转向哪个页面
2、整合Hibernate时,我第一次用的是最新的5.x,里边少一个类org.hibernate.service.ServiceRegistry,然后下载来一个4.x版本的;Hibernate.cfg.xml主要时数据库的配置(<mapping resource="com/beans/User.hbm.xml"/>,当时我写成<mapping resource="com.beans.User.hbm.xml"/>了,其实是写路径的),另外就是实体类的映射,例如:User.hbm.xml;最后是工具类HibernateSessionFactory的配置.
3、在整合spring3.x时,首先出现:找不到这个类“org.apache.commons.logging.LogFactory”,把commons-logging.jar这个包添加上去之后又出现:java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector,这个错,
但是我里边有这个jra包,后来又添加了“mchange-commons-”这个包后,出现【
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'driverClassName' of bean class [com.mchange.v2.c3p0.ComboPooledDataSource]: Bean property 'driverClassName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
】所以我感觉不是这个包的问题,我把他去掉还是这个说数据源创建错误,
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver :添加sqljdbc4.jar
4、在写查询所有用户工能:
报错:testUser is not mapped
问题所在:
@Override
public List<User> findAllUsers() {
String hql = "from
testUser order by userid asc";// TODO Auto-generated method stub
User user = new User();
List<User> list = this.getHibernateTemplate().find(hql);
return list;
}
对,就是testUser写错了!它是数据库中的表明,而正确的写法应该是实体类的User
@Override
public List<User> findAllUsers() {
String hql = "from
User order by userid asc";// TODO Auto-generated method stub
User user = new User();
List<User> list = this.getHibernateTemplate().find(hql);
return list;
}
这样就解决了。
在这之前我还写错了ListUserAction的查询所有用户的方法:
我这样写的:
public String
excute() throws Exception{
System.out.println("==========ListUserAction");
Map request = (Map) ActionContext.getContext().get("request");
List<UserForm> lisrUser = userService.findAllUsers();
System.out.println(lisrUser.get(0));
request.put("list", lisrUser);
return SUCCESS;
}
是粗心惹的祸!正确是 public String
execute(){};
5、登录功能:也是一个掌握的细节!虽然不起眼,但却让我忙活了快一天的时间们终于解决!
登录功能的实现,首先是根据用户名去查询,我在serviceImpl中这样写的:
public User findByProperty(String propertyName, Object value) {
String hql = "
from User where User."+propertyName+"=?";
List<User> user = this.getHibernateTemplate().find(hql,value);
if(user != null && user.size() == 1){
return user.get(0);
}
return null;
}
报错:Unable to resolve path [User.username],
参考这位博主的博客http://blog.csdn.net/kouhl/article/details/17550105终于把问题解决了:
Usert是类名,不能直接使用User.usernmae(不能通过类来访问,就好像你不能通过类名去访问实例变量一样),请不要问为什么,记住这是HQL的语法要求就行了。
那怎么办呢?通常会用别名的方式解决,比如
String hql = "from User as user where user."+propertyName+"=?";
(别名就好像是User类的一个对象,通过对象就可以访问实例变量啦)
这是我的源代码:
http://download.csdn.net/my
其他问题:
1、首先的报错如下:
严重: Could not open template
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'queryaction' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception isorg.springframework.beans.NotWritablePropertyException: Invalid property 'recordService' of bean class [com.action.QueryMobileAction]: Bean property 'recordService' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1423)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:314)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1117)
at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:221)
at org.apache.struts2.components.ServletUrlRenderer.renderFormUrl(ServletUrlRenderer.java:161)
at org.apache.struts2.components.Form.populateComponentHtmlId(Form.java:220)
at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:784)
at org.apache.struts2.components.ClosingUIBean.start(ClosingUIBean.java:57)
at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:53)
at org.apache.jsp.jsp.iframes.IVR.IVR_005fmytd_jsp._jspx_meth_s_005fform_005f0(IVR_005fmytd_jsp.java:138)
at org.apache.jsp.jsp.iframes.IVR.IVR_005fmytd_jsp._jspService(IVR_005fmytd_jsp.java:102)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2522)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2511)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'recordService' of bean class [com.action.QueryMobileAction]: Bean property 'recordService' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1042)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:902)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1420)
... 44 more
其中标红的就是真正的错误原因,在
com.action.QueryMobileAction]里边我原先是这样写的:
private RecordService
rsService;
public RecordSequence getRs() {
return rs;
}
public void setRs(RecordSequence rs) {
this.rs = rs;
}
public RecordService getRsService() {
return rsService;
}
public void setRsService(RecordService rsService) {
this.rsService = rsService;
}
而报错说
'recordService' 没有set和get,我一直认为只要随便命名一个实例化的类就行了,所以一直忽略这个原因,现在我想可能action里的实例化的 RecordService这个类的名字要与
applicationContext.xml的
<bean
id="recordService" class="com.serviceImpl.RecordServiceImpl">
<property name="recordDao">
<ref bean="recordDao"/>
</property>
</bean>
bean属性的id保持一致,这样才能找到action。
2、jsp页面标签问题:
<s:textfield name="user.username" lable="用户">
user那么表示在action中定义了一个user对象,里面有username属性
3、报错:
Null value was assigned to a property of primitive type setter of com.beans.RecordSequence.flag
原因:我数据库中有一个字段flag为NULL,我实体类中写为:【private int flag;】所以不行,改为【private Integer flag;。我的映射类
<property name="flag" column="flag" type="java.lang.Integer" not-null="false" length="10"></property>是对的。
我改完之后,怎么运行还是报错,最终终于发现问题所在:
没有及时生成set和get方法!