action调用service无限报空指针,试了很多方法都没用

index.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
</head>

<body>
    <script type="text/javascript">
        window.location.href = "Test_test.do";
    </script>
</body>
</html>

action

 package com.action;

import com.opensymphony.xwork2.ActionSupport;
import com.service.StuService;

public class TestAction extends ActionSupport {
    private StuService stuService;

    public String test() throws Exception {
        System.out.println("进入action");
        stuService.getArraylist();
        return "success";
    }

    public StuService getStuService() {
        return stuService;
    }

    public void setStuService(StuService stuService) {
        this.stuService = stuService;
    }

}

dao

 package com.dao;

import com.entity.Log;

public interface LogDAO {
    public void saveLog(Log log);
    public void finfLog();
}

daoImpl

 package com.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.dao.LogDAO;
import com.entity.Log;

@Component
public class LogDAOImpl implements LogDAO {
    private SessionFactory sessionfactory;

    public void saveLog(Log log) {
        Session session = sessionfactory.getCurrentSession();
        session.save(log);
        // throw new NullPointerException();
    }

    public void finfLog() {
        Session session = sessionfactory.getCurrentSession();
        Query query = session.createQuery(" from Log");
        ArrayList<Log> p = (ArrayList<Log>) query.list();
        for (int i = 0; i < p.size(); i++) {
            System.out.println(p.get(i).getMassage());
        }

    }

    public SessionFactory getSessionfactory() {
        return sessionfactory;
    }

    @Resource
    public void setSessionfactory(SessionFactory sessionfactory) {
        this.sessionfactory = sessionfactory;
    }

}

实体类

 package com.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="t_log")
public class Log {
    private int id;
    private String massage;

    public Log() {
        super();
    }

    public Log(int id, String massage) {
        super();
        this.id = id;
        this.massage = massage;
    }

    @Id
    @SequenceGenerator(name = "SEQ_STORE", sequenceName = "stu_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_STORE")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getMassage() {
        return massage;
    }

    public void setMassage(String massage) {
        this.massage = massage;
    }

}

service

 package com.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dao.LogDAO;
import com.dao.StuDAO;
import com.entity.Log;
import com.entity.Stu;

@Component
// 业务逻辑层
public class StuService {
    private StuDAO stuDao;
    private LogDAO logDao;

    @Transactional
    public void getArraylist() throws Exception {
        // stuDao.getStu();
        logDao.finfLog();
    }


    @Resource
    public void setLogDao(LogDAO logDao) {
        this.logDao = logDao;
    }

}

spring配置

 <?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-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/aop
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com" />
    <!-- 数据源 -->
    <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:STUDY" />
        <property name="username" value="sys as SYSDBA" />
        <property name="password" value="7998" />
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="DataSource" />
        <property name="packagesToScan">
            <list>
                <value>com.entity</value>
            </list>
        </property>
        <!-- Hibernate内容 -->
        <property name="hibernateProperties">
            <value>
                hibernate.dialect = org.hibernate.dialect.OracleDialect
                hibernate.show_sql = true
                hibernate.format_sql = true
            </value>
            <!-- <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> 
                <prop key="hibernate.show_sql">true</prop> </props> -->
        </property>
    </bean>

    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" />
            <tx:method name="*" rollback-for="Exce ption" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.service.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
    </aop:config>
    <bean name="testAction" class="com.action.TestAction">
        <property name="stuService" ref="stuService">
        </property>
    </bean>


</beans>

struts配置

```<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<!-- 从2.3.16开始DMI默认是禁止的 全称是:动态方法调用 -->

<!-- -->


<!-- 约定优于配置 -->

/success.jsp











6个回答

你是不是spring配置文件里少了点什么东西,应该要加上stuService的bean吧

private StuDAO stuDao;
private LogDAO logDao;
这俩个没有注入

在Service中使用的两个Dao对象,需要在Srping中配置bean,在Service中提供setter方法。

private StuDAO stuDao;
private LogDAO logDao;

spring配置中没有配置bean

建议你把注入方式统一下,要么就采用注解注入,要么采用xml注入,这样才方便调试和查看,调试的时候在set方法里输出一些信息,看看set方法有没有被调用。

spring注入配置不对,再检查

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