English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring Struts 통합

Spring 프레임워크는 의존성을 관리하는 간편한 방법을 제공합니다. 그것은 struts와 쉽게 통합할 수 있습니다. 2프레임워크와 통합됩니다.

ContextLoaderListener 클래스는 Struts와 2Spring 애플리케이션과의 통신. web.xml 파일에서 지정해야 합니다.


다음 단계를 수행해야 합니다:

struts2애플리케이션을 생성하고 spring jar 파일을 추가합니다. 에서 web.xml 파일에서 ContextLoaderListener 클래스를 정의합니다. 에서 struts.xml 파일에서 작업 클래스에 bean 이름을 정의합니다. 에서 applicationContext.xml 파일에서 Bean을 생성합니다. 그 클래스 이름은 액션 클래스 이름이어야 합니다. 예를 들어 com.w3codebox.Login과 id는 struts.xml 파일의 작업 클래스(예: login)와 일치해야 합니다. 에서 action 클래스에서, 메시지와 같은 다른 속성을 정의합니다.

Spring과 Struts 2통합 예제

작업을 간소화하기 위해 다음 파일을 생성해야 합니다: spring and struts 2애플리케이션:

index.jsp web.xml struts.xml applicationContext.xml Login.java welcome.jsp error.jsp

1)index.jsp

이 페이지는 사용자 이름을 사용자로부터 가져옵니다.

<%@ taglib uri="/struts-tags" prefix="s"%>
<s:form action="login">
<s:textfield name="userName" label="UserName"></s:textfield>
<s:submit></s:submit>
</s:form>

2)web.xml

그것은 struts에 2와 ContextLoaderListener 리스너 클래스는 struts에서 컨트롤러 클래스를 정의하여2와 spring 애플리케이션 간에 연결을 설정합니다.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>
  
<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
  
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  </web-app>

3)struts.xml

그것은 작업과 결과를 포함한 패키지를 정의합니다. 여기서 액션 클래스 이름은 login이며 applicationContext.xml 파일에서 검색됩니다.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts public "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="abc" extends="struts-default">
<action name="login" class="login">
<result name="success">welcome.jsp</result>
</action>
</package>
</struts>

4)applicationContext.xml

ID 로그인 이름을 가진 bean을 정의합니다. 이 bean은 mypack.Login 클래스와 일치합니다.

그것은 WEB-INF 디렉토리에 있습니다.

<?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:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="login" class="mypack.Login">
<property name="message" value="Welcome Spring"></property>
</bean>
</beans>

5)Login.java

그것은 userName과 실행 메서드를 가진 메시지 두 가지 속성을 정의합니다. 실행 메서드는 성공을 반환합니다.

package mypack;
public class Login {
private String userName,message;
public String getMessage() {
  return message;
}
public void setMessage(String message) {
  this.message = message;
}
public String getUserName() {
  return userName;
}
public void setUserName(String userName) {
  this.userName = userName;
}
public String execute(){
  return "success";
}
}

6)welcome.jsp

그는 userName과 메시지 속성의 값을 표시합니다.

<%@ taglib uri="/struts-tags" prefix="s"%>
Welcome, <s:property value="userName"/><br/>
${message}

7)error.jsp

이는 오류 페이지입니다. 하지만 필요하지 않습니다. 왜냐하면 우리는 액션 클래스의 execute 메서드에서 어떤 로직도 정의하지 않았기 때문입니다.

죄송합니다!

출력