gpt4 book ai didi

java - NoClassDefFound错误: org/springframework/jdbc/core/RowMapper

转载 作者:太空宇宙 更新时间:2023-11-04 11:59:46 24 4
gpt4 key购买 nike

我正在创建一个简单的 Web 应用程序,并决定使用 Spring JdbcTemplate 类。但现在我遇到了 NoClassDefFoundError: org/springframework/jdbc/core/RowMapper 。

根据堆栈跟踪,第 32 行的 ControllerServlet 出现问题。其中:

productDAOImpl = new ProductDAOImpl(dataSource);

错误的原因可能是什么?我应该做什么来修复它?

Context Path:/webstore_war_exploded
Servlet Path:/ControllerServlet
Path Info:null
Query String:null
Stack Trace
java.lang.NoClassDefFoundError: org/springframework/jdbc/core/RowMapper
com.ncproject.webstore.controller.ControllerServlet.init(ControllerServlet.java:32)
javax.servlet.GenericServlet.init(GenericServlet.java:244)
io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:117)
org.wildfly.extension.undertow.security.RunAsLifecycleInterceptor.init(RunAsLifecycleInterceptor.java:78)
io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:103)
io.undertow.servlet.core.ManagedServlet$DefaultInstanceStrategy.start(ManagedServlet.java:250)
io.undertow.servlet.core.ManagedServlet.getServlet(ManagedServlet.java:171)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:84)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)

ControllerServlet.java

@WebServlet("/ControllerServlet")
public class ControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private ProductDAOImpl productDAOImpl;

@Resource(lookup = "java:/PostgresNC")
private DataSource dataSource;


@Override
public void init() throws ServletException {
super.init();
try {
productDAOImpl = new ProductDAOImpl(dataSource);
} catch (Exception exc) {
throw new ServletException(exc);
}
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String theCommand = request.getParameter("command");

if (theCommand == null) {
theCommand = "PRODUCT_LIST";
}

switch (theCommand) {
case "PRODUCT_LIST":
listProducts(request, response);
break;
case "ADD_UPDATE":
addOrUpdateProduct(request, response);
break;
default:
listProducts(request, response);
}

} catch (Exception e) {
e.printStackTrace();
}
}

private void listProducts(HttpServletRequest request, HttpServletResponse response) throws Exception {

List<Product> products = productDAOImpl.getAllProducts();

request.setAttribute("PRODUCT_LIST", products);

RequestDispatcher dispatcher = request.getRequestDispatcher("/list-products.jsp");
dispatcher.forward(request, response);
}

private void addOrUpdateProduct(HttpServletRequest request, HttpServletResponse response) throws Exception {
// read product from the form
String idString = request.getParameter("productId");
String category = request.getParameter("category");
String description = request.getParameter("description");
String productName = request.getParameter("productName");
String price = request.getParameter("price");
BigDecimal priceBigDecimal = new BigDecimal(price);
String brand = request.getParameter("brand");

try {
int id = Integer.parseInt(idString.trim());
int numCategory = Integer.parseInt(category);

if(idString != null && category != null) {

Product theProduct = new Product(id, numCategory, description, productName, priceBigDecimal, brand);

// add the product to the database
productDAOImpl.saveOrUpdate(theProduct);
}
}
catch(NumberFormatException nfe) {
nfe.printStackTrace();
}

listProducts(request, response);
}

ProductDAOImpl.java

public class ProductDAOImpl implements ProductDAO {
private JdbcTemplate jdbcTemplate;

//private DataSource dataSource;

public ProductDAOImpl(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}

@Override
public List<Product> getAllProducts() {
String sql = "SELECT * FROM products ORDER BY prod_id\n" +
"ASC";
List<Product> productList = jdbcTemplate.query(sql, new RowMapper<Product>() {
@Override
public Product mapRow(ResultSet resultSet, int rowNum) throws SQLException {
Product theProduct = new Product();

theProduct.setProd_id(resultSet.getInt("prod_id"));
theProduct.setCategory(resultSet.getInt("category"));
theProduct.setDescription(resultSet.getString("description"));
theProduct.setProductName(resultSet.getString("productName"));
theProduct.setPrice(resultSet.getBigDecimal("price"));
theProduct.setBrand("brand");

return theProduct;
}
});
return productList;
}

@Override
public void saveOrUpdate(Product newProduct) {
if (newProduct.getProd_id() > 0) {
// update
String sql = "UPDATE products "
+ "SET category=?, description=?, prod_name=?, price=?, brand=? "
+ "WHERE prod_id=?";

jdbcTemplate.update(sql, newProduct.getCategory(), newProduct.getDescription(),
newProduct.getProductName(), newProduct.getPrice(), newProduct.getBrand());
} else {
// insert
String sql = "INSERT INTO products (prod_id, category, description, prod_name, price, brand) "
+ "VALUES (?, ?, ?, ?, ?, ?)";

jdbcTemplate.update(sql, newProduct.getProd_id(), newProduct.getCategory(), newProduct.getDescription(),
newProduct.getProductName(), newProduct.getPrice(), newProduct.getBrand());
}
} }

最佳答案

所需的 JAR 文件不可用,因此您需要将其放在 CLASSPATH 中。您可以选择将相应的 JAR 文件添加到您的 CLASSPATH,或者作为替代方案,您可以从 here 下载 JAR 文件。 .

同一主题的类似问题,供您引用:herehere .

关于java - NoClassDefFound错误: org/springframework/jdbc/core/RowMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41029708/

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