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

Integrazione di Struts di Spring

Spring框架提供了一种管理依赖项的简便方法。它可以很容易地与struts 2框架集成。

ContextLoaderListener 类用于与Struts 2通信Spring应用程序。必须在web.xml文件中指定。


您需要执行以下步骤:

创建struts2应用程序并添加spring jar文件。 web.xml 文件中,定义ContextLoaderListener类。 struts.xml 文件中,为操作类定义bean名称。 applicationContext.xml 文件中,创建Bean。其类别名称应为动作类别名称,例如com.w3codebox.Login和id应该与struts.xml文件的操作类(例如login)匹配。 action类在中,定义其他属性,例如消息。

Esempio di integrazione di Spring e Struts 2

Devi creare i seguenti file per semplificare l'operazione dell'applicazione spring and struts 2:

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

1)index.jsp

Questa pagina recupera il nome dall'utente.

<%@ 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

per struts 2 e ContextLoaderListener La classe ascoltatore definisce la classe controller per stabilire una connessione tra l'applicazione struts2 e 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

Definisce un pacchetto che contiene azioni e risultati. In questo caso, il nome della classe dell'azione è login, che verrà cercato nel file 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

Definisce un bean con ID nomeLogin. Questo bean corrisponde alla classe mypack.Login.

Deve trovarsi nella directory 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="Benvenuto Spring"></property>
</bean>
</beans>

5)Login.java

Definisce due proprietà userName e un messaggio con il metodo execute, che restituisce con successo.

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

Mostra i valori delle proprietà userName e messaggio.

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

7)error.jsp

Questa è una pagina di errore. Ma non è necessario, perché non abbiamo definito alcuna logica nel metodo execute della classe di azione.

Scusa!

Output