Struts 2 If,ElseIf,Else标签示例

下载它– 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.行动

具有String属性的Action类,其中包含“ Struts 2 ”值。

IfTagAction

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. If,ElseIf和Else标签示例

一个JSP页面,显示使用If,ElseIf和Else标记对“ framework ”变量执行条件检查。

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>

4.演示

http:// localhost:8080 / Struts2Example / ifTagAction.action

Struts 2 If, ElseIf, Else Tag

参考

  1. Struts 2 If标签文档
  2. Struts 2 ElseIf标签文档
  3. Struts 2其他文档

翻译自: https://mkyong.com/struts2/struts-2-if-elseif-else-tag-example/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值