[英]Velocity used with JSP throws javax.servlet.ServletException: java.lang.NoClassDefFoundError:
I've been trying to create an admin panel for my software. 我一直在尝试为我的软件创建一个管理面板。 I decided to use Velocity Template Language (VTL) with JSP to create dynamic pages. 我决定将速度模板语言(VTL)与JSP结合使用来创建动态页面。 But when I import the Bean I created in my program which has the VTL code, it throws the javax.servlet.ServletException: java.lang.NoClassDefFoundError:
and prints the following stack trace: 但是,当我导入在程序中创建的具有VTL代码的Bean时,它将抛出javax.servlet.ServletException: java.lang.NoClassDefFoundError:
并打印以下堆栈跟踪:
javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/velocity/context/Context
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:343)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
with root cause 有根本原因
java.lang.NoClassDefFoundError: org/apache/velocity/context/Context
java.lang.Class.getDeclaredConstructors0(Native Method)
java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
java.lang.Class.getConstructor0(Unknown Source)
java.lang.Class.getConstructor(Unknown Source)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1223)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1182)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
org.apache.jasper.compiler.Node$Root.accept(Node.java:475)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
org.apache.jasper.compiler.Generator.generate(Generator.java:3490)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
My JavaBean is: 我的JavaBean是:
package com.sarvika.cfnm.web;
import java.sql.*;
import java.io.*;
import org.apache.velocity.*;
import org.apache.velocity.app.VelocityEngine;
public class IndexGenerator {
private final String JDBC_URL = "jdbc:mysql://127.0.0.1/coffeenet" ;
private final String SQL_USER = "root" ;
private final String SQL_PASS = "root" ;
private final String VELOCITY_TEMPLATE_TABLE = "users_table.vm";
private final String VELOCITY_CONTEXT_USERS_ARRAY = "usersList";
private final String SELECT_QUERY = "SELECT AuthID FROM users ;" ;
private final String COLUMN_ALIAS_USERS = "AuthID";
private final File ADMIN_JSP = new File("WebContent/admin.jsp");
public void generate() {
VelocityEngine engine = new VelocityEngine();
Template template = engine.getTemplate(VELOCITY_TEMPLATE_TABLE);
VelocityContext context = new VelocityContext();
Object [] users = getAllUsers();
context.put(VELOCITY_CONTEXT_USERS_ARRAY, users);
StringWriter jspData = new StringWriter();
template.merge(context, jspData);
try {
BufferedWriter jspFileWriter = new BufferedWriter(new FileWriter(ADMIN_JSP));
jspFileWriter.write(jspData.toString());
jspFileWriter.close();
} catch (IOException ex){
}
}
private Object [] getAllUsers() {
Object [] users = null ;
try {
int counter = 0 ;
Connection sqlConnection = DriverManager.getConnection(JDBC_URL, SQL_USER, SQL_PASS);
Statement sqlStatement = sqlConnection.createStatement();
ResultSet resultSet = sqlStatement.executeQuery(SELECT_QUERY);
while ( resultSet.next() )
counter++;
users = new Object [counter] ;
resultSet = sqlStatement.executeQuery(SELECT_QUERY);
for ( int i = 0 ; i < users.length ; i++ ) {
resultSet.next();
users[i] = resultSet.getString(COLUMN_ALIAS_USERS);
}
} catch (SQLException ex) {
}
return users;
}
}
And the JSP is: JSP是:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="validator" class="com.sarvika.cfnm.main.Validator"></jsp:useBean>
<jsp:useBean id="gen" class="com.sarvika.cfnm.web.IndexGenerator"></jsp:useBean>
<center><img src="cfnm.png" align="top"></center>
<%
String userName = request.getParameter("userID");
String password = request.getParameter("password");
validator.setUserName(userName);
validator.setPassword(password);
%>
<%=
validator.check()
%>
<%
if (validator.getValidation() == true) {
gen.generate();
}
%>
</body>
</html>
Where I am making the mistake. 我在哪里犯错。 I am using Eclipse Indigo IDE. 我正在使用Eclipse Indigo IDE。 I searched for the topic and found out that I should include required JAR(s) in WEB-INF directory. 我搜索了该主题,发现我应该在WEB-INF目录中包含必需的JAR。 I have already did so but the problem still continues to exist. 我已经这样做了,但是问题仍然存在。
应该在/ WEB-INF / lib目录中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.