hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:O92
</property>
<property name="connection.username">kk</property>
<property name="connection.password">kkpwd</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="myeclipse.connection.profile">
oracleDriver
</property>
<property name="show_sql">true</property>
<mapping resource="org/kaixin/pojos/Userinfo.hbm.xml" />
<mapping resource="org/kaixin/pojos/Users.hbm.xml" />
</session-factory>
</hibernate-configuration>
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:aop="http://www.springframework.org/schema/aop"
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.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
红色字加粗部分是配置事务必须加上的。
在Spring配置文件中配置Hibernate的Session工厂和hibernateTemplate:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
给通用类Common注入hibernateTemplate:
<bean id="common" class="org.kaixin.common.Common">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
给需要的包下的类中的方法添加事务:
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" rollback-for="java.lang.Exception" />
<tx:method name="load*" read-only="true" rollback-for="java.lang.Exception" />
<tx:method name="find*" read-only="true" rollback-for="java.lang.Exception" />
<!--
<tx:method name="*" propagation="REQUIRED"
rollback-for="java.lang.Exception" />
-->
<tx:method name="*" rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
<aop:config>
<!--
<aop:pointcut id="serviceOperation" expression="execution(*
org.kaixin.*.*(..))" />
-->
<aop:pointcut id="serviceOperation" expression="execution(* org.kaixin.idao.*.*(..))" />
<aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice" />
</aop:config>
导入其他外部文件:
<import resource="pojo.xml" />
<import resource="dao.xml" />
<import resource="service.xml" />
<import resource="action.xml" />
pojo.xml:
<bean id="users" class="org.kaixin.pojos.Users"></bean>
<bean id="userinfo" class="org.kaixin.pojos.Userinfo"></bean>
dao.xml:
<bean id="usersdaoimpl" class="org.kaixin.dao.impl.UsersDaoImpl">
<property name="icommon">
<ref bean="common"/>
</property>
</bean>
<bean id="userinfodaoimpl" class="org.kaixin.dao.impl.UserinfoDaoImpl">
<property name="icommon">
<ref bean="common"/>
</property>
</bean>
service.xml:
<bean id="loginserviceimpl" class="org.kaixin.service.impl.LoginServiceImpl">
<property name="iusersdao">
<ref bean="usersdaoimpl"/>
</property>
</bean>
action.xml:
<bean id="loginaction" class="org.kaixin.beans.LoginAction" scope="prototype">
<property name="loginservice" ref="loginserviceimpl"></property>
</bean>
<bean name="registeraction" class="org.kaixin.beans.RegisterAction"scope="prototype"></bean>
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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<include file ="struts-default.xml" />
<!--<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />-->
<constant name="struts.objectFactory" value="spring"/>
<package name="log" extends="struts-default" namespace="/login_register">
<default-action-ref name="login" />
<action name="login" class="loginaction" >
<result name="input">/login/login.jsp</result>
<result name="loginSecceed">/login/loginSecceed.jsp</result>
<result name="loginFailed">/login/loginFailed.jsp</result>
</action>
<action name="register" class="registeraction">
<result name="registerSecceed">/login/registerSecceed.jsp</result>
<result name="registerFailed">/login/registerFailed.jsp</result>
</action>
</package>
</struts>
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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
login/login.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>登录</title>
<link rel="stylesheet" type="text/css" href="../css/overlay.css" />
<link rel="stylesheet" type="text/css" href="../css/share.css" />
<link rel="stylesheet" type="text/css" href="../css/login.css" />
</head>
<body>
<!-- container s -->
<div class="container">
<!-- header s -->
<div class="header">
<span class="logo"></span>
</div>
<!-- header e -->
<!-- bodier s-->
<div class="bodier">
<!-- left s -->
<div class="left">
<div class="l_login">
<div class="ll_login">
<s:form action="login" namespace="/login_register">
<s:textfield name="users.UUsername" label="用户名" ></s:textfield>
<s:textfield name="users.UPassword" label="密 码" ></s:textfield>
<s:submit label="submit"></s:submit>
</s:form>
</div>
</div>
<div class="l_register">
<s:a href="../login/register.jsp">注册</s:a>
</div>
</div>
<!-- left e -->
<!-- right s -->
<div class="right">
right
</div>
<!-- right e -->
</div>
<!-- bodier e -->
<!-- footer s -->
<div class="footer">
footer
</div>
<!-- footer e -->
</div>
<!-- container e -->
</body>
</html>
在写这些代码的过程中,处理过这些问题:
1:刚刚架构好hibernate,测试hibernate对oracle的查询功能,成功!可测试添加操作时,总是报一个错:java.sql.SQLException: 不支持的特性,于是换了oracle驱动,由原先的class12.jar换成了ojdbc6.jar,才知道ojdbc14,ojdbc15,ojdbc6的区别和用处。终于不报错了,可是又报org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.kaixin.pojos.Users#9]就是无法往oracle数据库添加数据,于是自己在oracle数据库中建立了一个序列和对应这个表的触发器;可还是不行,于是把pojo类对应的配置文件里面的native属性改成了sequence,还是不行,于是又把sequence改为了increment,数据库的插入语句才出来。
2:数据库插入语句是出来了,可就是没法往数据库添加数据,猜想是Spring的事务配置有问题,找事务配置语句的语法错误,终没找到,心里一烦躁,真不想继续,后来才发现,是自己配置连接点的时候写错了包名!改了之后马上就好了。
3:整合好hibernate3.3+Struts2.1时,试图提交表单实现转发,可总是没法找到我的命名空间下的action,看地址栏里面,简直一点规律都没有,于是自己推断是不是自己的命名空间和web-root下面的文件夹名字有出入,可我无论怎么调,就是拦截不到action,后来才发现struts.xml配置文件里面package标签的namespace属性应该跟前台页面中的namespace属性一致,要加反斜杠,而且不应该是<form>而应该是<s:form namespace="/xx">,可是加了反斜杠以后,虽然没有报错了,可还是拦截不到action,地址栏中的信息依然摸不到规律于是自己推断是不是拦截器有问题,改了拦截器配置.虽然之前在网上一直找不到适合于自己的答案,不论是改web.xml里面的Struts2对filter的class属性还是注意是否写错了什么细节。后来才发现到底什么时候用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter什么时候用org.apache.struts2.dispatcher.FilterDispatcher。前者的版本比后者高。然后又把默认的*.action改为了/*,也就是说把原来的
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
改成了:
<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>
其中把*.action改为/*是关键!
可问题还是没有解决。
老是报错无法实例化struts2的actionbean
于是下意识的在actionbean里面添加了个无参数的构造方法,这下再访问,一切OK了。
测试成功后又反复测命名空间是否与webroot下建立文件夹有关系。后来发现,没有直接关系,namespace是namespace,文件夹是文件夹,如果既有namespace又有文件夹的话,假如现在namespace是“login_register”,webroot下面的文件结构是这样“webroot/login/login.jsp”则应该这样访问:http://localhost:8087/kaixin1/login/login.jsp,点击提交按钮后地址栏中的内容就成了这样:http://localhost:8087/kaixin1/login_register/login.action。
4:正待一切都OK,此时我需要在actionbean里面用Spring给struts2的actionbean注入service类,可怎么都不成功,总报actionbean中execute方法中引用service时的nullpointerexception,估计是spring没有给struts中的service属性初始化,于是在action.xml里面配置了相应的bean,如:
<bean id="loginaction" class="org.kaixin.beans.LoginAction" scope="prototype">
<property name="loginservice" ref="loginserviceimpl"></property>
</bean>
然后在struts.xml文件里面改动了下:由原先的
<action name="login" class="org.kaixin.beans.LoginAction" >
<result name="input">/login/login.jsp</result>
<result name="loginSecceed">/login/loginSecceed.jsp</result>
<result name="loginFailed">/login/loginFailed.jsp</result>
</action>
改为了:
<action name="login" class="loginaction" >
<result name="input">/login/login.jsp</result>
<result name="loginSecceed">/login/loginSecceed.jsp</result>
<result name="loginFailed">/login/loginFailed.jsp</result>
</action>
这下空指针终于消除了。
5:空指针是消除了,可是提交页面时,
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I
又出现啦。
于是删掉了struts库下面的anltr.jar后,运行OK!只要原因是struts2里面的anltr与hibernate3.3里面的anltr包相冲突了。
怎么删除库下面的jar文件?
点myeclise的window--->preferences--->myEclipse--->Project Capabilities--->Struts2--->struts2 core libraries(下拉框的第二项),然后选择anltr.jar,remove掉,close一下自己的工程,再openproject,把tomcat下面的已经发布了的该工程remove掉,重新发布一次,重启服务器,一切OK。