JSP页面怎样调用Spring配置文件中定义的Bean
2014-07-14 18:10
525 查看
如果一个项目应用了Spring框架,实例化对象的过程也就交给了Spring了,也即一般在applicationContext.xml配置文件中配置类的依赖关系,这样做以后假如jsp页面上想要直接访问Service层的方法,由于service的实例化交给了Spring,所以也就不能new一个对象出来了即不能直接调用某个接口的实现类,它是通过注入的。这时如果想要访问Service中的方法该怎么办呢?
1、首先你需要有一个jar包,形如spring-web-3.2.0.M2.jar,将此包加入build Path并部署或者直接复制到WEB-INF/lib下,这是spring应用在web项目时需要用到的jar包
2、最后通过以下语句获取配置文件中相应的Bean
详细参考:http://jingyan.baidu.com/article/9113f81b2cb0822b3214c727.html
1、首先你需要有一个jar包,形如spring-web-3.2.0.M2.jar,将此包加入build Path并部署或者直接复制到WEB-INF/lib下,这是spring应用在web项目时需要用到的jar包
<span style="font-size:18px;"><%@page import="org.springframework.web.context.WebApplicationContext"%> <%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%></span>
2、最后通过以下语句获取配置文件中相应的Bean
<span style="font-size:18px;">WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext()); CourseService cs = (CourseService)wac.getBean("courseService"); Course model = cs.get(Integer.parseInt(courseId)); model.setDuration((int)ls); cs.update(model);</span>
详细参考:http://jingyan.baidu.com/article/9113f81b2cb0822b3214c727.html
相关文章推荐
- Spring 获取配置文件中定义的bean
- spring 实例化定义在xml配置文件中的bean的三种方法?
- 在JSP页面中调用Spring容器注入的Bean的2种方法
- Spring 全注解配置 bean 和 调用 (7) properties文件注入
- Spring-配置文件Bean定义中parent属性详解
- Spring在bean配置文件中定义电子邮件模板
- Spring 怎样加载多个bean 配置文件
- 《Spring 2.0 技术手册》读书笔记四-非XML的Bean定义文件配置
- 《Spring 2.0 技术手册》读书笔记四-非XML的Bean定义文件配置
- Spring Bean定义总结:配置文件手写Bean PK 组建扫描用注解
- JSP页面中调用Spring容器注入的Bean
- Spring-配置文件Bean定义中parent属性详解
- .Jsp 在页面浏览时弹出 未提供相应的服务,请检查Spring 配置文件
- 搭建SSH框架遇到的struts.xml找不到spring定义的bean的错误(配置文件的路径问题)
- 在JSP页面中调用Spring容器注入的Bean的2种方法
- 在JSP页面中调用Spring容器注入的Bean的2种方法
- JSP页面调用Spring容器注入的Bean
- 如何在JSP页面中调用Spring容器注入的Bean?
- Spring-配置文件Bean定义中parent属性详解
- Spring-配置文件Bean定义中parent属性详解