Struts 2 If、ElseIf、Elseタグの例
ダウンロード – リンク://wp-content/uploads/2010/07/Struts2-If-ElseIf-Else-Tag-Example.zip[Struts2-If-ElseIf-Else-Tag-Example.zip]
Struts 2
If、ElseIfおよびElse
タグを使用して基本条件チェックを実行します。
‘
If
‘タグは単独で使用できます
<s:if test="%{#variable=='String 1'}">
This is String 1
</s:if>
または ‘
Else If
‘タグ
<s:if test="%{#variable=='String 1'}">
This is String 1
</s:if>
<s:elseif test="%{#variable=='String 2'}">
This is String 2
</s:elseif>
および/または単一の/複数の ‘
Else
‘タグ。
<s:if test="%{#variable=='String 1'}">
This is String 1
</s:if>
<s:elseif test="%{#variable=='String 2'}">
This is String 2
</s:elseif>
<s:else>
Other Strings
</s:else>
上記の記述はすべて正しいです。 Struts 2 ‘
If、ElseIfおよびElse
タグの使用例を見てみましょう。
1.アクション
”
Struts 2
“値を含むStringプロパティを持つActionクラス。
package com.mkyong.common.action;
import com.opensymphony.xwork2.ActionSupport;
public class IfTagAction extends ActionSupport{
private String framework = "Struts 2";
public String getFramework() {
return framework;
}
public void setFramework(String framework) {
this.framework = framework;
}
public String execute() {
return SUCCESS;
}
}
2. ElseIfおよびElseタグの例の場合
-
If、ElseIfおよびElse
タグを使用して、 ”
framework ** “変数の条件チェックを実行するJSPページ。 -
if.jsp **
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<h1>Struts 2 If, Else, ElseIf tag example</h1>
<s:set name="webFramework" value="framework"/>
<s:if test="%{#webFramework=='Struts 2'}">
This is Struts 2
</s:if>
<s:elseif test="%{#webFramework=='Struts 1'}">
This is Struts 1
</s:elseif>
<s:else>
Other framework
</s:else>
</body>
</html>
3. 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.devMode" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<action name="ifTagAction"
class="com.mkyong.common.action.IfTagAction" >
<result name="success">pages/if.jsp</result>
</action>
</package>
</struts>
リファレンス
-
http://struts.apache.org/2.0.14/docs/if.html
[Struts 2タグの場合
ドキュメンテーション]。
http://struts.apache.org/2.0.14/docs/elseif.html
[Struts 2 ElseIfタグ
ドキュメンテーション]。
http://struts.apache.org/2.0.14/docs/else.html
[Struts 2 Else
ドキュメンテーション]
