简体   繁体   中英

SpringMVC - java.lang.NoClassDefFoundError: javax/servlet/ServletContext

So many trials done but the code is not executing successfully. All this code from Spring in Action 4th Edition 5th chapter and also this code is checked with their downloaded sample code, both are same, so i stopped thinking. Also I have googled so many links but cant get specific solution.

Configuring DispatcherServlet

public class SpittrWebappInitializer 
    extends AbstractAnnotationConfigDispatcherServletInitializer{

    protected Class<?>[] getRootConfigClasses(){
        return new Class<?>[] { RootConfig.class};
    }

    protected Class<?> [] getServletConfigClasses(){
        return new Class<?> [] { WebConfig.class};
    }

    protected String[] getServletMappings(){
        return new String[] {"/"};
    }
}

Enable Spring MVC

WebConfig.java

@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig 
        extends WebMvcConfigurerAdapter{ 

    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = 
                    new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(
                        DefaultServletHandlerConfigurer configurer){
        configurer.enable();
    }

}

RootConfig.java

@Configuration
@ComponentScan(basePackages={"spittr"},
            excludeFilters={
                @Filter(type=FilterType.ANNOTATION,      
                 value=EnableWebMvc.class)
            })
public class RootConfig {

}

HomeController.java

package spittr.web;
import static org.springframework.web.bind.annotation.RequestMethod.*;

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

@Controller
public class HomeController {

        @RequestMapping(value="/", method=GET)
        public String home(){
            return "home";
        }
}

View

The view name "home" is at /WEB-INF/views/home.jsp is already there.

Testing the controller

package spittr.web;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;

import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;


public class HomeControllerTest {
    @Test
    public void testHomePage() throws Exception {
        HomeController controller = new HomeController();

        //assertEquals("home", controller.home());

        MockMvc mockMvc = standaloneSetup(controller).build();

        mockMvc.perform(get("/"))
            .andExpect(view().name("home"));

    }

}

Project libraries are following

You need servlet-api in your classpath. If you are using maven, just add the following dependency in your POM:

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

If you are not using Maven, just download the servlet-api artifact from Maven central and keep it in your class path.

Spring MVC is a web framework and its built on top of Java servlets specification.

So the jar that implements this spec has to be in a classpath. More specifically ServetContext resides in :

javax.servlet:javax.servlet-api:3.1.0 (the version might be different depending on your spring version I guess, but this is a good point to start)

So just make sure it resides in a classpath and try to start your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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