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>

測試

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章