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.w)3codebox.Loginとidは、struts.xmlファイルのアクションクラス(例えばlogin)と一致する必要があります。 アクションクラスで、メッセージなどの他の属性を定義します。

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 リスナークラスは、struts2和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 とメッセージの属性を定義し、execute メソッドを返す成功を定義しています。

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 メソッドには何もロジックを定義していないからです。

ごめんなさい!

出力