SpringMVC的入门程序

SpringMVC的入门程序

文章目录

        • SpringMVC的入门程序
          • 1. 创建WEB工程
          • 2. 配置核心控制器
          • 3. 核心配置文件
          • 4. 页面创建
          • 5. 创建控制器
          • 6. 测试

1. 创建WEB工程

创建springmvc工程,同时修改pom.xml,在pom.xml中引入springmvc所需依赖jar包,具体的坐标如下:


<properties>
    <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>javax.servlet-apiartifactId>
        <version>3.0.1version>
        <scope>providedscope>
    dependency>

    <dependency>
        <groupId>javax.servlet.jspgroupId>
        <artifactId>jsp-apiartifactId>
        <version>2.0version>
        <scope>providedscope>
    dependency>
dependencies>
2. 配置核心控制器

在web.xml配置文件中核心控制器DispatcherServlet,核心控制器是一个servlet配置。


<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">


<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>
3. 核心配置文件

在resources下编写springmvc.xml的配置文件,主要有3点配置,分别为包扫描、springmvc注解驱动、视图解析器,代码如下:


<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="com.itheima">context:component-scan>
    
    
    <mvc:annotation-driven>mvc:annotation-driven>
    
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/">property>
        <property name="suffix" value=".jsp">property>
    bean>
beans>
4. 页面创建

webapp下创建index.jsp,jsp代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>springmvc入门案例title>
head>
<body>
    <h3>入门案例h3>
    <a href="/hello">入门案例a>
body>
html>

在WEB-INF目录下创建pages文件夹,编写success.jsp的成功页面,jsp代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>demotitle>
head>
<body>
    第一个SpringMVC入门案例
body>
html>
5. 创建控制器

在com.itheima.controller包下创建HelloController

package com.itheima.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
    /**
     * 接收请求
     * @return
     */
    @RequestMapping(path="/hello")
    public String sayHello() {
        System.out.println("Hello SpringMVC!!");
        return "success";
    }
}

完整工程如下:
SpringMVC的入门程序_第1张图片

6. 测试

启动Tomcat服务器,进行测试,输入访问地址 http://localhost:8585/

你可能感兴趣的:(框架)