📄 iteratetag.java
字号:
/*
Copyright 2004 Jenkov Development
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.jenkov.prizetags.logic.impl;
import com.jenkov.prizetags.util.RequestUtil;
import com.jenkov.prizetags.base.NamePropertyTag;
import javax.servlet.jsp.JspException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
/**
* This tag is capable of iterating collections, iterators or the key sets of maps.
* The iterate tag works much like the Struts iterate tag.
*
*
* @author Jakob Jenkov - Copyright 2005 Jenkov Development
*/
public class IterateTag extends NamePropertyTag{
protected Iterator iterator = null;
public int doStartTag() throws JspException {
Collection collection = null;
Object object = RequestUtil.findObject(pageContext, name, scope);
if(object == null){
throw new JspException("No bean located at key " + name);
}
if(property != null){
object = getBeanProperty(object, property);
}
if(object == null){
throw new JspException("The property " + property + " of the bean at key " + name + " was null.");
}
if( ! (object instanceof Collection || object instanceof Iterator || object instanceof Map) ){
throw new JspException("The object found on name/property " + name + "/" + property +
" is not a collection, iterator or map. " +
"It was (" + object.getClass() + " : " + object.toString() + ")");
}
if( object instanceof Iterator){
this.iterator = (Iterator) object;
} else if( object instanceof Collection ){
this.iterator = ((Collection) object).iterator();
} else if( object instanceof Map){
this.iterator = ((Map) object).keySet().iterator();
}
if(this.iterator != null && iterator.hasNext()){
RequestUtil.storeObject(pageContext, id, scope, iterator.next());
return EVAL_BODY_INCLUDE;
}
return SKIP_BODY;
}
public int doAfterBody() throws JspException {
if(iterator.hasNext()){
RequestUtil.storeObject(pageContext, id, scope, iterator.next());
return EVAL_BODY_AGAIN;
} else {
return SKIP_BODY;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -