gpt4 book ai didi

java - 无法在JSP中编译JSONObject

转载 作者:行者123 更新时间:2023-12-01 12:09:02 25 4
gpt4 key购买 nike

请无论如何使用以下代码帮助我(使用 Intellij IDEA 14.0 演示,但我手动启动 Tomcat,使用 TOMCAT8.exe):

<%@ page language="java" %>
<%@ page import="org.apache.commons.lang3.*" %>
<%@ page import="org.apache.commons.beanutils.*" %>
<%@ page import="org.apache.commons.collections4.*" %>
<%@ page import="org.apache.commons.logging.*" %>
<%@ page import="net.sf.json.JSONObject" %>
<%@ page import="net.sf.json.JSONArray" %>


<%
JSONObject my_data = new JSONObject();
JSONObject row = new JSONObject();
JSONObject dataUsers = new JSONObject();

JSONArray rows = new JSONArray();
JSONArray cells = new JSONArray();

cells.add("x1");
cells.add("x2");
cells.add("x3");
cells.add("x4");

row.put("id","id_val1");
row.put("cells",cells);

dataUsers.put("nume","total");
dataUsers.put("salariu","1000");

rows.add(row);

my_data.put("rows",rows);
my_data.put("dataUser",dataUsers);

//out.print(my_data);
out.print(my_data.toString(2));
%>

尽管我包含了指定的依赖库 http://json-lib.sourceforge.net/在CATALINA_HOME/lib 中PROJECT_ROOT/lib 我仍然得到这个:

Tomcat 的错误日志

09-Dec-2014 11:17:32.493 SEVERE [http-apr-9999-exec-3]     
org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet
[jsp] in context with path [/SAN] threw exception [An exception occurred processing
JSP page /db.jsp at line 53

53: JSONObject my_data = new JSONObject();
54: JSONObject row = new JSONObject();
55: JSONObject dataUsers = new JSONObject();
56:


Stacktrace:] with root cause
java.lang.ClassNotFoundException:
org.apache.commons.lang.exception.NestableRuntimeException
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.apache.jsp.db_jsp._jspService(db_jsp.java:132)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2407)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2396)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

提前谢谢您!

最佳答案

首先,避免在jsp页面中使用scriptlet,而是将此代码放在servlet中。

然后我认为 javax.json API具体来说javax.json.JsonObjectjavax.json.JsonArray是管理 Json 对象的更简单的解决方案,您的代码将是:

   JsonArrayBuilder cellsBuilder=Json.createArrayBuilder();
cellsBuilder.add("x1");
cellsBuilder.add("X2");
cellsBuilder.add("X3");
cellsBuilder.add("X3");

JsonArray cells=cellsBuilder.build();

JsonObject rows =Json.createObjectBuilder()
.add("id", "id_val1")
.add("cells", cells).build();

JsonObject dataUsers =Json.createObjectBuilder()
.add("nume","total")
.add("salariu","1000").build();

JsonObject my_data =Json.createObjectBuilder()
.add("rows", "rows")
.add("dataUser", dataUsers).build();

关于java - 无法在JSP中编译JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27375608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com