一净土 发表于 2016-12-3 04:22:32

【已解决】struts2与hibernate整合出错

本帖最后由 一净土 于 2016-12-3 17:15 编辑

最近学习Java Web开发,我就按照教程提供的一个最简单的案例试了下。可是总是出现问题。{:sweat:}   说: 'hibernate.dialect' must be set when no Connection avalable

都一星期了,还没找到具体解决方法。只能想个大神求救了。我使用的是Eclipse3.6,Tomcat的版本7.0.27和6.0.48都试过,服务器是MySQL,


项目样本:http://pan.baidu.com/s/1c2205YK

感觉10枚有点少,我也没办法,帖子只能设置10枚

hibernate配置如下:
<?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">
<hibernate-configuration>
      <session-factory>
<!--               <property name="connection.dialect">org.hibernate.dialect.Oracle10gDialect</property> -->
<!--               <property name="connection.Driver_class">oracle.jdbc.OracleDriver</property> -->
<!--               <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> -->
<!--               <property name="connection.username">dong</property> -->
<!--               <property name="connection.password">123456</property> -->

                        <property name="connection.dialect">org.hibernate.dialect.MySQLDialect</property>
                        
                        <property name="connection.Driver_class">com.mysql.jdbc.Driver</property>
                        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/my</property>
                        <property name="connection.username">root</property>
                        <property name="connection.password">123456</property>
               
                <property name="show_sql">true</property>
                <property name="hbm2ddl.auto">update</property>
               
                <mapping resource="cn/bean/Student.hbm.xml"/>
                <mapping resource="cn/bean/Grade.hbm.xml"/>
      </session-factory>
</hibernate-configuration>

web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SH01</display-name>

<filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>*.do</url-pattern>
</filter-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

struts2配置如下:
<?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.i18n.encoding" value="UTF-8"></constant>
      <constant name="struts.devMode" value="true"></constant>
      <constant name="struts.action.extension" value="do"></constant>
      <constant name="struts.i18n.reload" value="true"></constant>
      <constant name="struts.configuration.xml.reload" value="true"></constant>
      
      <package name="action" namespace="/" extends="struts-default">
                <action name="student" class="cn.action.StudentAction" >
                        <result name="success">index.jsp</result>
                </action>
               
      </package>
<!--         <include file="cn/action/struts_Student.xml"></include> -->
</struts>

在src目录下的Action代码为:
package cn.action;

import cn.demo.Demo;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class StudentAction extends ActionSupport {

      @Override
      public String execute() throws Exception {
                Demo demo = new Demo();
                demo.test01();
                return "success";
      }
      
}


与Action同一目录下的struts_Student.xml文件配置没有调用,在struts.xml中注释了;
测试类代码如下:
package cn.demo;

import org.hibernate.Transaction;
import org.junit.Test;

import cn.bean.Grade;

public class Demo {
      
      @Test
      public void test01(){
                org.hibernate.Session session =null;
                org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration();
                configuration = configuration.configure("/hibernate.cfg.xml");
                org.hibernate.SessionFactory factory = configuration.buildSessionFactory(); //web项目下这步就会报错
                session = factory.openSession();
                Transaction tc =session.beginTransaction();
                Grade grade = new Grade("大6");
                session.save(grade);
                tc.commit();
                session.close();
      }
}


错误信息如下:
Stacktraces
org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection avalable

    org.hibernate.dialect.resolver.DialectFactory.buildDialect(DialectFactory.java:107)
    org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:134)
    org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2119)
    org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2115)
    org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1339)
    cn.demo.Demo.test01(Demo.java:15)
    cn.action.StudentAction.execute(StudentAction.java:34)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1083)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:640)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)
   

You are seeing this page because development mode is enabled. Development mode, or devMode, enables extra debugging behaviors and reports to assist developers. To disable this mode, set:

struts.devMode=false

in your WEB-INF/classes/struts.properties file.

一直找不到解决办法,小弟对此真是没办法了。求大神指点



一净土 发表于 2016-12-3 17:16:22

找到那方言的错误了<property name="connection.dialect">org.hibernate.dialect.MySQLDialect</property>方言的名字不能这么写,这样写相当与你没有写方言,是这样name="dialect",还有<property name="connection.Driver_class">com.mysql.jdbc.Driver</property>中,驱动不能这么写,这样写相当于你没有写驱动,用的都是默认的,是这样:name="connection.driver_class",哎,这东西找了我一星期,还是老师说的对,默认的都不是好东西,错了都不知道
页: [1]
查看完整版本: 【已解决】struts2与hibernate整合出错