开发者社区> 问答> 正文

SSH整合 属性对象注入 失败报空指针异常

SSH框架整合,Spring只使用核心模块IOC
applicationContext.xml配置如下:

<bean id="userServiceImpl" class="com.founder.ssj.service.impl.UserServiceImpl"    >
</bean>
<bean id="loginAction"      class="com.founder.ssj.action.user.LoginAction"       >
    <property name="userService">
        <ref bean="userServiceImpl"/>
    </property>
</bean>

web.xml配置如下:

<!-- Spirng配置文件位置 -->

contextConfigLocation
classpath:applicationContext.xml

<!-- 加载 Spring -->


org.springframework.web.context.ContextLoaderListener



struts2

org.apache.struts2.dispatcher.FilterDispatcher



struts2
/*

struts配置如下:

<package name="main"  extends="struts-default" >
    <action name="login" class="com.founder.ssj.action.user.LoginAction" >
        <result name="success">/index.jsp</result>
        <result name="fail">/login.jsp</result>
    </action>
</package>

Action 代码:GET SET 都全了

public class LoginAction extends ActionSupport{
//为何没有被注入?
private IUserService userService;


private String username;
private String password;

public LoginAction() {

    super();
    System.out.println(2);
}

/**
 * 
 */
private static final long serialVersionUID = 835443313500278156L;


@Override
public String execute(){

    String state="fail";
    try {
        if(userService.login(username, password)){
            state="success";
        };
    } catch (Exception e) {
        e.printStackTrace();
    }
    return state;
}

每当执行到该方法时,userService 对象都是空?
为什么呢?

展开
收起
小旋风柴进 2016-03-03 11:34:41 3160 0
1 条回答
写回答
取消 提交回答
  • 在struts.xml中加入

    <constant name="struts.objectFactory" value="spring" />

    然后把
    <action name="login" class="com.founder.ssj.action.user.LoginAction" >
    改成
    <action name="login" class="loginAction" >

    2019-07-17 18:51:09
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
建立联系方法之一 立即下载
JAVA反射原理以及一些常见的应用 立即下载
低代码开发师(初级)实战教程 立即下载