欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringMVC-快速入门

程序员文章站 2022-03-26 11:13:09
1.导入SpringMVC的相关坐标(其他jar包省略) org.springframework spring-webmvc 5.2.9.RELEASE 2.配置SpringMVC核心控制...

1.导入SpringMVC的相关坐标(其他jar包省略)

	<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>

2.配置SpringMVC核心控制器DispathcerServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

<!--    配置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>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

<!--    配置监听器,spring-web提供的-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--    全局初始化参数,告诉配置文件路径-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
</web-app>

3.创建controller层类和页面

@Controller
public class UserController {

    @RequestMapping("/save")
    public String save(){
        return "success.jsp";
    }
}

4.使用注解配置Controller类中业务方法的映射地址
第三步以经实现

5.配置spring mvc核心配置文件spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd">

<!--    Controller组件扫描-->
    <context:component-scan base-package="com.hao.controller"/>

</beans>

6.客户端测试
启动tomcat服务器-》访问save -》测试成功

本文地址:https://blog.csdn.net/Kevinnsm/article/details/109999971

相关标签: Java springmvc