Spring MVC工程搭建

SpringMVC请求映射注解请求映射注解
请求映射注解 说明
@RequestMapping 通用的请求处理
@GetMapping 处理 HTTP GET 请求
@PostMapping 处理 HTTP POST 请求
@PutMapping 处理 HTTP PUT 请求
@PatchMapping 处理 HTTP PATCH 请求
@DeleteMapping 处理 HTTP DELETE 请求
建议在类级别上只使用 @RequestMapping ,用于指定基本路径。而在每个处理器方法上,使用更具体的请求映射注解,比如 @GetMapping。

以下是使用 Spring Web 注解定义控制器的典型示例:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.List;

/**

  • 商品控制器

  • @author Deniro Lee
    */
    @Controller
    @RequestMapping("/product")
    public class ProductController {

    @GetMapping("/show")
    public String show(Model model) {
    List products = Arrays.asList(
    new Product(“1”, “xx 微波炉”, 2299),
    new Product(“2”, “xx 电视”, 1789),
    new Product(“3”, “xx 笔记本电脑”, 5399)

     );
    
     model.addAttribute("products", products);
     return "product";
    

    }
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Spring MVC工程搭建
创建项目
1.创建与配置Maven工程

添加web
右键我们的项目名 -> 选择“Add Framework Support”
选择WebApplication
核实版本号
核实是否会自动创建web.xml
添加好后会出现如下文件夹
在这里插入图片描述

搭建配置SpringMVC
引入依赖

junit junit 4.13.2 test org.springframework spring-webmvc 5.2.13.RELEASE javax.servlet servlet-api 2.5 javax.servlet javax.servlet-api 4.0.1 provided 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 刷新maven等待自动下载

libraries中有了所有导入的包表示依赖引入完成
在这里插入图片描述

配置静态资源导出



src/main/java

/*.properties
/.xml

false


src/main/resources

**/.properties
**/*.xml

false



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Spring核心配置文件
文件路径为\src\main\resources文件名为applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- bean definitions here -->
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 添加SpringMVC配置内容 annotation-driven配置就是帮助我们自动完成上述两个实例的注入

mvc:annotation-driven/

1
2
3
静态资源过滤

mvc:default-servlet-handler/

1
2
3
视图解析器

1 2 3 4 5 编写代码测试 编写Controller层 controller包下新建HelloController类

package controller;

@Controller
public class HelloController {

@RequestMapping("/hello")
public String hello(Model model){
    // Model 封装数据
    model.addAttribute("msg","HELLO MY FIRST SPRING MVC PROJECT");

    // 返回的字符串就是视图的名字 会被视图解析器处理
    return "hello";
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Spring核心配置文件:applicationContext.xml

<context:component-scan base-package=“controller”/>
1
2
编写jsp

WEB-INF包下新建jsp包,jsp包下新建hello.jsp文件

<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>

Title ${msg} 1 2 3 4 5 6 7 8 9 编写web.xml 配置前端控制器 springmvc org.springframework.web.servlet.DispatcherServlet 1 2 3 4 5 配置初始化参数 在服务器启动时 加载spring的核心配置文件applicationContext.xml

配置初始化参数的代码写在前端控制器内

contextConfigLocation classpath:applicationContext.xml 1 2 3 4 5 设置启动级别

1

1
2
3
设置SpringMVC拦截请求

springmvc / 1 2 3 4 5 配置中文乱码过滤器 encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 encodingFilter /* 1 2 3 4 5 6 7 8 9 10 11 12 13 运行web项目 点击绿色的小三角运行TomCat,出现如下内容表示运行成功
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值