spring中如何实现参数隐式传递_如何在Action中隐式调用Spring容器中定义的bean??...

新手,刚学习spring,整合struts+spring,总是不能成功,下面是报错信息:

java.lang.NullPointerException

com.ssh3.struts.action.HelloAction.execute(HelloAction.java:69)

org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)

org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)

org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)

org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)

javax.servlet.http.HttpServlet.service(HttpServlet.java:709)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

struts-config.xml内容如下,通过plug-in,在web应用启动时创建spring容器:

      attribute="helloForm"

input="/form/hello.jsp"

name="helloForm"

path="/hello"

scope="request"

type="com.ssh3.struts.action.HelloAction">

HelloAction源代码如下:

package com.ssh3.struts.action;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ssh3.dao.StudentDAO;

import com.ssh3.struts.form.HelloForm;

public class HelloAction extends Action {

private StudentDAO studentDao;

public StudentDAO getStudentDao() {

return studentDao;

}

public void setStudentDao(StudentDAO studentDao) {

this.studentDao = studentDao;

}

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

HelloForm helloForm = (HelloForm) form;// TODO Auto-generated method stub

//通过直接创建ctx,可以正确执行        studentDAO.sayHello(),

//                ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

//                StudentDAO studentDAO = (StudentDAO) ctx.getBean("StudentDAO");

//

//                             System.out.println(studentDAO.sayHello());

//通过spring的IOC,报错

System.out.println(studentDao.sayHello());

return mapping.findForward("success");

}

}

applicationContext.xml,代码如下:

  Baby!

StudentDAO代码如下(并没有访问数据库,只是为了测试spring):

package com.ssh3.dao;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public class StudentDAO  {

private static final Log log = LogFactory.getLog(StudentDAO.class);

private String msg;

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

public String sayHello(){

return "Hello" + msg;

}

}

我的目的是不想在Action中通过创建ctx,在调用ctx.getBean("XX")方法来取得容器中的bean,看了好多资料说通过配置struts-config.xml或web.xml就可以隐式创建spring容器,但是在Action中我却无法使用applicationContext.xml中的bean。

请帮帮忙,指点一下,我哪里错了。

问题补充:

难道大家在实际项目中都显式的调用applicationContext.xml中定义的bean??

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值