zoukankan      html  css  js  c++  java
  • Struts2 控制标签:<s:if>、<s:elseif>和<s:else>

    单独使用<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。

    >

  • 相关阅读:
    06-图3 六度空间 (30 分)
    06-图2 Saving James Bond
    06-图1 列出连通集 (25 分)
    05-树9 Huffman Codes (30 分)
    05-树8 File Transfer (25 分)
    05-树7 堆中的路径 (25 分)
    04-树6 Complete Binary Search Tree (30 分)
    04-树5 Root of AVL Tree (25 分)
    03-树3 Tree Traversals Again (25 分)
    只允许输入数字的TextBox控件
  • 原文地址:https://www.cnblogs.com/langtianya/p/3010250.html
Copyright © 2011-2022 走看看