「Struts 2」の編集履歴(バックアップ)一覧はこちら
「Struts 2」(2006/10/20 (金) 13:07:33) の最新版変更点
追加された行は緑色になります。
削除された行は赤色になります。
- Tomcat project を作り、Struts support, Spring nature をつける
- コンパイル先が WEB-INF/classes になっていなければ、このフォルダを作り、コンパイル先に指定する。
- WEB-INF/jsp を作る
- WEB-INF/web.xml にdescription がなければ追加しておく
<web-app>
<description>
Sample to use JSP, Struts, Spring, Hibernate, etc for
JVM stress tests with 1.3 and later.
</description>
<display-name>Misc web components for tests</display-name>
...
- index.jsp を作り、welcome アクションへのフォワードを指定する
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<logic:redirect forward="welcome"/>
- struts-config.xml に welcome アクションを定義する
<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
...
<action-mappings>
<action path="/Welcome"
type="org.apache.struts.actions.ForwardAction"
parameter="/WEB-INF/jsp/Welcome.jsp">
</action>
...
- Welcome.jsp を作成
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
body の先頭に次のチェックを入れておく
<logic:notPresent name="org.apache.struts.action.MESSAGE"
scope="application">
<b>ERROR: Application resources not loaded --
check servlet container logs for error messages.
</b>
</logic:notPresent>
forward 先として使用されるのであれば次のタグも入れておく
<html:errors/>
- css/global.css を作成, header 内でインクルード
body {margin: 0; padding: 0; background: #fff; text-align: center; font: 12px 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; color: #000;}
table, td {font: 10px 'Lucida Grande', Geneva, Verdana, Arial, sans-serif; color: #000;}
----
<link rel="stylesheet" type="text/css" href="/css/global.css" media="all">
- 一旦デプロイして表示を確認
- フォームを設置
<html:form action="/DirDiffCheck" focus="sourcepath">
<table border="0" width="100%">
<tr>
<th align="right">Source:</th>
<td align="left"><html:text property="sourcePath"/></td>
<td align="left"><html:submit property="sourcepath_choose" value="Choose..."/></td>
</tr>
<tr>
<th align="right">Destination:</th>
<td align="left"><html:text property="destPass"/></td>
<td align="left">
<html:form action="/ChoosePath">
<html:submit property="targetpath_choose" value="Choose..."/>
</html:form>
</td>
</tr>
<tr>
<td align="right"><html:submit property="submit" value="Submit"/></td>
<td align="left"><html:reset/></td>
</tr>
</table>
</html:form>
- DirDiffCheck アクションを定義
<form-beans>
<form-bean name="ddcForm" type="app.ddcForm"/>
...
<action-mappings>
...
<action path="/DirDiffCheck"
name="ddcForm"
type="app.DirDiffCheckAction"
scope="request" validate="true"
input="/WEB-INF/jsp/Welcome.jsp">
<forward name="success" path="/WEB-INF/jsp/Welcome.jsp"/>
<forward name="dchoose" path="/WEB-INF/jsp/DirChoose.jsp"/>
</action>
- app.ddcFrom を作成, ActionFrom のサブクラスとする
-- 次のプロパティを用意する
private String sourcePath;
private String destPath;
-- validate メソッドを用意する
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((sourcePath == null) || (sourcePath.length() < 1)) {
errors.add("sourcePath", new ActionMessage("error.sourcePath.required"));
}
if ((destPath == null) || (destPath.length() < 1)) {
errors.add("destPath", new ActionMessage("error.destPath.required"));
}
return errors;
}
error.sourcePath.required=Soruce path is required
error.destPath.required=Dest path is required
- JSTL の使用
-- taglig 定義
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
-- jstl.jar, standard.jar を lib に追加
- Logging
commons-logging は Struts 必須のため、利用できるが、カテゴリ、レベルの
制御を考えると log4j に慣れている場合は log4j を用いたほうが早い。
また log4j.jar は WEB-INF/lib に配置する。コンテナの commons/lib では
ClassNotFoundException がおきる(らしい)
log4j.xml または log4j.properties は WEB-INF/classes に配置する。
log4j.xml の場合は log4j.configuration の設定がおそらく必要。
log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p [%t] (%F:%L) - %m%n"/>
</layout>
</appender>
<root>
<priority value="INFO" />
<appender-ref ref="stdout" />
</root>
</log4j:configuration>
log4j.properties
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# log4j.rootLogger=DEBUG, stdout, rolling
# log4j.appender.rolling=org.apache.log4j.RollingFileAppender
# log4j.appender.rolling.File=output.log
# log4j.appender.rolling.MaxFileSize=512KB
# log4j.appender.rolling.MaxBackguIndex=1
# log4j.appender.rolling.layout=org.apache.log4j.PatternLayout
# log4j.appender.rolling.layout.ConversionPattern=%5p [%t] (%F:%L)
# category setting
# log4j.logging.<pkg/class filter>=level
log4j.logging.app.pathedit=DEBUG
- JSP 内でのロギング
-- Scriptlet の使用
<%@ page import="org.apache.commons.logging.Log" %>
<%@ page import="org.apache.commons.logging.LogFactory" %>
<% Log logger = LogFactory.getLog(this.getClass()); %>
<% logger.debug("This is a debug message from a jsp"); %>
...
<% logger.info("This is another log message in the jsp"); %>
-- Log custom tag の利用
<taglib>
<taglib-uri>/WEB-INF/log.tld</taglib-uri>
<taglib-location>/WEB-INF/log.tld</taglib-location>
</taglib>
このタグライブラリは log4j 依存の様だ。
<% taglib uri="/WEB-INF/log.tld" prefix="logger" %>
:
<logger:debug message="This is a debug message from a jsp using the Log tag" />
:
<logger:info message="This is another message using the log4j tag" />
:
<logger:dump scope="page"/>
<logger:dump scope="request"/>
<logger:dump scope="session"/>
<logger:dump scope="application"/>
- Tomcat project を作り、Struts support, Spring nature をつける
- コンパイル先が WEB-INF/classes になっていなければ、このフォルダを作り、コンパイル先に指定する。
- WEB-INF/jsp を作る
- WEB-INF/web.xml にdescription がなければ追加しておく
<web-app>
<description>
Sample to use JSP, Struts, Spring, Hibernate, etc for
JVM stress tests with 1.3 and later.
</description>
<display-name>Misc web components for tests</display-name>
...
- index.jsp を作り、welcome アクションへのフォワードを指定する
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<logic:redirect forward="welcome"/>
- struts-config.xml に welcome アクションを定義する
<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
...
<action-mappings>
<action path="/Welcome"
type="org.apache.struts.actions.ForwardAction"
parameter="/WEB-INF/jsp/Welcome.jsp">
</action>
...
- Welcome.jsp を作成
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
body の先頭に次のチェックを入れておく
<logic:notPresent name="org.apache.struts.action.MESSAGE"
scope="application">
<b>ERROR: Application resources not loaded --
check servlet container logs for error messages.
</b>
</logic:notPresent>
forward 先として使用されるのであれば次のタグも入れておく
<html:errors/>
- css/global.css を作成, header 内でインクルード
body {margin: 0; padding: 0; background: #fff; text-align: center; font: 12px 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; color: #000;}
table, td {font: 10px 'Lucida Grande', Geneva, Verdana, Arial, sans-serif; color: #000;}
----
<link rel="stylesheet" type="text/css" href="/css/global.css" media="all">
- 一旦デプロイして表示を確認
- フォームを設置
<html:form action="/DirDiffCheck" focus="sourcepath">
<table border="0" width="100%">
<tr>
<th align="right">Source:</th>
<td align="left"><html:text property="sourcePath"/></td>
<td align="left"><html:submit property="sourcepath_choose" value="Choose..."/></td>
</tr>
<tr>
<th align="right">Destination:</th>
<td align="left"><html:text property="destPass"/></td>
<td align="left">
<html:form action="/ChoosePath">
<html:submit property="targetpath_choose" value="Choose..."/>
</html:form>
</td>
</tr>
<tr>
<td align="right"><html:submit property="submit" value="Submit"/></td>
<td align="left"><html:reset/></td>
</tr>
</table>
</html:form>
- DirDiffCheck アクションを定義
<form-beans>
<form-bean name="ddcForm" type="app.ddcForm"/>
...
<action-mappings>
...
<action path="/DirDiffCheck"
name="ddcForm"
type="app.DirDiffCheckAction"
scope="request" validate="true"
input="/WEB-INF/jsp/Welcome.jsp">
<forward name="success" path="/WEB-INF/jsp/Welcome.jsp"/>
<forward name="dchoose" path="/WEB-INF/jsp/DirChoose.jsp"/>
</action>
- app.ddcFrom を作成, ActionFrom のサブクラスとする
-- 次のプロパティを用意する
private String sourcePath;
private String destPath;
-- validate メソッドを用意する
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((sourcePath == null) || (sourcePath.length() < 1)) {
errors.add("sourcePath", new ActionMessage("error.sourcePath.required"));
}
if ((destPath == null) || (destPath.length() < 1)) {
errors.add("destPath", new ActionMessage("error.destPath.required"));
}
return errors;
}
error.sourcePath.required=Soruce path is required
error.destPath.required=Dest path is required
- JSTL の使用
-- taglig 定義
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
-- jstl.jar, standard.jar を lib に追加
- Logging
commons-logging は Struts 必須のため、利用できるが、カテゴリ、レベルの
制御を考えると log4j に慣れている場合は log4j を用いたほうが早い。
また log4j.jar は WEB-INF/lib に配置する。コンテナの commons/lib では
ClassNotFoundException がおきる(らしい)
log4j.xml または log4j.properties は WEB-INF/classes に配置する。
log4j.xml の場合は log4j.configuration の設定がおそらく必要。
log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p [%t] (%F:%L) - %m%n"/>
</layout>
</appender>
<root>
<priority value="INFO" />
<appender-ref ref="stdout" />
</root>
</log4j:configuration>
log4j.properties
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# log4j.rootLogger=DEBUG, stdout, rolling
# log4j.appender.rolling=org.apache.log4j.RollingFileAppender
# log4j.appender.rolling.File=output.log
# log4j.appender.rolling.MaxFileSize=512KB
# log4j.appender.rolling.MaxBackguIndex=1
# log4j.appender.rolling.layout=org.apache.log4j.PatternLayout
# log4j.appender.rolling.layout.ConversionPattern=%5p [%t] (%F:%L)
# category setting
# log4j.logging.<pkg/class filter>=level
# log4j.logging.app.pathedit=DEBUG !!!BAD
# log4j.category.app.pathedit=DEBUG
- JSP 内でのロギング
-- Scriptlet の使用
<%@ page import="org.apache.commons.logging.Log" %>
<%@ page import="org.apache.commons.logging.LogFactory" %>
<% Log logger = LogFactory.getLog(this.getClass()); %>
<% logger.debug("This is a debug message from a jsp"); %>
...
<% logger.info("This is another log message in the jsp"); %>
-- Log custom tag の利用
<taglib>
<taglib-uri>/WEB-INF/log.tld</taglib-uri>
<taglib-location>/WEB-INF/log.tld</taglib-location>
</taglib>
このタグライブラリは log4j 依存の様だ。
<% taglib uri="/WEB-INF/log.tld" prefix="logger" %>
:
<logger:debug message="This is a debug message from a jsp using the Log tag" />
:
<logger:info message="This is another message using the log4j tag" />
:
<logger:dump scope="page"/>
<logger:dump scope="request"/>
<logger:dump scope="session"/>
<logger:dump scope="application"/>