Recently, I am reading the book Spring in Action to learn to build a basic Spring MVC project. Because I am self-taught, I have encountered many pits. I hope that the majority of Wolf friends will not be tortured like me……
I used JDK 1.8; tomcat 9 ; IDEA 2018
After configuring the environment, open IDEA and create a Spring MVC project. IDEA is automatically created with the Spring MVC project:
After creation, IDEA will automatically download some files used by Spring. Wait a moment.
Then configure the Tomcat server, which is not covered here;
After configuring the server, try the following code:
package spittr.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.web.servlet.config.annotation.EnableWebMvc; /** * @author mojiayi * @date 2019-01-06 15:26 */ @Configuration @ComponentScan(basePackages = {"spittr"}, excludeFilters = @ComponentScan.Filter(type= FilterType.ANNOTATION, value = EnableWebMvc.class)) public class RootConfig { } package spittr.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; /** * @author mojiayi * @date 2019-01-06 15:21 */ public class SpitterWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<? >[] getRootConfigClasses() { return new Class<? >[]{RootConfig.class}; } @Override protected Class<? >[] getServletConfigClasses() { return new Class<? >[]{WebConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } } package spittr.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; /** * @author mojiayi * @date 2019-01-06 15:26 */ @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(); } } package spittr.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import static org.springframework.web.bind.annotation.RequestMethod.GET; /** * @author mojiayi * @date 2019-01-06 16:00 */ @Controller public class HomeController { @RequestMapping(value="/", method = GET) public String home(){ return "home"; }} Put the above code in your SRC folder and run...... You will find that Tomcat does not work, and it will prompt you to check the server log. To save time, I will directly tell you how to change it:Copy the code
The main reason for this error is that the above code uses a Java configuration file, not a web.xml configuration file, so the dispatcher.servlet and configuration file configured in the WEB.xml automatically created by IDEA are not available. That’s the code above)
The dispatcher above is webconfig.java; ContextConfigLocation stands for rootconfig.java; So it’s best to delete web.xml or comment out the content.
Just run it.
Some wolves may lack the environment’s JAR package at runtime, so it is recommended to use servlet.jar; JSTL. Jar; The standard. Jar packages are stored in the Tomcat lib directory and imported into the project. The hierarchical directory for the entire project is shown below:
Next, run the Tomcat server. Do not run home.jsp directly. The file is in the WEB-INF directory and cannot be accessed directly.
A basic Spring MVC project is now complete.