struts2 <s:if>, <s:elseif>, <s:else>标签示例用于执行基本的条件检查。这里创建一个web工程:strut2iftag,来演示在多个复选框如何设置的默认值,整个项目的结构如下图所示:
<s:if test="%{#variable=='string 1'}"> this is string 1 </s:if>
或使用 <s:elseif> 标签
<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: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>
iftagaction
package com.h3.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; } }
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>
<?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.h3.common.action.iftagaction" > <result name="success">pages/if.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2iftag/iftagaction.action