1.springmvc的流程---同步
1. 客户端发生请求http://localhost:8080/145springmvc01/hello01
2. 来的tomcat服务器。
3. springmvc的前端控制器DipatcherServlet接受所有的请求。
4. 查看你的请求地址和哪个@RequestMaping匹配。
5. 执行对应的方法。方法会返回一个字符串。springmvc把该字符串解析为要转发的网页。
6. 把该字符串经过视图解析器拼接。
7. 拿到拼接的地址,找到对应的网页。2. springmvc入门
(1)创建一个web工程
(2)引入springmvc的依赖。
<!--maven可以把该依赖相关的依赖都引入。-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>(3) 创建控制层。
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @program: springMVC_145
* @description:
* @author: zlh
* @create: 2021-12-06 16:58
**/
@Controller
public class Hello01 {
@RequestMapping("/a")
public String aaa(){
return "aaa";
}
}
(4)配置springmvc的配置文件。
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1.包扫描 扫描我们自己的控制层类所在的包或者父包-->
<context:component-scan base-package="com.ykq.controller"/>
</beans>(5)引入前端控制器 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">
<!--注册servlet-->
<servlet>
<servlet-name>springmvc01</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载springmvc的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
</servlet>
<!--servlet映射路径
/: 表示所有的控制层路径 以及静态资源
/*: 表示全部 包含jsp网页
-->
<servlet-mapping>
<servlet-name>springmvc01</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>(6)测试。
运行tomcat在网页访问controller中RequestMapping定义的路径
3.springmvc如何接受请求参数。
3.1.接受参数个数少的。
1.前台页面
<%--
Created by IntelliJ IDEA.
User: zlh
Date: 2021/12/6
Time: 17:05
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="/hello/a">
<input type="text" name="name">
<input type="text" name="age">
<button>提交</button>
</form>
</body>
</html>2.controller层
RequestParam 给接收的值起别名 接参得和前台name对应
value 书写别名的名字
required true 必传项 false 非必传 设置为false时 需要defaultValue 为它设置默认值
@RequestParam(value="names",required = false,defaultValue="李四")
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @program: springMVC_145
* @description:
* @author: zlh
* @create: 2021-12-06 16:58
**/
@Controller
@RequestMapping("hello")
public class Hello01 {
@RequestMapping("/a")
public String aaa(String name,Integer age){
System.out.println(name+"+++"+age);
return "aaa";
}
}
3.2.接受的实体参数。
1.实体
接收日期型参数需要转换类型
@DateTimeFormat(pattern="yyyy-MM-dd") 前端传递时间格式进行转换
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss") 把java对象转为json格式
package com.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
* @program: springMVC_145
* @description:
* @author: zlh
* @create: 2021-12-04 14:39
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String a;
private String b;
private String c;
private String d;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date date;
}
2.前台页面
<%--
Created by IntelliJ IDEA.
User: zlh
Date: 2021/12/4
Time: 14:21
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="/c">
第一个值<input type="text" name="a"><br>
第2个值<input type="text" name="b"><br>
第3个值<input type="text" name="c"><br>
第4个值<input type="text" name="d"><br>
日期<input type="date" name="date">
<%--button不设置type默认submit--%>
<button>提交</button>
</form>
</body>
</html>
3.控制层
package com.controller;
import com.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;
/**
* @program: springMVC_145
* @description:
* @author: zlh
* @create: 2021-12-04 09:17
**/
@Controller
public class HelloController {
@RequestMapping("/c")
public String c(User user){
System.out.println(user);
return "aaa";
}
}
3.3.接受的Map参数。
1.前端页面
<%--
Created by IntelliJ IDEA.
User: zlh
Date: 2021/12/4
Time: 14:21
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="/b">
第一个值<input type="text" name="a"><br>
第2个值<input type="text" name="b"><br>
第3个值<input type="text" name="c"><br>
第4个值<input type="text" name="d"><br>
日期<input type="date" name="date">
<%--button不设置type默认submit--%>
<button>提交</button>
</form>
</body>
</html>
2.控制层
package com.controller;
import com.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;
/**
* @program: springMVC_145
* @description:
* @author: zlh
* @create: 2021-12-04 09:17
**/
@Controller
@RequestMapping("/b")
public String b(@RequestParam Map map){
System.out.println(map);
return "aaa";
}
}
4.接收日期格式类型
<mvc:annotation-driven/> 默认会帮我们注册默认处理请求,参数和返回值的类
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.包扫描 扫描我们自己的控制层类所在的包或者父包-->
<context:component-scan base-package="com.controller,com.handler"/>
<!--拦截器-->
<mvc:annotation-driven/>
<!--释放静态资源-->
<mvc:default-servlet-handler/>
<!--视图解析器 前补/ 后补 .jsp-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>5.处理静态资源
在配置文件中加上
<!--释放静态资源-->
<mvc:default-servlet-handler/>
版权声明:本文为m0_58984702原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。