当前位置: 移动技术网 > IT编程>开发语言>Java > Java的Struts框架中的if/else标签使用详解

Java的Struts框架中的if/else标签使用详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

这些标签执行可在每一种语言找到的一种基本条件流程。 'if'标签可用于本身或与“else if''标签和/或单/多'else'标签,如下图所示:

<s:if test="%{false}">
  <div>will not be executed</div>
</s:if>
<s:elseif test="%{true}">
  <div>will be executed</div>
</s:elseif>
<s:else>
  <div>will not be executed</div>
</s:else>

创建动作类:

package com.yiibai.struts2;

public class helloworldaction{
  private string name;

  public string execute() throws exception {
   return "success";
  }
  
  public string getname() {
   return name;
  }

  public void setname(string name) {
   this.name = name;
  }
}

创建视图
让我们有index.jsp文件如下:

<%@ page language="java" contenttype="text/html; charset=iso-8859-1"
 pageencoding="iso-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" 
"http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<title>hello world</title>
</head>
<body>
  <h1>hello world from struts2</h1>
  <form action="hello">
   <label for="name">please pick a name</label><br/>
   <select name="name">
     <option name="mike">mike</option>
     <option name="jason">jason</option>
     <option name="mark">mark</option>
   </select>
   <input type="submit" value="say hello"/>
  </form>
</body>
</html>

接下来 helloworld.jsp 演示使用, if, else 和 elseif 标签:

<%@ page contenttype="text/html; charset=utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>example of if and else</title>
</head>
<body>
<b>example of if and else</b><br/>
<s:if test="name=='mike'">
  you have selected 'mike'. 
</s:if>
<s:elseif test="name=='jason'">
  you have selected 'jason'
</s:elseif>
<s:else>
  you have not selected 'mike' or 'jason'.
</s:else>
</body>
</html>

在这里,如果标签返回true,如果“test”属性中指定的条件返回true。在我们的例子中,我们比较反对“mike”。如果这个名字是mike,标签返回true,我们打印的字符串,否则“elseif”块被执行,如果不满意,那么被执行else块。这是从传统的,如果没有什么不同,if, else if 可以在java语言中。

配置文件
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="helloworld" extends="struts-default">
   
   <action name="hello" 
      class="com.yiibai.struts2.helloworldaction" 
      method="execute">
      <result name="success">/helloworld.jsp</result>
   </action>

  </package>
</struts>

web.xml 应该像这样:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemalocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="webapp_id" version="3.0">
  
  <display-name>struts 2</display-name>
  <welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
   <filter-name>struts2</filter-name>
   <filter-class>
     org.apache.struts2.dispatcher.filterdispatcher
   </filter-class>
  </filter>

  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

右键点击项目名称,并单击 export > war file创建一个war文件。然后部署此war在tomcat的webapps目录下。最后,启动tomcat服务器和尝试访问url http://localhost:8080/helloworldstruts2/index.jsp。这会给出以下画面:

2015123183516308.jpg (560×282)

现在选择“mark”,并提交页面。您应该看到翻页

2015123183538774.jpg (560×277)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网