我试图从arraylist中获取值(从servlet类发送这个arraylist)并在jsp页面上显示它们。我正在使用jstl标签。我看了一些例子,我不知道为什么,但程序总是给出错误。这是我的错误消息:
org.apache.jasper.JasperException: An exception occurred processing JSP page /result.jsp at line 26
23: </br></br>Match Score: <c:out value="${score}"/>
24: <c:forEach items="${alternativedrugs}" var="alternativedrug">
25: </br></br>
26: </br></br>Drug <c:out value="${alternativedrug.name}"></c:out>
27: </br></br>Drug Description: <c:out value="${alternativedrug.description}"></c:out>
28: </br></br>Drug Targets: <c:out value="${alternativedrug.targets}"></c:out>
29: </br></br>Drug Interactions: <c:out value="${alternativedrug.interactions}"></c:out>
这是我的jstl代码:
This Drug Found
<c:forEach items="${alternativedrugs}" var="alternativedrug">
</br></br>Drug Name: <c:out value="${alternativedrug.getName()}"/>
</br></br>Drug Description: <c:out value="${alternativedrug.getDescription()}"/>
</br></br>Drug Targets: <c:out value="${alternativedrug.getTargets()}"/>
</br></br>Drug Interactions: <c:out value="${alternativedrug.getInteractions()}"/>
</br></br>Match Score: <c:out value="${score}"/>
对象类:
package drugtargetdisease;
import java.util.ArrayList;
public class MyDrug {
ArrayList<String> partner = new ArrayList();
private String targets = "";
private String name ="";
private String description ="";
private String interactions = "";
public MyDrug(){
}
public String getTargets() {
return targets;
}
public void setTargets(String targets) {
this.targets = targets;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getInteractions() {
return interactions;
}
public void setInteractions(String interactions) {
this.interactions = interactions;
}
}
Servlet Part(alternativedrugs是MyDrug Objects的ArrayList):
otherdrug.setName((String) myentity.getProperty("drug"));
otherdrug.setDescription(((Text) myentity.getProperty("description")).getValue());
otherdrug.setInteractions(((Text) myentity.getProperty("interactions")).getValue());
otherdrug.setTargets(((Text) myentity.getProperty("target")).getValue());
if(!otherdrug.getName().equals(maindrug.getName())){
if(MaxScore <= compare.getExpected(maindrug.getTargets(), otherdrug.getTargets(),maindrug.getInteractions(), otherdrug.getInteractions())){
MaxScore = compare.getExpected(maindrug.getTargets(), otherdrug.getTargets(),maindrug.getInteractions(), otherdrug.getInteractions());
alternativedrugs.add(otherdrug);
}
req.setAttribute("alternativedrugs", alternativedrugs);
req.setAttribute("score", MaxScore);
try {
rd.forward(req, resp);
} catch (ServletException e) {
e.printStackTrace();
}
完整追踪:
Stacktrace:
at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at com.google.appengine.tools.development.PrivilegedJspServlet.access$101(PrivilegedJspServlet.java:23)
at com.google.appengine.tools.development.PrivilegedJspServlet$2.run(PrivilegedJspServlet.java:59)
at java.security.AccessController.doPrivileged(Native Method)
at com.google.appengine.tools.development.PrivilegedJspServlet.service(PrivilegedJspServlet.java:57)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:94)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:327)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
at guestbook.CompareServlet.doPost(CompareServlet.java:85)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:110)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:61)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:94)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:380)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: javax.el.PropertyNotFoundException: Could not find property name in class guestbook.MyDrug
at javax.el.BeanELResolver.toBeanProperty(BeanELResolver.java:430)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:290)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:231)
at org.apache.el.parser.AstValue.getValue(AstValue.java:123)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:935)
at org.apache.jsp.result_jsp._jspx_meth_c_005fout_005f1(result_jsp.java:201)
at org.apache.jsp.result_jsp._jspx_meth_c_005fforEach_005f0(result_jsp.java:156)
at org.apache.jsp.result_jsp._jspService(result_jsp.java:99)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
... 55 more
答案 0 :(得分:1)
因此,错误消息为Could not find property name in class guestbook.MyDrug
。
这意味着没有方法
public String getName()
guestbook.MyDrug
中的。 (请注意,它可以返回任何内容,而不仅仅是String。但由于属性为name
,我认为它的类型为String
)。
JSP EL访问bean 属性。它不访问您班级的公共领域。使用公共字段是一种不好的做法,JSP并不鼓励这样做。使您的字段保密,并在必要时提供访问者(getter和setter)。如果要从JSP中读取字段,则需要一个getter。