Spring MVC教程一 (hello world!)

作为一个懒癌患者,做程序也几年了,没啥长进。在尚硅谷看到spring mvc的视频讲解非常好,希望自己可以按照视频写一套文字的教程。也希望能坚持点什么,同时能帮助别人更好的学习spring mvc这项技术。

工具

  • IntelliJ IDEA
  • apache-tomcat-9.0.12-windows-x64

入门之hello world

项目创建
  • 创建工程

创建maven项目

  • 输入公司名或组织名
    在这里插入图片描述
  • 选择使用的maven
    在这里插入图片描述
  • 点击next输入项目保存的地址后,maven项目创建成功。在这里插入图片描述
  • 创建java包及资源文件包
    在这里插入图片描述
    将java包在这里插入图片描述
    在这里插入图片描述
    好的,现在一个完整的maven web项目已经完成,下面可以开始spring mvc的开发了。
hello world 开发
  • 添加Spring MVC依赖添加Spring MVC依赖
 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.1.3.RELEASE</version>
 </dependency>
  • 配置web.xml
<!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 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">
 <display-name>Archetype Created Web Application</display-name>
 <!--配置DispatcherServlet-->
 <servlet>
   <servlet-name>springDispatcherServlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <!--配置DispatcherServlet的一个初始化参数,配置SpringMVC配置文件的位置和名称-->
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:spring/spring-mvc.xml</param-value>
   </init-param>
   <!--servlet加载的时候创建-->
   <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
   <servlet-name>springDispatcherServlet</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>	
  • 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:p="http://www.springframework.org/schema/p"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
      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.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

       <!--配置自动扫描的包-->
       <context:component-scan base-package="com.erricsson.panda.controller.*"></context:component-scan>

       <!-- 开启注解 -->
       <mvc:annotation-driven />

       <!--配置视图解析器-->
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="prefix" value="/"/>
           <property name="suffix" value=".jsp"/>
       </bean>
</beans>
  • controller代码
package com.xcl.controller;

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

/**
 * @author chuanlei.xu
 * @version 1.0
 * @date 2019/2/20 11:02
 */

@Controller
public class HelloWorld {
    /*
    * 使用@RequestMapping来映射请求的url
    *
    */
    @RequestMapping("/helloworld")
    public String helloworld(){

        System.out.print("hello world");
        return "success";
    }
}

RequestMapping除了可以修饰方法外,还可以修饰类。比如在HelloWorld类上添加修饰@RequestMapping(“/request”),则url变为/request/helloworld

  • index.jsp
<html>
<body>

    <a href="helloworld">hello world</a>
</body>
</html>
  • success.jsp
<html>
<body>
    success
</body>
</html>
  • 运行环境配置
    在这里插入图片描述
    在这里插入图片描述

  • 运行
    在这里插入图片描述
    至此hello world 项目完成。

  • github 地址
    https://github.com/chuanleixu/SpringMVC_Tutorials

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值