spring与struts1集成方案(一)
来源:互联网 发布:算法 第四版 pdf 微盘 编辑:程序博客网 时间:2024/06/11 19:41
该方案并未将struts交与spring管理,仅仅是将struts单独植入。
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.5"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!--指定spring配置文件位置,默认从web根目录寻找配置文件。另外可以根据spring提供的classpath:前缀来指定从类路径下查找 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:spring/*.xml</param-value></context-param><!-- 初始化spring容器(通过监听):ContextLoader是一个工具类,用来初始化WebApplicationContext,其主要方法就是initWebApplicationContext,主要的工作是把WebApplicationContext(XmlWebApplicationContext是默认实现类)放在了ServletContext中;ServletContext也是一个"容器",也是一个类似Map的结构.而WebApplicationContext在ServletContext中的KEY就是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE.我们如果要使用WebApplicationContext则需要从ServletContext取出,Spring提供了一个WebApplicationContextUtils类,可以方便的取出WebApplicationContext,只要把ServletContext传入就可以了。--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>action</servlet-name><servlet-class>org.apache.struts.action.ActionServlet</servlet-class><init-param><param-name>config</param-name><param-value>/WEB-INF/struts-config.xml</param-value></init-param><init-param><param-name>debug</param-name><param-value>3</param-value></init-param><init-param><param-name>detail</param-name><param-value>3</param-value></init-param><load-on-startup>0</load-on-startup></servlet><servlet-mapping><servlet-name>action</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
spring配置文件、service层未改动,参见spring与hibernate集成.
action
package com.ch.action;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.actions.DispatchAction;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;import org.springframework.web.context.support.XmlWebApplicationContext;import com.ch.dao.IPersonService;import com.ch.entity.Person;public class PersonAction extends DispatchAction {@Overrideprotected ActionForward unspecified(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {// TODO Auto-generated method stubreturn list(mapping, form, request, response);}@SuppressWarnings("unchecked")public ActionForward list(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {// TODO Auto-generated method stub/** * 在web应用中,spring容器在启动的时候就被初始化(参见web.xml)。 * 所以我们可以通过spring的工具类WebApplicationContextUtils来获取上下文 */WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());IPersonService personService2 = (IPersonService) wc.getBean("personService");List<?> list = (List<Person>) personService2.findAllPersons();request.setAttribute("list", list);return mapping.findForward("list");}}
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"><struts-config> <form-beans /> <global-exceptions /> <global-forwards /> <message-resources parameter="struts.ApplicationResources" /> <action-mappings> <action path="/person/list" type="com.ch.action.PersonAction" validate="false" parameter="method"> <!-- jsp部署在WEB-INF下,防止用户直接访问jsp --> <forward name="list" path="/WEB-INF/person/list.jsp"></forward> </action> </action-mappings></struts-config>
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%><%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%><%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%><%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP</title> </head> <body> <nested:present name="list"> <table> <nested:iterate name="list" id="person" indexId="index"> <tr> <td>${index}</td> <td><nested:write property="personId" name="person"/></td> <td><nested:write property="personName" name="person"/></td> </tr> </nested:iterate> </table> </nested:present> </body></html>
测试
- spring与struts1集成方案(一)
- spring与struts1集成方案(二)
- Struts1.X与Spring集成——第一种方案
- Struts1.X与Spring集成——第二种方案
- Spring与Struts1的集成
- Spring与Struts1的集成
- Spring与Struts1的集成
- struts1与spring的集成
- Struts1与Spirng的集成方案:(最佳方案)
- Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
- struts与spring集成方案
- Struts1.x/2.x 与 Spring集成方式
- 深入探讨Spring与Struts集成方案
- 深入探讨Spring与Struts集成方案
- 探讨Spring与Struts 的集成方案
- struts1.x+spring+hibernate集成
- struts1集成spring的方式
- 深入探讨Spring与Struts的集成方案深入探讨Spring与Struts的集成方案
- Vim 中文乱码及菜单乱码的解决方法
- tomcat配置虚拟主机
- NS-3中Ipv4Header说明
- Prerequisite installation failed: SqlExpressRtc
- 构建基于.NET的管理信息系统
- spring与struts1集成方案(一)
- oracle数据类型
- Linux下halt,poweroff,reboot,shutdown命令的区别
- .NET程序员,我们应该更专业点
- sql语句,exec执行 拼接的字符串 输出参数 问题
- 通达OA 有时在线时长也需要改一下,这个需求有时也会有
- 20个常用的linux系统管理工具
- Hibernate工具类丰富和Hql用法 对象属性名与关键字冲突
- Android2.3 中如何使用GPU硬件加速