小弟刚接触SSH,不太会,求大神指导。
applicationContext.xml
struts.xml
我感觉没有错啊,可是为什么spring就是没有作用?纠结好几天了,那位大侠帮帮忙哦,感激不尽!!

解决方案 »

  1.   


    这是web.xml,哪里有问题啊?求指教
      

  2.   


    这是web.xml,哪里出错了??
      

  3.   

    应该把spring的配置文件配置到web.xml文件中来
      

  4.   

    比如:<!-- spring的应用上下文 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/classes/applicationContext.xml,
    /WEB-INF/classes/applicationContext-action.xml,
    /WEB-INF/classes/applicationContext-service.xml,
    /WEB-INF/classes/applicationContext-dao.xml
    </param-value>
    </context-param>
      

  5.   

    就是运行程序时不起作用,我觉得应该是没有进去spring的配置,hibernate也是一样的,jsp显示这样的错误
      

  6.   

    很明显空指针异常嘛,都已经进userAction的login方法了。是不是userService的bean没配置.userAction类21行是什么 
      

  7.   

    启动时候 如果配置log的话,会把spring加载的文件log打印出来 看看你的启动log
      

  8.   

    <bean id="userAction" class="com.bysj.useraction"></bean>有这个配置吗
      

  9.   

    没有userAction这个bean,看你的applicationContext不是基于annotation的配置,也没有手动定义这个bean。当然找不到
      

  10.   

    有的,我另外写的一个users.xml,在applicationContext.xml里面调用的,在applicationcontext.xml最后不是有个<import resource="application-user.xml">吗,就在那个里面
    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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.0.xsd ">

    <!-- 用户管理 -->
    <bean id="IUsersDao" class="com.bysj.dao.impl.IUsersDao" scope="prototype">
    <property name="sessionFactory" ref="sessionFactory" />
    <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>
    <bean id="IUsersService" class="com.bysj.service.impl.IUsersService" scope="prototype">
    <property name="IUsersDao" ref="IUsersDao" />
    </bean>
    <bean id="UsersAction" class="com.bysj.action.UsersAction" scope="prototype">
    <property name="IUsersService" ref="IUsersService" /> 
    </bean></beans>是这个吧
      

  11.   

    是java代码package com.bysj.action;import java.util.Map;import org.apache.struts2.interceptor.SessionAware;import com.bysj.model.Users;
    import com.bysj.service.UsersService;
    import com.bysj.service.impl.IUsersService;
    import com.opensymphony.xwork2.ActionSupport;public class UsersAction extends ActionSupport implements SessionAware
    {
     
    private IUsersService UsersService;
    private Users users;

    public String Login()
    {
    //UsersService= new IUsersService();
    boolean b=UsersService.Login(users);//这是第21行
    if(b)
    return SUCCESS;
    return ERROR;
    } public void setSession(Map<String, Object> arg0)
    {

    } public Users getUsers()
    {
    return users;
    } public void setUsers(Users users)
    {
    this.users = users;
    } public UsersService getUsersService()
    {
    return UsersService;
    } public void setUsersService(IUsersService usersService)
    {
    UsersService = usersService;
    }
    }
      

  12.   

    有的,我另外写的一个users.xml,在applicationContext.xml里面调用的,在applicationcontext.xml最后不是有个<import resource="application-user.xml">吗,就在那个里面
    application-user.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:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.0.xsd "> <!-- 用户管理 -->
     <bean id="IUsersDao" class="com.bysj.dao.impl.IUsersDao" scope="prototype">
     <property name="sessionFactory" ref="sessionFactory" />
     <property name="jdbcTemplate" ref="jdbcTemplate" />
     </bean>
     <bean id="IUsersService" class="com.bysj.service.impl.IUsersService" scope="prototype">
     <property name="IUsersDao" ref="IUsersDao" />
     </bean>
     <bean id="UsersAction" class="com.bysj.action.UsersAction" scope="prototype">
     <property name="IUsersService" ref="IUsersService" /> 
     </bean> </beans>是这个吧 
      

  13.   

    你的UserAction里的Login方法报错了,把报错的方法或者整个类贴出来看看
      

  14.   

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/classes/applicationContext.xml,
    /WEB-INF/classes/applicationContext-action.xml,
    /WEB-INF/classes/applicationContext-service.xml,
    /WEB-INF/classes/applicationContext-dao.xml
    </param-value>
    </context-param>路径不对  看你项目路径 少了个config
      

  15.   

    有的,在WEB-INF/config/applicationContext.xml里面
      

  16.   

    package com.bysj.action;import java.util.Map;import org.apache.struts2.interceptor.SessionAware;import com.bysj.model.Users;
    import com.bysj.service.UsersService;
    import com.bysj.service.impl.IUsersService;
    import com.opensymphony.xwork2.ActionSupport;public class UsersAction extends ActionSupport implements SessionAware
    {
     
    private IUsersService UsersService;
    private Users users;

    public String Login()
    {
    //UsersService= new IUsersService();
    boolean b=UsersService.Login(users);
    if(b)
    return SUCCESS;
    return ERROR;
    } public void setSession(Map<String, Object> arg0)
    {

    } public Users getUsers()
    {
    return users;
    } public void setUsers(Users users)
    {
    this.users = users;
    } public UsersService getUsersService()
    {
    return UsersService;
    } public void setUsersService(IUsersService usersService)
    {
    UsersService = usersService;
    }
    }
    就是这个,不会有错吧
      

  17.   

    private IUsersService UsersService;

     <bean id="UsersAction" class="com.bysj.action.UsersAction" scope="prototype">
     <property name="IUsersService" ref="IUsersService" /> 
     </bean>
    比较,楼主太粗心了明显注入的属性不对嘛。name应该是UsersService,一般不要大写,不规范,多难看
      

  18.   

    对了嘛,你配置离注入的是属性 IUsersService,而你类里的属性名称是UsersService,你在Login方法里调用它,肯定报空指针啦
      

  19.   

    我是说你的配置文件的目录里少了config
    /WEB-INF/classes/applicationContext.xml,这个里面少了config
      

  20.   


    这个不是我的配置,我的配置是这样的WEB-INF/config/applicationcontext.xml
      

  21.   


    我现在改了,是这样的我的action里的UsersAction代码是这样的:package com.bysj.action;import java.util.Map;import org.apache.struts2.interceptor.SessionAware;import com.bysj.model.Users;
    import com.bysj.service.UsersService;
    import com.bysj.service.impl.IUsersService;
    import com.opensymphony.xwork2.ActionSupport;public class UsersAction extends ActionSupport implements SessionAware
    {
     
    private IUsersService  UsersService;
    private Users users;

    public String Login()
    {
    //UsersService= new IUsersService();
    boolean b=UsersService.Login(users);
    if(b)
    return SUCCESS;
    return ERROR;
    } public void setSession(Map<String, Object> arg0)
    {

    } public Users getUsers()
    {
    return users;
    } public void setUsers(Users users)
    {
    this.users = users;
    } public UsersService getUsersService()
    {
    return UsersService;
    } public void setUsersService(IUsersService usersService)
    {
    UsersService = usersService;
    }
    }
    是这样的吗?可是还是同样的问题啊
      

  22.   

    Quote: 引用 28 楼 M_Lane 的回复:

    引用 25 楼 wiselight 的回复:
    private IUsersService UsersService;

     <bean id="UsersAction" class="com.bysj.action.UsersAction" scope="prototype">
     <property name="IUsersService" ref="IUsersService" /> 
    ……[/quot
    name="IUsersService肯定是错的。改了还是有问题,可能是你没编译,查看classes里有没有编译
      

  23.   

    <bean id="UsersAction" class="com.bysj.action.UsersAction" scope="prototype">
     <property name="IUsersService" ref="IUsersService" /> 
     </bean>改成单例试试 scope="prototype"  属性去掉
      

  24.   

    service 和dao  的bean改成单例
    action不用改
      

  25.   

    不行,改了之后就彻底错了,jsp页面都显示不了了
      

  26.   

    确定application-user.xml修改了么,你不看eclipse的,看tomcat里的
    struts配置文件user.xml是怎么配置的?
    难道<import resource="application-user.xml">不起作用?把它都放到applictionContext.xml里看
      

  27.   

    错误开头还有反射和动态代理的错误,可能楼主变量名字起得不厚道,变量名都是首字母小写,
    java的反射机制是以getXXX()方法的get后面的字符串作为变量名,当然也是首字母小写.
    比如你的action类中,getUsersService()方法,反射后得到的变量名是userService,而你定义的变量名是UserService,这是两个不同的变量
    还有你定义各种bean的时候,命名还是不规范,首字母居然大写同类名。还有
    <bean id="userDao" class="com.bysj.dao.impl.IUserDao" scope="prototype">
    以楼主的命名来看,class是应该一个接口,不是一个实现类,不空才怪。
    我看楼主的错误起因就是命名不规范。
      

  28.   

    NullPointException个人觉得你需要检查几个地方:
    1、你的user对应的struts的配置文件中你自己检查一下,是否那个action的class的名称和spring你注入的名一致。
    2、如果还是不行,你把你的action的class修改为你该action的全路径名称再试试!
    3、在看看action中的service的get和set放名称是否和spring注入action中的ref的名称一致
    4、最好写个junit的测试代码在action中,看你可以直接调用到service中的方式。
      

  29.   

    恩,我注意到这个问题了,命名上有比较大的错误,我的get方法是工具生成的,自动生成是getUsersService(),没意识到要小写,不过我修改好之后又出现这样一个问题,是什么原因啊?
      

  30.   

    userDaoImpl里没有jdbcTemplate属性的set方法
      

  31.   

    路径错误吧。你把spring的配置也放到和struts,hibernate同级目录下,再检查下各大框架配置时的路径。