1.基本介绍
1.特点
2.SpringMVC跟SpringBoot的关系
2.快速入门
1.需求分析
2.图解
3.环境搭建
1.创建普通java工程
2.添加web框架支持
3.配置lib文件夹
1.导入jar包
2.Add as Library
3.以后自动添加
4.配置tomcat
1.配置上下文路径
2.配置热加载
5.src下创建Spring配置文件applicationContext-mvc.xml
6.配置中央控制器web.xml读取Spring配置文件
服务器启动则自动装载这个servlet,实例化servlet,调用init方法,读取spring配置文件
<?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-name> springDispatcherServlet</ servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class>
< init-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath:applicationContext-mvc.xml</ param-value>
</ init-param>
< load-on-startup> 1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name> springDispatcherServlet</ servlet-name>
< url-pattern> /</ url-pattern>
</ servlet-mapping>
</ web-app>
4.具体实现
文件目录
1.编写首页面login.jsp
<%--
Date: 2024/2/23
Time: 20:44
User: 孙显圣
Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="">
username:<input name="username" type="text">
password:<input name="password" type="password">
<input type="submit" value="登录">
</form>
</body>
</html>
2.编写控制器UserServlet.java
package com. sun. web ;
import org. springframework. stereotype. Controller ;
import org. springframework. web. bind. annotation. RequestMapping ;
@Controller
public class UserServlet {
@RequestMapping ( value = "/login" )
public String login ( ) {
System . out. println ( "login ok..." ) ;
return "login_ok" ;
}
}
3.编写视图解析器要跳转的页面login_ok.jsp
<%--
Date: 2024/2/23
Time: 20:54
User: 孙显圣
Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>ok!</h1>
</body>
</html>
4.配置视图解析器和容器扫描applicationContext-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: 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" >
< context: component-scan base-package = " com.sun.web" />
< bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" id = " internalResourceViewResolver" >
< property name = " prefix" value = " /WEB-INF/pages/" />
< property name = " suffix" value = " .jsp" />
</ bean>
</ beans>
5.配置login.jsp的action
5.结果展示
1.login.jsp
2.点击登录
6.注意事项和细节说明
1.控制器UserServlet指定url的时候可以省略value
2.关于web.xml文件中配置读取Spring的配置文件
3.SpringMVC执行流程
4.RequestMapping注解使用方式(不要背!)
1.@RequestMapping可修饰方法和类
文件目录
1.编写控制器UserHandler.java
package com. sun. web ;
import org. springframework. stereotype. Controller ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RequestMethod ;
@Controller
@RequestMapping ( value = "/user" )
public class UserHandler {
@RequestMapping ( value = "/buy" , method = RequestMethod . POST )
public String buy ( ) {
System . out. println ( "buy方法被调用" ) ;
return "success" ;
}
}
2.编写success.jsp
< % --
Date : 2024 / 2 / 24
Time : 12 : 40
User : 孙显圣
Version : 1.0
-- % >
< % @ page contentType= "text/html;charset=UTF-8" language= "java" % >
< html>
< head>
< title> Title < / title>
< / head>
< body>
< h1> success< / h1>
< / body>
< / html>
3.编写request.jsp
< % --
Date : 2024 / 2 / 24
Time : 12 : 43
User : 孙显圣
Version : 1.0
-- % >
< % @ page contentType= "text/html;charset=UTF-8" language= "java" % >
< html>
< head>
< title> Title < / title>
< / head>
< body>
< form action= "user/buy" method= "post" >
< input type= "submit" value= "提交" >
< / form>
< / body>
< / html>
4.结果展示
1.首页
2.点击提交
5.注意事项
1.如果指定了post请求,使用其他请求方式则会报错
2.如果没有指定请求方式则get或post都可以
2.@RequestMapping可指定params支持简单表达式
文件目录
1.基本介绍
2.请求必须包含某个参数
1.UserHandler.java添加方法
@RequestMapping ( value = "/find" , method = RequestMethod . POST , params = "bookid" )
public String search ( String bookid) {
System . out. println ( "bookid=" + bookid) ;
return "success" ;
}
2.编写request.jsp
<%--
Date: 2024/2/24
Time: 12:43
User: 孙显圣
Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="user/find" method="post">
输入bookid:<input name="bookid" type="text">
<input type="submit" value="提交">
</form>
</body>
</html>
3.结果展示
3.请求必须包含参数并且指定参数值
1.修改UserHandler.java
2.结果展示
4.需要有参数并且值不等于什么
1.修改UserHandler.java
2.结果展示
3.@RequestMapping支持Ant风格资源地址
1.基本介绍
2.匹配多层路径
1.修改UserHandler.java
@RequestMapping ( value = "/message/aa/**" )
public String im ( ) {
System . out. println ( "匹配成功" ) ;
return "success" ;
}
2.结果展示
4.@RequestMapping可配合@PathVariable映射URL绑定的占位符
1.基本介绍
2.通过路径变量获取参数
1.修改UserHandler.java
@RequestMapping ( value = "/reg/{username}/{password}" )
public String register ( @PathVariable ( "username" ) String name, @PathVariable ( "password" ) String password) {
System . out. println ( "username = " + name + " password = " + password) ;
return "success" ;
}
2.结果展示
5.@RequestMapping注意事项和细节
1.映射的URL不能重复
1.修改UserHandler.java
@RequestMapping ( "/l1" )
public String h1 ( ) {
return "success" ;
}
@RequestMapping ( "/l1" )
public String h2 ( ) {
return "success" ;
}
2.结果展示
2.请求方式简写
1.基本介绍
2.修改UserHandler.java
@GetMapping ( "/buy1" )
public String buy_ ( ) {
return "success" ;
}
3.结果展示
3.提交数据简写
1.基本介绍
2.修改UserHandler.java
@GetMapping ( "/hello" )
public String hello ( String email) {
System . out. println ( email) ;
return "success" ;
}
3.结果展示
5.课后练习
1.题目
2.第一题
3.第三题
1.修改UserHandler.java
@PostMapping ( "/computer" )
public String computer ( String brand, String price, String num) {
System . out. println ( "brand=" + brand + " price=" + price + " num=" + num) ;
return "success" ;
}
2.homework01.jsp
<%--
Date: 2024/2/24
Time: 15:21
User: 孙显圣
Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="user/computer" method="post">
品牌:<input name="brand" type="text"><br>
价格:<input name="price" type="text"><br>
数量:<input name="num" type="text"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
3.结果展示