一,springMVC入门程序hello world

内容概述:
1,搭建环境(导入配置,编写配置文件,完善项目结构)
2,编写测试用的入门程序
3,部署项目到tomcat服务器上
4,运行成功查看

一,搭建环境:

1,建立webapp项目,在main下面建立java,resources文件,并设为源文件。
图一:
在这里插入图片描述
图二:
在这里插入图片描述
图三:
在这里插入图片描述
图四:
在这里插入图片描述
图五:
在这里插入图片描述
2,在pom.xml的文件中导入SpringMVC需要的配置

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>10.0.1</maven.compiler.source>
    <maven.compiler.target>10.0.1</maven.compiler.target>
    <spring.version>5.2.0.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
  </dependencies>

在这里插入图片描述
3,在resources文件下建立SpringMVC.xml文件,配置里面的基本信息:

<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"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <!-- 配置组件扫描 -->
    <context:component-scan base-package="cn.itcast" />
    <!-- 配置MVC注解扫描 -->
    <mvc:annotation-driven />
    <!-- 配置视图解析器, -->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" /><!-- 前缀 -->
        <property name="suffix" value=".jsp" /><!-- 后缀 -->
    </bean>

在这里插入图片描述
4,在web.xml配置SpringMVC前端控制器的映射,和初始化

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

在这里插入图片描述

二,编写测试文件

1,编写index.jsp文件,模拟前端页面发送一个请求
在这里插入图片描述
2,前端控制器接收到请求,处理请求
在这里插入图片描述
3,视图解析器接收处理请求后返回的信息,跳转到目标页面
在这里插入图片描述
4,跳转的页面
在这里插入图片描述

三,部署服务器(已经安装tomcat服务器)

图一:选择右上角的编辑
在这里插入图片描述
图二:选择本地的tomcat服务器在这里插入图片描述
图三:编写必要的信息
在这里插入图片描述
图四:添加要部署的项目
在这里插入图片描述

四,测试结果:

图一:点击右上角运行的按钮
在这里插入图片描述
图二:运行成功后,自动跳转到浏览器页面,点击链接模拟发送请求

在这里插入图片描述
图三:成功跳转到success.jsp页面,并在服务端打印处理请求方法中的信息
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值