当前位置: 首页 > article >正文

springMVC---常用注解

目录

一、创建项目

1.依赖

2.web.xml

3.spring-mvc.xml

二、RequestParam注解

1.作用

2.属性

3.代码

DeptController类

启动tomcat

三、RequestBody注解 

1.作用

2.属性

3.代码

(1)DeptController类

(2)index.jsp

(3)启动tomcat

四、PathVaribale注解

1.作用

2.属性

3.Restful风格的URL

4.代码

(1)DeptController类

(2)web.xml

(3)启动tomcat

五、RequestHeader注解

1.作用

2.属性

3.代码

(1)DeptController类

(2)启动tomcat

六、CookieValue注解

1.作用

2.属性

3.代码

(1)只有value

(2)只有value,但是没有提供

(3)两个都有


一、创建项目

1.依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qcby</groupId>
    <artifactId>springMVC13</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--版本锁定-->
        <spring.version>5.0.2.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!-- 配置过滤器,解决中文乱码的问题 -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!-- 指定字符集 -->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!--前端控制器-->
    <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:spring-mvc.xml</param-value>
        </init-param>
        <!--配置启动加载-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

3.spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!-- 配置spring创建容器时要扫描的包 -->
    <context:component-scan base-package="com.qcby.controller"></context:component-scan>

    <!-- 配置视图解析器 -->
    <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>

    <!-- 配置spring开启注解mvc的支持-->
    <!--    <mvc:annotation-driven></mvc:annotation-driven>-->
</beans>

二、RequestParam注解

1.作用

把请求中的指定参数传递给控制器中的形参赋值

2.属性

(1)value:请求参数中的名称

(2)required:请求参数中是否必须提供此参数,默认是true(必须提供),false(表示可以不传值)

3.代码

DeptController类

package com.qcby.controller;

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

@Controller
@RequestMapping("/dept")
public class DeptController {
    /*
    * RequestParam注解
    * required=false, 默认为true,表示必须传值,不传就会报错,抛出异常;为false表示可以不传值
    * defaultValue="abc",abc是传的值;如果没有传值,就按默认值,默认值是""(空字符串)
    * */
    @RequestMapping("/save1.do")
    public String save(@RequestParam(value = "username",required = false,defaultValue = "abc") String name ){
        System.out.println("姓名:"+name);
        return "success";
    }
}

启动tomcat

三、RequestBody注解 

1.作用

用户获取请求体的内容(注:get方法不可以

2.属性

required:是否必须有请求体,默认是true

3.代码

(1)DeptController类

加了一个save2()方法

/*
* @RequestBody
* */
@RequestMapping("/save2.do")
public String save2(@RequestBody String body){
    System.out.println("请求体内容:"+body);
    return "success";
}

(2)index.jsp

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2024/11/3
  Time: 19:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<form action="dept/save2.do" method="post">
    用户名:<input type="text" name="name" /><br/>
    密码:<input type="password" name="password" /><br/>
    年龄:<input type="text" name="age" /><br/>
    <input type="submit" value="提交" />
</form>

</body>
</html>

(3)启动tomcat

现在<form>换成get请求:

<form action="dept/save2.do" method="get">
    用户名:<input type="text" name="name" /><br/>
    密码:<input type="password" name="password" /><br/>
    年龄:<input type="text" name="age" /><br/>
    <input type="submit" value="提交" />
</form>

 运行

四、PathVaribale注解

1.作用

有绑定url中的占位符的

例:url中有/delete/{id},{id}就是占位符

2.属性

value:指定url中的占位符

3.Restful风格的URL

(1)请求路径一样,可以根据不同的请求方式去执行后台的不同方法

(2)Restful风格的URL优点

①结构清晰

②符合标准

③易于理解

④扩展方便

4.代码

(1)DeptController类

加了一个save3()方法

/*
* @PathVariable 指定url占位符的名称
* */
@RequestMapping(path = "/save3.do/{id}")
public String save3(@PathVariable(value = "id") Integer id){
    System.out.println("id:"+id);
    return "success";
}

(2)web.xml

(3)启动tomcat

五、RequestHeader注解

1.作用

获取指定请求头的值

2.属性

value:请求头的名称

3.代码

(1)DeptController类

加了一个save4()方法

/*
* @RequestHeader  获取请求头的值
* */
@RequestMapping("/save4.do")
public String save4(@RequestHeader(value = "Accept-Language") String header){
    System.out.println("Accept请求头的值:"+header);
    return "success";
}

(2)启动tomcat

六、CookieValue注解

1.作用

用于获取指定cookie的名称的值

2.属性

value:cookie的名称

defaultValue:如果没有value提供的值,则使用defalutValue的值

3.代码

(1)只有value

①DeptController类

加了一个save5()方法

/*
* @CookieValue 获取到cookie的值
* */
@RequestMapping("/save5.do")
public String save5(@CookieValue(value = "JSESSIONID") String cookie){
    System.out.println("值:"+cookie);
    return "success";
}

②启动tomcat

(2)只有value,但是没有提供

①DeptController类

加了一个save6()方法

@RequestMapping("/save6.do")
public String save6(@CookieValue(value = "cookie") String cookie){
    System.out.println("值:"+cookie);
    return "success";
}

②启动tomcat

(3)两个都有

①DeptController类

加了一个save7()方法

@RequestMapping("/save7.do")
public String save7(@CookieValue(value = "cookie",defaultValue = "abc") String cookie){
    System.out.println("值:"+cookie);
    return "success";
}

②启动tomcat


http://www.kler.cn/a/502912.html

相关文章:

  • k8s 搭建 zookeeper集群
  • .NET 在控制台或者桌面程序中使用依赖注入
  • 在IDEA上运行Java项目
  • 如何解决Webview和H5缓存问题,确保每次加载最新版本的资源
  • C# 中的 Stream
  • stacking中如何把基础学习器设置为ann神经网络
  • springMVC---resultful风格
  • 解除WPS登录限制
  • java流式处理zip+多线程
  • 黑马linux入门笔记(01)初始Linux Linux基础命令 用户和权限 实用操作
  • 【HTML+CSS+JS+VUE】web前端教程-31-css3新特性
  • android studio实现圆形图片
  • Spring拦截链揭秘:如何在复杂应用中保持控制力
  • 基于物联网表计的综合能源管理方案
  • 排序:插入、选择、交换、归并排序
  • C++学习指南(七)——stack/queue/priority_queue
  • 【Leetcode-两数之和】利用双指针、快排思路解决两数之和问题
  • 当你不小心使用了MySQL的保留字作为字段名而导致你的SQL语法解析错误该怎么办!
  • kubernetes第八天
  • 18.C语言文件操作详解:指针、打开、读取与写入