前回作成したStruts2.2アプリにSpringを導入して、業務処理実装クラスで設定した値をJSPで表示するように改造しました。
また、不要なファイルの削除とURLに「.action」を付けなくても使えるように設定変更を行いました。
「.action」を外した場合の弊害として、HTML・画像・CSSなどの静的ファイルをここに置けなくなります。
まあ、この辺は別のプロジェクトを作る(サーバ上では静的に置いてApacheで処理する)のが良いと思っています。
設定変更
/src/main/resources/struts.xmlを追加
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.action.extension" value="" />
</struts>
不要な設定・ファイルを削除
/src/main/webapp/WEB-INF/web.xmlを編集
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="struts_blank" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank Convention</display-name>
<listener>
<listener-class>ap.InitListener</listener-class>
</listener>
<省略>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
下記のファイルを削除
/src/main/java/ap/InitListener.java
/src/main/java/ap/actions/HelloAction.java
/src/main/java/ap/actions/Index.java
/src/main/resources/ap/package_es.properties
/src/main/resources/ap/package.properties
/src/main/webapp/WEB-INF/content/hello.jsp
/src/main/webapp/WEB-INF/appengine-web.xml
/src/main/webapp/index.jsp
/src/test/java/ap/actions/HelloActionTest.java
/src/test/java/ap/actions/IndexTest.java
下記はエキスプローラーより削除
C:¥work¥workspace¥struts2¥src¥main¥webapp¥WEB-INF¥classes¥ap¥actions¥HelloAction.class
C:¥work¥workspace¥struts2¥src¥main¥webapp¥WEB-INF¥classes¥ap¥actions¥Index.class
C:¥work¥workspace¥struts2¥src¥main¥webapp¥WEB-INF¥classes¥ap¥InitListener.class
C:¥work¥workspace¥struts2¥src¥main¥webapp¥WEB-INF¥classes¥ap¥package_es.properties
Spring導入
/pom.xmlを右クリックして[Add Dependency]
org.apache.struts -> struts2-spring-plugin -> 2.2.3 org.springframework -> spring -> 2.5.6
/pom.xmlを編集
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<省略>
<properties>
<struts2.version>2.2.3</struts2.version>
<spring.version>2.5.6</spring.version>
</properties>
<省略>
<dependencies>
<省略>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.2.3</version>
<version>${struts2.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
<version>${spring.version}</version>
<type>pom</type>
</dependency>
</dependencies>
<省略>
</project>
コマンドプロンプト(cmd)
cd C:¥work¥workspace¥struts2 mvn war:inplace
※「[INFO] BUILD SUCCESS」と表示されればOK
プロジェクトを右クリックして[リフレッシュ]
/src/main/webapp/WEB-INF/web.xmlを編集
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="struts_blank" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank Convention</display-name>
<省略>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
</web-app>
/src/main/resources/applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<import resource="applicationContext-service.xml" />
</beans>
/src/main/resources/applicationContext-service.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="ap.service">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
</beans>
サンプル作成
/src/main/webapp/WEB-INF/content/index.jspを作成
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>テスト</title>
</head>
<body>
<p>サニタイズあり:<s:property value="message"/></p>
<p>サニタイズなし:${message}</p>
</body>
</html>
/src/main/java/ap/actions/IndexAction.javaを作成
package ap.actions;
import org.springframework.beans.factory.annotation.Autowired;
import ap.service.MessageService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
/**
* Index画面のActionクラス
* @author 作成者名
*/
public class IndexAction extends ActionSupport {
/** MessageService. */
@Autowired
private MessageService messageService;
/** メッセージ */
private String message;
/**
* Index画面の初期処理
* @return 遷移先
* @throws Exception
*/
@Override
public String execute() throws Exception {
// メッセージ取得&設定
this.setMessage(messageService.getMessage());
return Action.SUCCESS;
}
/**
* メッセージ取得
* @return message
*/
public String getMessage() {
return message;
}
/**
* メッセージ設定
* @param message
*/
public void setMessage(String message) {
this.message = message;
}
}
/src/main/java/ap/service/MessageService.javaを作成
package ap.service;
/**
* messageに関する業務処理インタフェイス
* @author 作成者名
*/
public interface MessageService {
/**
* メッセージ取得
* @return メッセージ
*/
String getMessage();
}
/src/main/java/ap/service/impl/MessageServiceImpl.javaを作成
package ap.service.impl;
import org.springframework.stereotype.Service;
import com.opensymphony.xwork2.ActionSupport;
import ap.service.MessageService;
/**
* messageに関する業務処理実装クラス
* @author 作成者名
*/
@Service
public class MessageServiceImpl extends ActionSupport implements MessageService {
/** メッセージ */
public static final String MESSAGE = "hello.message";
/**
* {@inheritDoc}
*/
@Override
public String getMessage() {
return getText(MESSAGE);
}
}
/src/main/resources/ap/package.propertiesを作成
hello.message=<b>こんにちは!</b>
動作確認
ServersタグのTomcat v7.0 Server at localhostを右クリックしてClean&Restart
http://localhost:8080/struts2/
※「こんにちは!」と表示されればOK
