spring mvc 基础入门

1.启动服务器,加载一些配置文件

DispatcherServlet 对象被创建
springmvc.xml 被加载
HelloController 对象被创建
InternalResourceViewResolver 视图解析器对象被创建
开启mvc的注解支持

对应代码

初始网站页面index.jsp

<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2020/4/11
  Time: 12:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <h3>入门程序</h3>

    <a href="hello">12</a>

</body>
</html>
View Code

springmvc.xml 配置文件:开启注解支持,开启注解bean的扫描包,实例化视图解析器对象,

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

    <!-- 开启注解扫描的包 -->
    <context:component-scan base-package="cn.cast"/>

    <!--视图解析器对象-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--开启SpringMVC框架注解的支持-->
    <mvc:annotation-driven/>

</beans>
View Code

web.xml 配置文件:配置DispatcherServlet(拦截器)及其属性

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <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:springmvc.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>

</web-app>
View Code

HelloContoller 类:用于处理/hello的请求

package cn.cast.controller;

/**
 * 控制器类
 */

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
    /**
     * 请求映射注解:path=/hello
     *      /hello就变成这个方法执行的请求路径
     * @return
     */
    @RequestMapping(path = "/hello")
    public String sayHello(){
        System.out.println("Hello SpringMVC");
        return "success";
    }

}
View Code

success.jsp:处理请求后返回的响应页面

<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2020/4/11
  Time: 13:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <h3>入门成功</h3>

</body>
</html>
View Code

2.后台处理请求

  后台收到 <a href="hello">12</a> 的请求

  --> 所有请求被 DispatcherServlet 拦截处理

  --> 拦截器调用HelloController的sayHello(已经被配置过用来相应/hello请求)方法

  --> 方法执行完后返回 “success”

  -->拦截器通过视图解析器 解析 “success” 加上prefix和suffix,令页面跳转到success.jsp

  -->返回的相应包里就是这个success.jsp

猜你喜欢

转载自www.cnblogs.com/zsben991126/p/12679262.html