我们正在将我的应用程序从JSF1.0迁移到JSF2.0。
在JSF1.0中,我们有自定义迭代器类,它将用于在JSP页面上显示List值。
如果我们在JSF2.0中使用相同的自定义转换器,则List的最后一条记录会在所有行中重复显示。
以下是输出的屏幕截图。该列表包含4个对象,最后一个对象的值为“test4”和“abc4”
我们在JSF2运行时使用JSP作为视图。
FirstName LastName test4 abc4 test4 abc4 test4 abc4 test4 abc4
这是自定义迭代器自定义标记。
package com.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.webapp.UIComponentBodyTag;
import javax.faces.webapp.UIComponentELTag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
/**
* Custom JSF Iterator Tag
*/
public class CustomIterator
extends
UIComponentELTag
{
/**
* <p>
* Thread Safety: @ThreadSafe
* </p>
*/
/*private final static Logger LOGSERVICE = Logger
.getInstance(CustomIterator.class.getName());*/
/**
* The Iterator.
*/
private Iterator iterator;
/**
* The data.
*/
private Object values;
/**
* The data tag attribute.
*/
private String data;
/**
* The temporary storage name.
*/
private String var;
private ValueExpression ve;
public ValueExpression getVe() {
return ve;
}
public void setVe(ValueExpression ve) {
this.ve = ve;
}
/**
* Default Constructor.
*/
public CustomIterator()
{
}
public String getComponentType()
{
return null;
}
public String getRendererType()
{
return null;
}
public void setData(String data)
{
this.data = data;
}
public String getData()
{
return data;
}
public void setVar(String var)
{
this.var = var;
}
public String getVar()
{
return var;
}
/**
* Start tag encoder.
*
* @return indication to render.
*/
public int doStartTag()
{
final String methodName = "doStartTag";
FacesContext context = FacesContext.getCurrentInstance();
if (isValueReference(data))
{
//ValueBinding vb = context.getApplication().createValueBinding(data);
ValueExpression ve12 = context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(), data, ArrayList.class);
if (ve12 != null)
{
//values = vb.getValue(context);
values = ve12.getValue(getELContext());
}
else
{
return SKIP_BODY;
}
}
else
{
throw new NullPointerException();
}
if ((null != values) && (values instanceof List))
{
List list = (List) values;
if (null == iterator)
{
iterator = list.iterator();
}
//ValueBinding vb = null;
//ValueExpression ve = null;
Object obj = null;
if (iterator.hasNext())
{
obj = iterator.next();
ve = context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(), UtilConstants.EXPRESSION_OPEN_STR
+ var
+ UtilConstants.EXPRESSION_CLOSE_STR, Object.class);
ve.setValue(getELContext(), obj);
return EVAL_BODY_INCLUDE;
}
else
{
return SKIP_BODY;
}
}
return 0;
}
private boolean isValueReference(String value)
{
if (value == null)
{
throw new NullPointerException();
}
if ((value.indexOf("#{") != -1)
&& (value.indexOf("#{") < value.indexOf('}')))
{
return true;
}
return false;
}
/**
* End tag encoder.
*
* @return indication to render.
*/
public int doEndTag()
throws JspException
{
final String methodName = "doEndTag";
return 0;
}
/**
* After body encoder.
*
* @return indication to render.
*/
public int doAfterBody()
{
final String methodName = "doAfterBody";
FacesContext context = FacesContext.getCurrentInstance();
if (null != bodyContent)
{
try
{
JspWriter out = getPreviousOut();
out.print(bodyContent.getString());
bodyContent.clearBody();
}
catch (IOException e)
{
//LOGSERVICE.logError(e.getMessage());
}
}
if ((iterator != null) && (iterator.hasNext()))
{
if (values instanceof List)
{
Object obj = iterator.next();
ve.setValue(getELContext(), obj);
}
return EVAL_BODY_AGAIN;
}
else
{
return SKIP_BODY;
}
}
public void release()
{
final String methodName = "release";
iterator = null;
}
}
请建议如何解决此问题。