Struts <logic:match>标记用于检查给定属性包含给定值的substring 。 例如,属性结果“ Google搜索引擎”,值“ gle”将匹配,而值“ ABC”将不匹配。 如果条件匹配,则将执行标签的主体。 Struts <logic:notMatch>的操作与此相反。
Struts的match标记具有一个必须知道的属性,名称为“ location ”,其值为“ start ”或“ end ”
- location =“ start” –仅在给定值显示为给定属性的起始子字符串时匹配。 例如,“ Google搜索引擎” –“ Goog”将匹配,“ gine”将不匹配。
- location =“ end” –仅在给定值显示为给定属性的结尾子字符串时匹配。 例如,“ Google搜索引擎” –“ Goog”将不匹配,“ gine”将匹配。
- 没有位置定义 –如果给定值显示为给定属性的子字符串,则匹配。 例如,“ Google搜索引擎” –“ Goog”将匹配,“ gine”将匹配。
这是显示<logic:match>和<logic:notMatch>用法的示例。
LogicExampleAction.java
package com.mkyong.common.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LogicExampleAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception {
request.setAttribute("email", "mkyong123456@yahoo.com");
return mapping.findForward("success");
}
}
LogicExample.jsp
Struts - <logic:match> & <logic:notMatch>
Email - mkyong123456@yahoo.com
1. Is "yong" is a substring of the email? -
true
false
2. Is "yongABC" is a substring of the email? -
true
false
3. Is email start with "mkyong"? -
true
false
4.. Is email start with "yong"? -
true
false
5. Is email end with "com"? -
true
false
6. Is email end with "net"? -
true
false
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<action-mappings>
<action
path="/LogicTest"
type="com.mkyong.common.action.LogicExampleAction">
<forward name="success" path="/pages/LogicExample.jsp"/>
</action>
</action-mappings>
</struts-config>
结果
http:// localhost:8080 / StrutsExample / LogicTest.do
Struts - <logic:match> & <logic:notMatch>
Email - mkyong123456@yahoo.com
1. Is "yong" is a substring of the email? - true
2. Is "yongABC" is a substring of the email? - false
3. Is email start with "mkyong"? - true
4.. Is email start with "yong"? - false
5. Is email end with "com"? - true
6. Is email end with "net"? - false
标签: Struts
翻译自: https://mkyong.com/struts/struts-logicmatch-logicnotmatch-example/