Struts2 控制標簽:


單獨使用<s:if>標簽<s:if test="%{#variable=='String 1'}">
  This is String 1
</s:if>

也可以和<s:elseif>標簽一起使用:

<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:else>標簽:

<s:if>+<s:elseif>+<s: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>

上面的這些語句都是正確的。下面通過示例程序來學習<s:if>、<s:elseif>和<s:else>標簽的用法。

1、Action

IfTagAction類,帶有一個String屬性,該屬性包含有字符串值“Struts 2”。

IfTagAction.javapackage com.xuejava.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、JSP頁面

一個JSP頁面,使用<s:if>、<s:elseif>和<s:else>標簽來執行對“framework”變量的條件檢查。

if.jsp<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %> <html>
<head>
</head>

<body>
   <h1>Struts 2 If, Else, ElseIf tag 示例</h1>

  <s:set name="webFramework" value="framework"/>

  <s:if test="%{#webFramework=='Struts 2'}">
    這是Struts 2
  </s:if>
  <s:elseif test="%{#webFramework=='Struts 1'}">
    這是Struts 1
  </s:elseif>
  <s:else>
    其它框架
  </s:else> 

</body>
</html>3、struts.xml

使用struts.xml將所有的東西鏈在一起。

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.xuejava.common.action.IfTagAction" >
     <result name="success">pages/if.jsp</result>
   </action> 
</package>

</struts>4、測試

訪問URL:http://localhost:8080/Struts2Example/ifTagAction.action。

>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM