令人反感的类:javax / servlet / Servlet.class

时间:2019-04-09 22:29:21

标签: java spring maven tomcat servlets

我正在构建一个简单的Spring应用程序,我想在其中从AJAX调用控制器。 但是在使用servlet-api时会遇到一些问题。我在使用HttpServletRequest和HttpServletResponse时在POM文件中添加了依赖性。

// test setup
// ...
    const server = new Hapi.Server();
    // ...
    await server.register({
        plugin: require('../../server/views'),
        options: {
            path: path.join(__dirname, '../views/templates'),
            realServer: server // <= added option
        }
    });

如果我不包括此jar依赖项,则HttpServletRequest无法解决发生的类型问题。并且如果我将maven依赖项包括在内并在Deployment程序集中添加这些maven依赖项,则发生以下提及的错误,并且甚至没有启动tomcat

信息:validateJarFile(D:\ workspaceUtilities.metadata.plugins \ org.eclipse.wst.server.core \ tmp0 \ wtpwebapps \ SpringControllerWebApp \ WEB-INF \ lib \ servlet-api-2.5.jar)-未加载jar 。参见Servlet Spec 3.0,第10.7.2节。令人反感的类:javax / servlet / Servlet.class

如果我从部署程序集中删除了Maven依赖项,则会启动tomcat服务器,但出现以下错误:

严重:Web应用程序[/ SpringControllerWebApp]中的Servlet [dispatcher]抛出load()异常 java.lang.ClassNotFoundException:org.springframework.web.servlet.DispatcherServlet

请帮助我解决此问题。

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

Controller class:

package com.src;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.java.Employee;



@Controller
@RequestMapping("/employee/add.htm")
public class EmployeeController {
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody Employee add(HttpServletRequest request,HttpServletResponse response) throws Exception {

    System.out.println("EmployeeController--Start");

    Employee employee = new Employee();
    String firstName = request.getParameter("firstName");
    String lastName = request.getParameter("lastName");
    String email = request.getParameter("email");
    employee.setEmail(email);
    employee.setFirstName(firstName);
    employee.setLastName(lastName);

    System.out.println("EmployeeController--End");
    return employee;
    }
     }

0 个答案:

没有答案