spring与struts1集成方案(一)

该方案并未将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 {

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		return 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>

测试

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值