ssh Session session = super.getSession();//得不到session

我是新学ssh的

[code="java"]
public class LogonDAO extends HibernateDaoSupport
{
public void add()
{

    LogonUser l = new LogonUser();
    l.setName("name");
    l.setPw("pw");
    Session session = super.getSession();//得不到session 
    session.save(l);
}

@SuppressWarnings("unchecked")
public List query(String name, String pw)
{
    Session session = super.getSession();
    String hql = "from LogonUser user where user.name = :name and user.pw = :pw";
    List  list =  session.createQuery(hql).list();
    session.createQuery(hql).setString("name", name);
    session.createQuery(hql).setString("pw", pw);
    return list;

}

}
[/code]

spring 配置文件
[code="java"]<?xml version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="dataSource"
    class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/nes"></property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.connection.autocommit">true</prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
            <value>
                com/myssh/logon/hbmEnity/LogonUser.hbm.xml 
            </value>
        </list>
    </property>
</bean>
<bean id="hibernateTemplate"
    class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

[/code]

struts 配置文件
[code="java"]<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<form-beans>
    <form-bean name="logonForm"
        type="com.myssh.logon.actionForm.LogonForm">
    </form-bean>
</form-beans>
<global-forwards/>
<action-mappings>
    <action path="/logon" parameter="method" scope="request"
        type="com.myssh.logon.action.LogonAction" name="logonForm">
    </action>
</action-mappings>
<!-- 替换控制器 -->
 <controller
    processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
<message-resources parameter="com.myssh.ApplicationResources" />
<!-- 整合spring使用的插件-->
<plug-in
    className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation"
        value="/WEB-INF/applicationContext.xml" />
</plug-in>


[/code]

web配置文件
[code="java"]<?xml version="1.0" encoding="UTF-8"?>
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">
<!-- 歡迎頁 -->

index.jsp

<!-- 配置SPRING -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<!-- 配置STRUTS -->
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
        org.apache.struts.action.ActionServlet
    </servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
        <param-name>debug</param-name>
        <param-value>2</param-value>
    </init-param>
    <init-param>
        <param-name>detail</param-name>
        <param-value>2</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<!--以下是由Spring提供的filter解決Struts亂碼問題 -->
<filter>
    <filter-name>encoding</filter-name>
    <filter-class>
        org.springframework.web.filter.CharacterEncodingFilter
    </filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 解決Struts亂碼 -->

<!-- 使用Spring解決Hiberante因session關閉導致的延遲例外問題 -->
<filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 解決延遲加載例外問題 -->
<resource-ref>
    <res-ref-name>jdbc/nes</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>


[/code]
tomcat 5 下的配置文件
[code="java"]



maxWait
5000


maxActive
20


password
root


url
jdbc:mysql://localhost/nes


driverClassName
com.mysql.jdbc.Driver


maxIdle
10


username
root


[/code]
有什么问题吗 ? 为什么在DAO中Session session = super.getSession();//得不到session

7个回答

而且事物管理好像没有,事务的传播也没有。。。。。[code="java"]<!-- 配置事务管理器 -->




<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
    <tx:attributes>
        <!-- 在开发的时候可以这样定义,但部署的时候一定要详细定义 -->
        <tx:method name="*" propagation="REQUIRED"/>
    <!--  
        <tx:method name="add*" propagation="REQUIRED"/>
        <tx:method name="del*" propagation="REQUIRED"/>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="*" read-only="true"/>
    -->
    </tx:attributes>
</tx:advice>[/code]

用super.getCurrentSession();

[code="java"]

classpath:hibernate.cfg.xml

[/code]

好像sessionFactory是这样配置的

明显Dao方法中没有注入HibernateTemplate。



[code="java"] 1. public class LogonDAO extends HibernateDaoSupport

  1. {
  2. public void add()
  3. {

  4. LogonUser l = new LogonUser();
  5. l.setName("name");
  6. l.setPw("pw");
  7. Session session = super.getSession();//得不到session
    1. session.save(l);
    2. }

    3. @SuppressWarnings("unchecked")
    4. public List query(String name, String pw)
    5. {
    6. Session session = super.getSession();
    7. String hql = "from LogonUser user where user.name = :name and user.pw = :pw";
    8. List list = session.createQuery(hql).list();
    9. session.createQuery(hql).setString("name", name);
    10. session.createQuery(hql).setString("pw", pw);
    11. return list;

    12. }
    13. } [/code]

LogonDAO没有注入sessionfactory或hiberantetemplate。。
随便找一个注入一下就成了。。。

[code="java"] at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:140) [/code]

看看这个地方 为什么抛 nullpointer

既然你使用了SSH为何不用它的HibernateTemplate呢?怎么说呢、几乎都有一些小问题,Session哪里来,从SessionFactory来的。 class="org.springframework.orm.hibernate3.HibernateTemplate">







配置了这个,要从hibernateTemplate取得SF,然后才有Session哪。你什么都没有,当然是NullPointException了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐