如何搭建Spring MVC项目是所有Spring MVC初学者面临的一个问题,本文我们就为大家详细的介绍如何 搭建Spring MVC项目步骤,以供大家参考。
3.1 添加依赖
要想使用Spring MVC,首先我们需要在pom.xml中添加如下依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- 其他Web依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
因为后面要将项目部署到Tomcat,所以我们在pom.xml中配置下打包方式为war包:
<packaging>war</packaging>
<build>
<plugins>
<!--其他配置-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
3.2 新建演示页面
在src/main/resources下新建views目录,然后在此目录下新建index.jsp页面如下所示:
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
<title>Spring MVC</title>
</head>
<body>
<pre>
Welcome to Spring MVC world
</pre>
</body>
</html>
这里可能存在的问题是,右键新建JSP文件时,没有JSP文件模板,就像下面这样:
解决方案如下所示:
依次点击File--Project Structure,打开Project Structure对话框,左侧选中Modules,然后点击+号,选择Web。
此时再次右键新增JSP文件,就会看到JSP文件模板:
此时项目根目录会生成1个web文件夹,可以将其删除。
3.3 Spring MVC配置
新建配置类MyMvcConfig如下所示:
package chapter05.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
/**
* Spring MVC配置
*/
@Configuration
@EnableWebMvc
@ComponentScan("chapter05")
public class MyMvcConfig {
/**
* 视图解析器配置
*
* @return
*/
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/classes/views/");
viewResolver.setSuffix(".jsp");
viewResolver.setViewClass(JstlView.class);
return viewResolver;
}
}
注意事项:
1)该配置类使用了@EnableWebMvc注解来启用Spring MVC,它会开启一些默认配置。
2)该配置类配置了视图解析器,这里我们配置的是JSP的视图解析器。
视图解析器中,我们设置了前缀和后缀,如果控制器中返回的逻辑视图名称是index,实际渲染时找的视图就是/WEB-INF/classes/views/index.jsp,为什么设置的前缀是/WEB-INF/classes/views/而不是/src/main/resources/views/呢,那是因为项目编译完运行时的目录是/WEB-INF/classes/views/。
如果编译完成该目录下没有jsp文件,则需要在pom.xml中添加如下配置
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.jsp</include>
<include>**/*.js</include>
</includes>
</resource>
</resources>
</build>
3.4 Web配置
新建Web配置类WebInitializer如下所示:
package chapter05.config;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(MyMvcConfig.class);
context.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
这里重点要关注的是,该类实现了接口WebApplicationInitializer并重写了onStartup()方法,WebApplicationInitializer类是Spring提供用来配置Servlet 3.0+版本配置的接口,从而可以替代掉web.xml。
3.5 新建控制器
新建控制器HelloController如下所示:
package chapter05.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/index")
public String hello() {
// 这里返回的逻辑视图名
return "index";
}
}
上面代码中的@Controller注解声明这是1个控制器,@RequestMapping("/index")用来配置URL映射。
上述内容就是整个搭建Spring MVC项目的过程,当然后续还需要一些操作才能正式完成Spring MVC项目。后续内容可以在本站的Spring MVC视频教程中查看。同时,也会为你解锁更多的Spring MVC相关技术,记得去观看学习哦。
QCode09-04 14:38
Code大师09-04 14:50
不写代码你养我啊08-23 11:14
不写代码你养我啊09-17 18:02
要学习了06-18 18:13