繁体   English   中英

@Controller导致java.lang.NoClassDefFoundError:javax / servlet / http / HttpServletRequest

[英]@Controller causing java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest

@Controller添加到DummyController类时,为什么会出现java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest

it.cspnet.firstspringmvc.controller.Main

   public static void main(String args[]) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("jpaContext.xml");
        Servizi servizi = ctx.getBean(Servizi.class);
        Utente utente = new Utente();
        utente.setUserName("test");
        utente.setPassword("test");
        Utente utenteInDb = servizi.login(utente);

        for (Ordine ordine : utenteInDb.getOrdini()) {
            System.out.println("ordine: " + ordine);
        }
    }

it.cspnet.firstspringmvc.controller.DummyController

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;

@Controller
public class DummyController {

    @RequestMapping(value = "/dummy", method = {RequestMethod.GET})
    public String get(Model model, HttpServletRequest request) {
        return "dummy";
    }
}

当我从DummyController中删除@Controller批注时, main将打印出example的内容,但是如果我将其放回原处,则会抛出该错误:

线程“主”中的异常java.lang.NoClassDefFoundError:java.lang.Class.getDeclaredMethods0(本地方法)处的javax / servlet / http / HttpServletRequest

我正在使用这个项目:

https://github.com/ivansaracino/spring-mvc-jparepository-example.git

我要做的就是添加MainDummyController

您可能缺少正确的依存关系,例如:

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

请注意,您的版本可能是2.5、3.0或3.1-取决于您使用的应用程序服务器。 同样,当您要创建可执行文件war时,可能不应该使用提供的范围(取决于servlet容器)。

您的依赖项范围是“提供的”,因此在进行战争时,该依赖项不会添加到类路径中! 确保依赖关系存在于App Server库路径上。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM