Spring 과 Struts 통합 방식 2

통합 방식 1 을 바탕 으로 개선:
         action   
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
 UserDao userDao = (UserDao)wac.getBean("userDao");      
   。

         spring  ,  action   spring     ,      dao    action bean     。
            applicationContext-actions.xml

	<bean name="loginAction" class="com.lwf.spring.web.action.LoginAction">
		<property name="userDao" ref="userDao"></property>
	</bean>
	       struts action,     ,       spring action     
	       :
		             struts-config.xml  path applicationContext-actions.xml  bean name  。
	    ActionServlet   struts-config.xml  path, path     ,     spring   action,   spring      name path    。
		
		   :     action :DelegatingActionProxy
		  :  spring   action,   struts-config  action type  org.springframework.web.struts.DelegatingActionProxy
	   spring     spring-framework-2.5.6.SEC01\dist\modules    :spring-webmvc-struts.jar
	 :<action path="/login" 
			type="org.springframework.web.struts.DelegatingActionProxy"
			name="loginForm">
			<forward name="success" path="/login_success.jsp"></forward>	
		</action>
		
		     spring       scope="prototype"            ,       struts              。
		
		   :  spring   DelegatingRequestProcessor  struts RequestProcessor。
		  :    struts-config.xml       DelegatingRequestProcessor,      <controller>     “processorClass”   。           <action-mapping>      。 
			<controller>
			  <set-property property="processorClass"
			      value="org.springframework.web.struts.DelegatingRequestProcessor"/>
			</controller>
		        ,           Action,Sping      context       。    ,          。             : 
		<action path="/user" type="com.whatever.struts.UserAction"/>		
		<action path="/user"/>
		     Struts   modules   ,  bean        module    。     ,     Action      <action path="/user"/>,     module    “admin”,           <bean name="/admin/user"/>   bean。 

	          LoginAction ,    userDao.add(name, pwd);      WebApplicationContext  dao     。

 
설정 이 끝 난 코드 를 보십시오:
action 코드:
package com.lwf.spring.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.lwf.spring.web.dao.UserDao;
import com.lwf.spring.web.form.LoginForm;

public class LoginAction extends Action {

	private UserDao userDao;
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		LoginForm loginForm = (LoginForm)form;
		String name = loginForm.getName();
		String pwd = loginForm.getPassword();
		
		userDao.add(name, pwd);
		
		return mapping.findForward("success");
	}
	
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

}

 
웹. xml 은 변 하지 않 았 습 니 다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="spring" version="2.5">
  <display-name>TestWeb</display-name>
  <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>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
		<form-bean name="loginForm" type="com.lwf.spring.web.form.LoginForm"></form-bean>
	</form-beans>
	<action-mappings>
		<action path="/loginjsp" forward="/login.jsp"></action>
		<action path="/login" 
			type="org.springframework.web.struts.DelegatingActionProxy"
			name="loginForm">
			<forward name="success" path="/login_success.jsp"></forward>	
		</action>
	</action-mappings>
	<message-resources parameter="MessageResources"></message-resources>
</struts-config>


 
새 applicationContext - actions. xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
		
>
	
	<bean name="/login" class="com.lwf.spring.web.action.LoginAction" scope="prototype">
		<property name="userDao" ref="userDao"></property>
	</bean>
</beans>

 
원래 applicationContext - beans. xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
		
>
	<bean id="userDao" class="com.lwf.spring.web.dao.UserDaoImpl" ></bean>
</beans>

 
다른 파일 은 변 하지 않 습 니 다.
구체 적 인 코드 는 spring 참조struts_2

좋은 웹페이지 즐겨찾기