日期:2014-05-17  浏览次数:20888 次

struts2、Spring3、hibernate,运行时Action中提示service空指针
Action代码如下:
Java code
package com.bbs.action;

import com.bbs.bean.User;
import com.bbs.service.UserService;

public class LoginAction {
    private User user;
    private String userName;
    private String pwd;
    private UserService userService;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String Login() {
        String rs = null;
        try {
            User user = userService.findUser(userName);
            System.out.println(user.getName() + user.getPasw());
            if (user != null && user.getPasw().equals(pwd)) {
                rs = "success";
            } else {
                rs = "error";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
}


UserServiceImpl代码:
Java code
package com.bbs.service.impl;

import com.bbs.bean.User;
import com.bbs.dao.UserDao;
import com.bbs.service.UserService;

public class UserServiceImpl implements UserService{
    
    private UserDao userDao;

    @Override
    public User findUser(String userName) {
        return userDao.findUser(userName);
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

}


struts.xml代码:
XML code
<?xml version="1.0" encoding="GBK" ?>   
<!DOCTYPE struts PUBLIC   
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
    "http://struts.apache.org/dtds/struts-2.0.dtd">   
<struts>   
    <package name="front" namespace="/" extends="struts-default">   
        <action name="index">   
            <result>/index.jsp</result>   
        </action>
        <action name="toLogin">
         <result>/WEB-INF/jsp/login.jsp</result>       
        </action>
        <action name="login" class="com.bbs.action.LoginAction" method="Login">
             <result name="success">/index.jsp</result>
             <result name="error">/WEB-INF/jsp/error.jsp</result>       
        </action>   
    </package>   
</struts>


applicationContext.xml代码:
XML code
<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/bea