ITeye博客

spring hibernate struts 整合开发(3) - Struts集成Spring

czj4451 · 2012-05-16 22:07 · spring/ejb · 企业架构 · Spring · Struts · Hibernate · JSP · Web · Markdown

[url="http://czj4451.iteye.com/blog/1526897"]一. spring hibernate struts 整合开发(1) - 搭建环境[/url] [url="http://czj4451.iteye.com/blog/1528634"]二. spring hibernate struts 整合开发(2) - Spring集成的Hibernate编码和测试[/url] 三. spring hibernate struts 整合开发(3) - Struts集成Spring [url="http://czj4451.iteye.com/blog/1535063"]四. spring hibernate struts 整合开发(4) - Struts与Spring集成2[/url] [url="http://czj4451.iteye.com/blog/1535105"]五. spring hibernate struts 整合开发(5) - Hibernate二级缓存[/url] [url="http://czj4451.iteye.com/blog/1535279"]六. spring hibernate struts 整合开发(6) - 额外功能[/url] [size=small]1. 在web容器中实例化spring容器[/size] spring自带的实例化容器方法是: [code="java"] AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); [/code] 在web.xml中,通过配置listener和context param: [code="xml"] contextConfigLocation classpath:beans.xml org.springframework.web.context.ContextLoaderListener [/code] 查看ContextLoaderListener源码,可以看到spring把上下文放到servletContext里: [code="java"] servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); [/code] [size=small]2. Struts配置[/size] a. 在web.xml中,加入struts配置段: [code="xml"] struts org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml 0 struts *.do [/code] [size=small]b. 新建struts-config.xml文件,加入下面配置:[/size] [code="xml"] [/code] 3. 新建com.john.web.action.PersonAction类,重载execute方法: [code="java"] public class PersonAction extends Action{ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Spring provides us a util to get the context WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext()); PersonService personService = (PersonService)ctx.getBean("personService"); request.setAttribute("persons", personService.getPersons()); return mapping.findForward("list"); } } [/code] [size=small]4. 新建person_list.jsp文件,加入下面内容:[/size] [code="html"] <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> ${person.id} ${person.name} [/code] 开启Web服务(Tomcat, jetty等)测试 整理自:传智播客spring教程

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。