目录
[(1)DeptController类](#(1)DeptController类)
[(2)index.jsp](#(2)index.jsp)
[(1)DeptController类](#(1)DeptController类)
[(1)DeptController类](#(1)DeptController类)
[(2)启动tomcat](#(2)启动tomcat)
[(1)只有value](#(1)只有value)
[(2)只有value,但是没有提供](#(2)只有value,但是没有提供)
[(3)两个都有](#(3)两个都有)
一、创建项目
1.依赖
XML
<?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
<?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
<?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类
java
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()方法
java
/*
* @RequestBody
* */
@RequestMapping("/save2.do")
public String save2(@RequestBody String body){
System.out.println("请求体内容:"+body);
return "success";
}
( 2)index.jsp
html
<%--
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请求:
html
<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()方法
java
/*
* @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()方法
java
/*
* @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()方法
java
/*
* @CookieValue 获取到cookie的值
* */
@RequestMapping("/save5.do")
public String save5(@CookieValue(value = "JSESSIONID") String cookie){
System.out.println("值:"+cookie);
return "success";
}
②启动tomcat
( 2)只有value,但是没有提供
①DeptController类
加了一个save6()方法
java
@RequestMapping("/save6.do")
public String save6(@CookieValue(value = "cookie") String cookie){
System.out.println("值:"+cookie);
return "success";
}
②启动tomcat
( 3)两个都有
①DeptController类
加了一个save7()方法
java
@RequestMapping("/save7.do")
public String save7(@CookieValue(value = "cookie",defaultValue = "abc") String cookie){
System.out.println("值:"+cookie);
return "success";
}
②启动tomcat