我有一个简单的代码,可以使用servlet和JSP。通常它在我使用以下代码时有效。 然而,这是一些可能性,我得到了这个意外的错误,无法弄清楚问题是什么
Caused by:
javax.el.ELException - Cannot convert testID of type class java.lang.String to
class java.lang.Integer
我的结果课
public class Result {
private String testID ;
private String date ;
private String result ;
private String grade ;
public String getTestID() {
return testID;
}
public String getDate() {
return date;
}
public String getResult() {
return result;
}
public String getGrade() {
return grade;
}
public void setTestID(String testID) {
this.testID = testID;
}
public void setDate(String date) {
this.date = date;
}
public void setResult(String result) {
this.result = result;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
我的Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String testID = request.getParameter("Action").trim();
User user = (User) request.getSession().getAttribute("currentSessionUser");
String sql = "select * from result where testID =" + testID + " and userID = " + user.getUserID();
PreparedStatement ps = DBconnection.getPreparedStatement(sql);
List <Result> results = new ArrayList<Result>();
try {
ResultSet rs = ps.executeQuery();
while(rs.next())
{
Result result = new Result();
result.setTestID(rs.getString("testID"));
result.setGrade(rs.getString("grade"));
result.setDate(rs.getString("dates"));
result.setResult(rs.getString("result"));
results.add(result);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
DBconnection.close();
}
request.getSession().setAttribute("results", results);
response.sendRedirect(request.getContextPath() +"/faces/subResult.jsp");
我的JSP
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
</head>
<body>
<ul>
<c:forEach items="${results}" var="results">
<li> ${results.testID} </li>
<li><c:out value="${result.grade}" /></li>
<li><c:out value="${result.result}" /></li>
<li>Hello</li>
</c:forEach>
</ul>
</body>
堆栈跟踪
javax.faces.FacesException: javax.el.ELException: Cannot convert testID of type class java.lang.String to class java.lang.Integer
at org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.wrap(ExceptionHandlerImpl.java:241)
at org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:156)
at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:258)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1229)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:774)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:456)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1032)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3748)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:962)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:459)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:526)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:312)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:283)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1814)
Caused by: javax.el.ELException: Cannot convert testID of type class java.lang.String to class java.lang.Integer
at org.apache.el.lang.ELSupport.coerceToNumber(ELSupport.java:338)
at org.apache.el.lang.ELSupport.coerceToNumber(ELSupport.java:309)
at org.apache.el.lang.ELSupport.coerceToType(ELSupport.java:422)
at org.apache.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:47)
at org.apache.myfaces.el.convert.PropertyResolverToELResolver.coerceToInt(PropertyResolverToELResolver.java:271)
at org.apache.myfaces.el.convert.PropertyResolverToELResolver.getValue(PropertyResolverToELResolver.java:157)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:55)
at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:142)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:55)
at org.apache.el.parser.AstValue.getValue(AstValue.java:173)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:283)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:778)
at com.ibm._jsp._subResult._jspx_meth_c_forEach_0(_subResult.java:182)
at com.ibm._jsp._subResult._jspService(_subResult.java:98)
at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1229)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:774)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:456)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
at com.ibm.wsspi.webcontainer.servlet.GenericServletWrapper.handleRequest(GenericServletWrapper.java:122)
at com.ibm.ws.jsp.webcontainerext.AbstractJSPExtensionServletWrapper.handleRequest(AbstractJSPExtensionServletWrapper.java:216)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1032)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:1384)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:193)
at com.FYP.servletGetSubResult.doGet(servletGetSubResult.java:74)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1229)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:774)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:456)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1032)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:1384)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:193)
at org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(ServletExternalContextImpl.java:367)
at org.apache.myfaces.view.jsp.JspViewDeclarationLanguage.buildView(JspViewDeclarationLanguage.java:94)
at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:66)
at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
... 25 more
答案 0 :(得分:1)
我想这一行给出了问题
<li> ${results.testID + ""} </li>
我想知道它的目的是什么。为什么要尝试将Integer与空字符串连接起来?
使用el
运算符无法实现+
中的连接。你只需要将el表达式放在彼此靠近的位置。
以下内容应该有效
<li> ${results.testID} ${""}</li>
这会做同样的事情,但更有意义
<li> ${results.testID}</li>
编辑:
正如JB Nizet在对该问题的评论中指出的那样,该问题是在命名引用当前项目的变量时由错误的选择引起的。
引用他:
不确定这是否与异常有关,但是命名 当前项目结果(在var =&#34;结果&#34;)是一个坏主意:它是 与集合本身相同的名称。将其命名为结果,然后更改 $ {results.testID}到$ {result.testID}。
答案 1 :(得分:-1)
很可能您将Result.testID定义为Integer(在Result类中)。
但是在您的数据库中,testID列是String。大多数数据都是数字的,因此result.setTestID(rs.getString("testID"));
通常不会失败,但是在某些情况下,testID列中的数据库数据不是数字。因此“无法将java.lang.String类型的testID转换为类java.lang.Integer”错误