SpringMVC快速入门
SpringMVC快速入门
需求:客户端发起请求,服务器端接收请求,执行逻辑并进行视图跳转。
开发步骤:
1导入SpringMVC相关坐标
2配置SpringMVC核心控制器DispathcerServlet
3创建Controller类和视图页面
4使用注解配置Controller类中业务方法的映射地址
5配置SpringMVC核心文件spring-mvc.xml
6客户端发起请求测试
导入Spring和SpringMVC的坐标
<!--Spring坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--SpringMVC坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
2在web.xml配置SpringMVC的核心控制器
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value> //spring=mvc的配置文件
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> //默认会拦截html和jsp的请求需要加上下面的mapping
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
3创建Controller和业务方法
@Controller //Spring Servlet的新注解
public class QuickController {
@RequestMapping("/quick")
public String quickMethod(){
System.out.println(" running.....");
return "index.html"; //功能上相当于转发
}}
4创建spring-mvc.xml
本文来自博客园,作者:冰莫莫,转载请注明原文链接:https://www.cnblogs.com/icemomo/p/16019947.html