环境准备
1.Eclipse Java EE版本
2.JDK,我本机使用的是1.80版
创建Maven项目
1.新建一个项目,选择other, 然后从maven下选择Maven Project
2.选定workspace后,选择一个Archetype。这里选择maven-archetype-webapp
3.填写项目的相关参数。包名,项目工程名。如下,本项目的工程名为maven。然后点击Finish。
4.项目创建成功后,首先打开pom.xml,引入Spring和Spring MVC相关的Jar包。前4个是Spring框架所依赖的Jar包,第5个是Spring MVC的Jar包,最后一个是解析JSP文件所需要的Jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
5.添加Tomcat的类库。
在项目上邮件-> Build Path -> Configure Build Path -> 选择Libraries选项卡-> Add Library -> Server Runtime -> 选择tomcat后,点击Finish。
此时,便可以将项目添加到tomcat中,并运行tomcat了。
通过浏览器访问localhost:8080/maven/,便能看到index.jsp里输出的内容了。
6.添加Controller控制器等
① 新建com.config包,并新建WebConfig。
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;
@Configuration
@EnableWebMvc
@ComponentScan("com.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();
}
}
@ComponentScan(“com.web”)表示扫描这个包下的所有类,自动发现Controller
② 在com.config包下新建RootConfig,内容如下
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages = {"com"}, excludeFilters = {
@Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)})
public class RootConfig {
}
③ 新建SpittrWebAppInitializer类
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpittrWebAppInitializer 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[] {"/"};
}
}
这里指定了RootConfig作为根配置,指定了DispatcherServlet配置声明在WebConfig中。而getServletMappings()方法则将"/"映射到DispatcherServlet上,并处理进入应用的所有请求。
④ 在com.web包下新建HomeController类
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping(value = "/home", method = GET)
public String home() {
return "home";
}
}
这里指定了请求是/maven/home,并返回"home"。根据WebConfig中的viewResolver()方法,返回的"home"会自动映射到/WEB-INF/views/home.jsp文件上。
⑤ 在/WEB-INF下新建views文件夹,并新建home.jsp文件,并在<body>标签中写入一下内容。重新发布项目到tomcat中,通过浏览器访问localhost:8080/maven/home便能看到home.jsp中的内容了。