springmvc入门案例演示

工程目录图
springmvc入门案例演示_第1张图片

1、创建maven项目,选择maven-archeType-webapp模型

springmvc入门案例演示_第2张图片

2、导入pom

<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
    <spring.version>5.0.2.RELEASEspring.version>
  properties>

  <dependencies>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>servlet-apiartifactId>
      <version>2.5version>
      <scope>providedscope>
    dependency>
    <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>jsp-apiartifactId>
      <version>2.2version>
      <scope>providedscope>
    dependency>
  dependencies>

3、webapp下新建index.jsp

<%--
  Created by IntelliJ IDEA.
  User: 13754
  Date: 2021-07-08
  Time: 10:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>

    <h3>入门程序h3>

    <a href="/hello">入门程序a>

body>
html>

4、新建controller

package com.lian.springmvc;

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

/**
 * 1、想要让一个类的方法执行,需要让这个类变为对象
 * 2、前端控制器DispatcherServlet执行流程:
 * load-on-startup配置让服务器启动就创建DispatcherServlet类
 * DispatcherServelet就会用init-param加载springmvc.xml
 * springmvc.xml 就会用 context:component-scan 开启注解扫描
 * 注解扫描就会使 @Controller 注解生效,且spring注解controller自动创建bean实体类
 */
@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(){
        System.out.println("hello");
        return "success";
    }

}

5、resource下新建springmvc.xml配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/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="com.lian.springmvc"/>


    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    bean>


    <mvc:annotation-driven/>

beans>

6、web.xml配置前端控制器DispatcherServlet



<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>


  <servlet>
    <servlet-name>dispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc.xmlparam-value>
    init-param>

    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

web-app>

7、WEB-INF/page下新建success.jsp

<%--
  Created by IntelliJ IDEA.
  User: 13754
  Date: 2021-07-08
  Time: 10:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
    <h1>成功页面h1>
body>
html>

8、页面测试

springmvc入门案例演示_第3张图片
springmvc入门案例演示_第4张图片

你可能感兴趣的:(springmvc)