jsf自制标签报错?
访问MyJsf页面出错,错误信息如下:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:147)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
root cause
java.lang.ClassCastException
cn.pj.tags.pjlabelTag.setProperties(pjlabelTag.java:37)
javax.faces.webapp.UIComponentTag.findComponent(UIComponentTag.java:705)
javax.faces.webapp.UIComponentTag.doStartTag(UIComponentTag.java:423)
org.apache.jsp.MyJsp_jsp._jspx_meth_pj_mylabeltag_0(MyJsp_jsp.java:85)
org.apache.jsp.MyJsp_jsp._jspService(MyJsp_jsp.java:60)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:147)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
note The full stack trace of the root cause is available in the Tomcat logs.
在下初学者,这个问题查资料,调试,弄了一天,一直搞不定,只好向各位高手请教了!谢谢!
1 开发工具:eclipse+myeclipes+tomcate
2 项目结构如下
myface
--src
--cn
--pj
--tags
-pjlabelComponent.java
-pjlabelTag.java
--WebRoot
--WEB-INF
--classes
--...(省略)
--lib
-commons-beanutils.jar
-commons-collections.jar
-commons-digester.jar
-commons-logging.jar
-jsf-api.jar
-jsf-impl.jar
-jstl.jar
-standard.jar
-faces-config.xml
-mytag.tld
-web.xml
-MyJsp.jsp
3 pjlabelComponent.java如下:
package cn.pj.tags;
import java.io.IOException;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
public class pjlabelComponent extends UIOutput {
private String pjlabel;
public String getPjlabel() {
return pjlabel;
}
public void setPjlabel(String pjlabel) {
this.pjlabel = pjlabel;
}
//保存标签的状态
public Object saveState(FacesContext context) {
Object values[] = new Object[2];
values[0] = super.saveState(context);
values[1] = pjlabel;
return ((Object) (values));
}
public void restoreState(FacesContext context, Object state) {
Object values[] = (Object[])state;
super.restoreState(context, values[0]);
pjlabel = (String)values[1];
}
//呈现标签及内容(标签开始)
public void encodeBegin(FacesContext context) throws IOException {
ResponseWriter writer = context.getResponseWriter();
//标签的开始
writer.startElement( "mylabel ", this);
//标签的Mylabel属性
writer.write(pjlabel);
}
//呈现标签及内容(标签结束)
public void encodeEnd(FacesContext context) throws IOException {
ResponseWriter writer = context.getResponseWriter();
//标签的结束
writer.endElement( "mylabel ");
writer.flush();
}
public void decode(FacesContext context) {
return;
}
public String getFamily(){
return "mylabeltag ";
}
}
4 pjlabelTag.java如下:
package cn.pj.tags;
import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentTag;
public class pjlabelTag extends UIComponentTag{
private String pjlabel;
public String getPjlabel() {
return pjlabel;
}
public void setPjlabel(String pjlabel) {
this.pjlabel = pjlabel;
}
//销毁标签时
public void reease() {
//首先继承父类的方法
super.release();
pjlabel=null;
}
//标签属性值绑定到组件
protected void setProperties(UIComponent component) {
//首先继承父类的方法
super.setProperties(component);
((pjlabelComponent)component).setPjlabel(pjlabel);
}
public String getComponentType() {
return "mylabeltag ";
}
public String getRendererType() {
return null;
}
}
5 faces-config.xml如下:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN " "http://java.sun.com/dtd/web-facesconfig_1_1.dtd ">
<faces-config>
<component>
<component-type> mylabeltag </component-type>
<component-class> cn.pj.tags.pjlabelComponent </component-class>
</component>
</faces-config>
6 mytag.tld如下:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN "
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd ">
<taglib>
<tlib-version> 0.03 </tlib-version>
<jsp-version> 1.2 </jsp-version>
<short-name> pj </short-name>
<uri> http://tags.pj.cn </uri>
<tag>
<name> mylabeltag </name>
<tag-class> cn.pj.tags.pjlabelTag </tag-class>
<attribute>
<name> pjlabel </name>
<description> The value of the label </description>
</attribute>
</tag>
</taglib>
ps:这个例子我第一次作的时候没有问题,过了一天,做了些关于自制标签的其他例子后,
再重运行这个例子就报这个错了,不知道是不是我作其他例子造成的?
[解决办法]
不好意思 忘记加上MyJsp.jsp的内容了!
7 MyJsp.jsp如下
<%@ taglib prefix= "pj " uri= "http://tags.pj.cn " %>
<pj:mylabeltag pjlabel= "my first DIY JSF tag "/>
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html