水神骑士联盟吧 关注:4贴子:334
  • 1回复贴,共1

当使用spring,struts整合包后

只看楼主收藏回复

action不交给spring IOC容器管理,spring也会为action注入依赖关系例如:


IP属地:河北1楼2016-05-27 16:33回复
    <package name="testPackage" extends="struts-default">
    <action name="test-*" method="{1}" class="junit.test.action.TestAction">
    <result name="success">/index.jsp</result>
    </action>
    </package>
    这里test的创建管理由struts自己处理
    public class TestAction extends ActionSupport {
    private TestService testService ;
    public void setTestService(TestService testService) {
    this.testService = testService;
    }
    @Override
    public String execute() throws Exception {
    testService.printf() ;
    return SUCCESS;
    }
    }
    这里不会出现空指针异常,因为spring与struts的整合包会在IOC容器中查找可以注入set方法的bean并注入对象所以不会出现错误,但是不推荐这么做(建议action交给spring管理,并手动注入关系)


    IP属地:河北2楼2016-05-27 16:38
    回复