目录
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、z:select标签
满足以下要求
需求:
1.省略遍历过程
2.当做数据回显时,无需增加if判断,无需增加新的代码
我们先根据原来c标签的select进行分析,我们所需要的元素
<select id="" name="">
<option value="1">zs</option>
<option value="2">ls</option>
</select>
1、 分析:
1)后台要遍历->数据源->items
2)需要一个对象的属性代表下拉框对应的展示内容->textVal
3)需要一个对象的属性代表下拉框对应的value值->textKey
4)默认的头部选项展示内容->headerTextVal
5)默认的头部选项值->headerTextKey
6)数据库存储的值,为了方便做数据回显->selectedVal
以上六点就是我们所需要的,以下两点可以省略不写
7)id
8) name
我们根据以上分析的需要进行创建助手类
package com.mgy.tag;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.PropertyUtils;
/**
* 分析:
* 1.后台要遍历->数据源->items
* 2.需要一个对象的属性代表下拉框对应的展示内容->textVal
* 3.需要一个对象的属性代表下拉框对应的value值->textKey
* 4.默认的头部选项展示内容->headerTextVal
* 5.默认的头部选项值->headerTextKey;
* 6.数据库存储的值,为了方便做数据回显->selectedVal
* 7.id
* 8.name
* 9.cssStyle...
* @author Administrator
*
*/
public class SelectTag extends BodyTagSupport{
private List<Object> items;
private String textVal;//teacher的name
private String textKey;
private String headerTextVal;
private String headerTextKey;
private String selectedVal;
private String id;
private String name;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();//获取out
try {
out.print(toHTML());
} catch (Exception e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
StringBuffer sb=new StringBuffer();//获取StringBuffer
sb.append("<select id='"+id+"' name='"+name+"'>");
if(headerTextVal!=null&&!"".equals(headerTextVal)) {//判断headerTextVal是否为空和空字符串是否等于headerTextVal
sb.append("<option value='"+headerTextKey+"'>"+headerTextVal+"</option>");
}
for (Object obj : items) {//遍历
// <z:select textVal="name" items="${list }" textKey="tid"></z:select>
Field textKeyFiled = obj.getClass().getDeclaredField(textKey);
textKeyFiled.setAccessible(true);
Object value = textKeyFiled.get(obj);//真正下拉框展示的值
if(selectedVal!=null&&!"".equals(selectedVal)&&selectedVal.equals(value)) {
sb.append("<option selected value='"+value+"'>"+PropertyUtils.getProperty(obj, textVal)+"</option>");
}
else{
sb.append("<option value='"+value+"'>"+PropertyUtils.getProperty(obj, textVal)+"</option>");
}
}
sb.append("</select>");
return sb.toString();
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getTextVal() {
return textVal;
}
public void setTextVal(String textVal) {
this.textVal = textVal;
}
public String getTextKey() {
return textKey;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public String getHeaderTextVal() {
return headerTextVal;
}
public void setHeaderTextVal(String headerTextVal) {
this.headerTextVal = headerTextVal;
}
public String getHeaderTextKey() {
return headerTextKey;
}
public void setHeaderTextKey(String headerTextKey) {
this.headerTextKey = headerTextKey;
}
public String getSelectedVal() {
return selectedVal;
}
public void setSelectedVal(String selectedVal) {
this.selectedVal = selectedVal;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
注意:PropertyUtils.getProperty(obj, textVal)相当于反射那三行
2、 配置:
<tag>
<name>select</name>
<tag-class>com.mgy.tag.SelectTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textVal</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headerTextVal</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headerTextKey</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>selectedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
调用:
<%@page import="java.util.ArrayList"%>
<%@page import="com.mgy.entity.Teacher"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
#mySelect{
color:red;
}
</style>
</head>
<body>
<%
List<Teacher> list=new ArrayList<>();
list.add(new Teacher("t001","zs"));
list.add(new Teacher("t002","李四"));
list.add(new Teacher("t003","老六"));
request.setAttribute("list", list);
%>
<z:select selectedVal="t002" id="mySelect" textVal="name" headerTextKey="-1" headerTextVal="====请选择====" items="${list }" textKey="tid"></z:select>
</body>
</html>
效果:
二、z:foreach标签
1、分析
根据c标签的foreach标签进行分析
<c:forEach items="${clas12}" var="c">
分析两个属性:
items:List<Object>
var:String
分析线路:
第二条:eval_body_include
第三条:eval_body-again
package com.mgy.tag;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* <c:forEach items="${clas12}" var="c">
* 分析两个属性:
* items:List<Object>
* var:String
* 分析线路:
* 第二条:eval_body_include
* 第三条:eval_body-again
* @author Administrator
*
*/
public class ForeachTag extends BodyTagSupport{
private String var;
private List<Object>items;
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
@Override
public int doStartTag() throws JspException {
Iterator<Object> it = items.iterator();
// <option value="where cid='${c.cid}'">${c.cname}</option>
// var =c;it.next()是集合中的某一个对象
// pageContext.setAttribute("c", items.get(0));
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it", it);//为了保留迭代时现有位置
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
Iterator<Object> it = (Iterator<Object>) pageContext.getAttribute("it");
if(it.hasNext()) {
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it", it);//为了保留迭代时现有位置
return EVAL_BODY_AGAIN;
}
else {
return EVAL_PAGE;
}
}
}
2、配置
<tag>
<name>for</name>
<tag-class>com.mgy.tag.ForeachTag</tag-class>
<body-content>JSP</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>
3、调用
<%@page import="java.util.ArrayList"%>
<%@page import="com.mgy.entity.Teacher"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
#mySelect{
color:red;
}
</style>
</head>
<body>
<%
List<Teacher> list=new ArrayList<>();
list.add(new Teacher("t001","zs"));
list.add(new Teacher("t002","李四"));
list.add(new Teacher("t003","老六"));
request.setAttribute("list", list);
%>
<z:for items="${list }" var="t">
${t.tid }:${t.name }
</z:for>
</body>
</html>
效果:
三、案例
1、分析
<input type="checkbox" name="hid" value="1">篮球
分析以上标签知道以下所需元素
1.数据源->items->${stu}
2.有一个对象的属性进行当作复选框展示的内容-->textVal
3.有一个对象的属性进行当作复选框展示的内容-->textKey
4.数据回显-->checkbokVal
package com.mgy.tag;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.PropertyUtils;
/**
* <input type="checkbox" name="hid" value="1">篮球
* 1.数据源->items->${stu}
* 2.有一个对象的属性进行当作复选框展示的内容-->textVal
* 3.有一个对象的属性进行当作复选框展示的内容-->textKey
* 4.数据回显-->checkbokVal
* @author Administrator
*
*/
public class CheckedTag extends BodyTagSupport{
private List<Object>items;
private String textVal;
private String textKey;
private List<Object>checkbokVal;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();//获取out对象
try {
out.print(toHTML());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws Exception, InvocationTargetException, NoSuchMethodException {
StringBuffer sb=new StringBuffer();
String html;
String value;
for (Object obj : checkbokVal) {
//拿到obj中对应的值
value=(String)PropertyUtils.getIndexedProperty(obj, textVal);
html=(String)PropertyUtils.getIndexedProperty(obj, textKey);
if(checkbokVal.contains(value)) {//判断回显集合里是否包含这个value
sb.append("<input checkbok type='checkbox'value='"+value+"'>"+html+"");
}
else {
sb.append("<input type='checkbox'value='"+value+"'>"+html+"");
}
}
return sb.toString();
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getTextVal() {
return textVal;
}
public void setTextVal(String textVal) {
this.textVal = textVal;
}
public String getTextKey() {
return textKey;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public List<Object> getCheckbokVal() {
return checkbokVal;
}
public void setCheckbokVal(List<Object> checkbokVal) {
this.checkbokVal = checkbokVal;
}
}
2、配置
<tag>
<name>checkbox</name>
<tag-class>com.mgy.tag.CheckedTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textVal</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>checkboxVal</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
3、调用
<%@page import="java.util.ArrayList"%>
<%@page import="com.mgy.entity.Hobby"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List li=new ArrayList();
li.add(new Hobby("1","篮球"));
li.add(new Hobby("2","跳舞"));
li.add(new Hobby("3","唱歌"));
li.add(new Hobby("4","足球"));
List la=new ArrayList();//回显集合
la.add("1");
la.add("3");
request.setAttribute("li", li);
request.setAttribute("la", la);
%>
<z:checkbox checkboxVal="${la }" textVal="hname" items="${li }" textKey="hid"></z:checkbox>
</body>
</html>
效果:
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。