使用标签控制页面逻辑案例

package com.loaderman.demo.b_cases;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class ChooseTag extends SimpleTagSupport {
    //不是属性,而是临时变量
    private boolean flag;

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void doTag() throws JspException, IOException {
        //输出标签体内容
        this.getJspBody().invoke(null);
    }
}
package com.loaderman.demo.b_cases;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class ForEachTag extends SimpleTagSupport {
    private Object items;//需要遍历的数据.List和map
    private String var;//每个元素的名称
    
    public void setItems(Object items) {
        this.items = items;
    }

    public void setVar(String var) {
        this.var = var;
    }

    @Override
    public void doTag() throws JspException, IOException {
        //遍历items数据
        //List
        /*PageContext pageContext = (PageContext)this.getJspContext();
        if(items instanceof List){
            List list = (List)items;
            for (Object object : list) {
                //把每个对象放入域对象中(pageContext)
                pageContext.setAttribute(var, object);
                //显示标签体内容
                this.getJspBody().invoke(null);
            }
        }
        
        //Map
        if(items instanceof Map){
            Map map = (Map)items;
            Set<Entry> entrySet = map.entrySet();
            for(Entry entry :entrySet){
                //把每个对象放入域对象中(pageContext)
                pageContext.setAttribute(var, entry);
                //显示标签体内容
                this.getJspBody().invoke(null);
            }
        }*/
        
        
        //简化代码
        //思路: 
            //1)list         -> Collection
            //2) map.entrySet -> Collection
        PageContext pageContext = (PageContext)this.getJspContext();
        Collection colls = null;
        if(items instanceof List){
            colls = (List)items;
        }
        if(items instanceof Map){
            Map map = (Map)items;
            colls = map.entrySet();
        }
             
        for(Object object:colls){
            //把每个对象放入域对象中(pageContext)
            pageContext.setAttribute(var, object);
            //显示标签体内容
            this.getJspBody().invoke(null);
        }
    }
}
package com.loaderman.demo.b_cases;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class IfTag extends SimpleTagSupport {
    private boolean test;
    
    public void setTest(boolean test) {
        this.test = test;
    }

    @Override
    public void doTag() throws JspException, IOException {
        //根据test的返回值决定是否输出标签体内容
        if(test){
            this.getJspBody().invoke(null);
        }
    }
}
package com.loaderman.demo.b_cases;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 自定义登陆页面标签
 * @author APPle
 *
 */
public class LoginTag extends SimpleTagSupport{
    private String username;
    private String password;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public void doTag() throws JspException, IOException {
         HttpServletResponse response = (HttpServletResponse)((PageContext)this.getJspContext()).getResponse();
         //设置输出内容类型和编码
         response.setContentType("text/html;charset=utf-8");
         String html = "";
        
         html += "<center><h3>用户登陆页面</h3></center>";
         html += "<table border='1' align='center' width='400px'>";
         html += "    <tr>";
         html += "        <th>用户名:</th>";
         html += "        <td><input type='text' name='"+username+"'/></td>";
         html += "    </tr>";
         html += "    <tr>";
         html += "        <th>密码:</th>";
         html += "        <td><input type='password' name='"+password+"'/></td>";
         html += "    </tr>";
         html += "    <tr>";
         html += "        <td colspan='2' align='center'><input type='submit' value='登陆'/>&nbsp;<input type='reset' value='重置'/></td>";
         html += "    </tr>";
         html += "</table>";
        
        JspWriter out = this.getJspContext().getOut();
        out.write(html);
    }
}
package com.loaderman.demo.b_cases;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class OtherwiseTag extends SimpleTagSupport {
    @Override
    public void doTag() throws JspException, IOException {
        //通过父标签传递,when标签中test的值
        //获取父标签
        ChooseTag parent = (ChooseTag)this.getParent();
        boolean test = parent.isFlag();
        
        if(!test){
            this.getJspBody().invoke(null);
        }
    }
}
package com.loaderman.demo.b_cases;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class WhenTag extends SimpleTagSupport {
    private boolean test;
    
    public void setTest(boolean test) {
        this.test = test;
    }

    @Override
    public void doTag() throws JspException, IOException {
        //根据test的返回值决定是否输出标签体内容
        if(test){
            this.getJspBody().invoke(null);
        }  
        //获取父标签
        ChooseTag parent = (ChooseTag)this.getParent();
        parent.setFlag(test);
            
    }
}

使用:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://loaderman.cn" prefix="loaderman" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>登陆页面</title>  
  </head>
  
  <body>
  <form action="" method="post">
   <loaderman:login password="pwd" username="user"></loaderman:login> 
   </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://loaderman.cn" prefix="loaderman" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>if标签</title>  
  </head>
  
  <body>
    <loaderman:if test="${10>5}">
        条件成立
    </loaderman:if>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://loaderman.cn" prefix="loaderman" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>choose标签</title>  
  </head>
  
  <body>
    <loaderman:choose>
        <loaderman:when test="${10<5}">
            条件成立
        </loaderman:when>
        <loaderman:otherwise>
            条件不成立
        </loaderman:otherwise>
    </loaderman:choose>
  </body>
</html>
<%@ page language="java" import="java.util.*,com.loaderman.demo.*" pageEncoding="utf-8"%>
<%@taglib uri="http://loaderman.cn" prefix="loaderman" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>forEach标签</title>  
  </head>
  
  <body>
    <%
       //保存数据
       //List
         List<Student>  list = new ArrayList<Student>();
         list.add(new Student("rose",18));
         list.add(new Student("jack",28));
         list.add(new Student("lucy",38));
         //放入域中
         pageContext.setAttribute("list",list);
         
         //Map
         Map<String,Student> map = new HashMap<String,Student>();
         map.put("100",new Student("mark",20));
         map.put("101",new Student("maxwell",30));
         map.put("102",new Student("narci",40));
         //放入域中
         pageContext.setAttribute("map",map);
     %>
     
     <loaderman:forEach items="${list}" var="student">
             姓名:${student.name } - 年龄:${student.age }<br/>
     </loaderman:forEach>
     
     <hr/>
     
     <loaderman:forEach items="${map}" var="entry">
           编号:${entry.key} - 姓名:${entry.value.name} - 年龄:${entry.value.age }<br/>
     </loaderman:forEach>
  </body>
</html>
package com.loaderman.demo;

public class Student {
    private String name1;
    private int age;
    public String getName() {
        return name1;
    }
    public void setName(String name) {
        this.name1 = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Student(String name, int age) {
        super();
        this.name1 = name;
        this.age = age;
    }
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    
}

loaderman.tld文件核心代码:

<tag>
    <name>if</name>
    <tag-class>com.loaderman.demo.b_cases.IfTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
        <name>test</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
  
  <tag>
    <name>choose</name>
    <tag-class>com.loaderman.demo.b_cases.ChooseTag</tag-class>
    <body-content>scriptless</body-content>
  </tag>
  
  <tag>
    <name>when</name>
    <tag-class>com.loaderman.demo.b_cases.WhenTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
        <name>test</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
  
  <tag>
    <name>otherwise</name>
    <tag-class>com.loaderman.demo.b_cases.OtherwiseTag</tag-class>
    <body-content>scriptless</body-content>
  </tag>
  
  <tag>
    <name>forEach</name>
    <tag-class>com.loaderman.demo.b_cases.ForEachTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
        <name>items</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <name>var</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag><tag>
    <name>login</name>
    <tag-class>com.loaderman.demo.b_cases.LoginTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
        <name>username</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>password</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
  

 

posted on 2018-11-22 17:40  LoaderMan  阅读(306)  评论(0编辑  收藏  举报

导航