Spring 과 Struts 통합 방식 2
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Spring AOP 단위 테스트를 수행하는 방법Spring AOP로 만든 업무 횡단적인 처리를 가진 클래스를 단독으로 테스트하고 싶다. Spring AOP는 DI를 기반으로 성립되어 있어 컴퍼넌트를 사용하는 측이 proxy(컨테이너에 등록한 Bean 그 자체가 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.