Model(模型)
View(视图)
Controller(控制器)
接收用户请求,委托给模型进行处理(状态改变),处理完毕后,把返回的模型数据,传递给视图,由视图负责展示,也就是说,控制器,做了个调度员的工作;
最典型的 MVC:JSP + servlet + javabean 的模式;

在 web 早期的开发中,通常采用的都是 Model1;
Model1 中,主要分为两层,视图层和模型层;

Model1 优点:架构简单,比较适合小型项目开发;
Model1 缺点:JSP 职责不单一,职责过重,不便于维护;
Model 2 :把一个项目分成三部分,包括视图、控制、模型;

职责分析
小结:
运行环境:
新建一个 Maven 工程当做父工程,并添加相关依赖:
<!--Servlet 依赖-->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
</dependency>
<!--JSP 依赖-->
<dependency>
<groupId>com.guicedee.services</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<version>62</version>
</dependency>
<!--jstl 表达式依赖-->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>2.0.0</version>
</dependency>
<!--标签库依赖-->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.5</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!--spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.17</version>
</dependency>
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取前端参数
String method = req.getParameter("method");
if (method.equals("add")) {
req.getSession().setAttribute("msg", "执行了 add 方法!");
}
if (method.equals("delete")) {
req.getSession().setAttribute("msg", "执行了 delete 方法!");
}
// 调用业务层
// 视图转发或重定向
req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
WEB-INF 目录下新建 jsp 目录,新建 hello.jsp;<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
web.xml 或通过注解方式 @WebServlet("/hello");<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.study.springmvc.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
localhost:8080/映射地址/hello?method=add;localhost:8080/映射地址/hello?method=delete;MVC 框架要做哪些事情
.jsp/html 表示层数据;小结:
Spring MVC 的特点
调度 Servlet 设计;小结:
Spring 的 web 框架围绕 DispatcherServlet 设计;
DispatcherServlet 作用:将请求分发到不同的处理器;
从 Spring 2.5 开始,Java 5 及以上版本,可以采用基于注解的controller 声明方式;
Spring MVC 框架像许多其他 MVC 框架一样,以请求为驱动,围绕一个中心 Servlet,分派请求及提供其他功能,DispatcherServlet 是一个实际的 Servlet(它继承自 HttpServlet 基类);

Spring MVC 原理
当发起请求时,被前置的控制器拦截到请求,根据请求参数,生成代理请求,找到请求对应的实际控制器;
控制器处理请求,创建数据模型,访问数据库,将模型响应给中心控制器,控制器使用模型与视图,渲染视图结果,将结果返回给中心控制器,再将结果返回给请求者;

Spring MVC 的流程图:

简要分析执行流程
http://localhost:8080/SpringMVC/hello;http://localhost:8080:服务器域名;localhost:8080 上的 SpringMVC 站点的 hello 控制器;<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!--spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.17</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!--jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!--jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
web.xml,注册 DispatcherServlet:<?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">
<!--1.注册DispatcherServlet:SpringMVC的核心,请求分发器或前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--关联一个springmvc的配置文件:【servlet-name】-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--启动级别-1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--/ 只匹配所有的请求,不包含jsp页面;-->
<!--/* 匹配所有的请求+jsp的页面;-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
<!--添加处理映射器:根据请求查找对应的处理器(如:/hello请求,对应 bean id="/hello")-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--添加处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--
视图解析器:DispatcherServlet传递过来的ModelAndView
1.获取ModelAndView的数据
2.解析ModelAndView的视图名字
3.拼接视图名字,打到对应的视图 /WEB-INF/jsp/hello.jsp
4.将获取的数据,渲染到视图上
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀,注意结尾后面的/-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
创建业务层:Controller
要么实现 Controller 接口;
要么增加注解;
需要返回一个 ModelAndView,装数据,封视图;
// 注意:导入Controller接口
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
// ModelAndView 模型和视图
ModelAndView mv = new ModelAndView();
// 封装对象,放在ModelAndView中, Model
mv.addObject("msg", "HelloSpringMVC!");
// 封装要跳转的视图,放在ModelAndView中
// 通过springmvc-servlet.xml文件,自动拼接成 /WEB-INF/jsp/hello.jsp
mv.setViewName("hello");
return mv;
}
}
<!--Handler-->
<bean id="/hello" class="com.study.controller.HelloController"/>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello</title>
</head>
<body>
${msg}
</body>
</html>
配置 Tomcat,启动测试:

可能遇到的问题:
访问出现 404:

排查步骤:
查看控制台输出,看一下是不是缺少了什么 jar 包;
如果 jar 包存在,显示无法输出,在 IDEA 的项目发布中,添加 lib 依赖:

重启 Tomcat,即可解决;
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
pom.xml 文件引入相关的依赖:参照上面配置版
web.xml:
1;/(不要用 /* ,会报 404);<?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">
<!--1.注册servlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 启动顺序,数字越小,启动越早 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--所有请求都会被springmvc拦截,不包含.jsp -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc-servlet.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"
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">
<!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
<context:component-scan base-package="com.study.controller"/>
<!-- 让Spring MVC不处理静态资源 -->
<mvc:default-servlet-handler/>
<!--
支持mvc注解驱动
在spring中一般采用@RequestMapping注解来完成映射关系
要想使@RequestMapping注解生效
必须向上下文中注册DefaultAnnotationHandlerMapping
和一个AnnotationMethodHandlerAdapter实例
这两个实例分别在类级别和方法级别处理。
而annotation-driven配置,会自动完成上述两个实例的注入。
-->
<mvc:annotation-driven/>
<!--视图解析器:DispatcherServlet给他的ModelAndView-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
/WEB-INF/ 目录下,这样可以保证视图安全,因为这个目录下的文件,客户端不能直接访问;/HelloController/hello ;hello,加上配置文件中的前后缀,变成 WEB-INF/jsp/hello.jsp ;@Controller
@RequestMapping("/HelloController")
public class HelloController {
// 真实访问地址:项目名/HelloController/hello
@RequestMapping("/hello")
public String hello(Model model) {
// 向模型中添加属性msg与值,可以在JSP页面中取出并渲染
model.addAttribute("msg", "Hello SpringMVC Annotation");
// WEB-INF/jsp/hello.jsp
return "hello";
}
}
WEB-INF/jsp 目录中,创建 hello.jsp;<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello</title>
</head>
<body>
${msg}
</body>
</html>
配置 Tomcat 运行:

小结:
web.xml , 注册 DispatcherServlet;@RequestMapping;org.springframework.web.servlet.mvc 包下,接口中只有一个方法:// 实现该接口的类,获得控制器功能
public interface Controller {
// 处理请求且返回一个模型与视图对象
ModelAndView handleRequest(HttpServletRequest var1, HttpServletResponse var2) throws Exception;
}
测试
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
// 定义控制器
// 注意:不要导错包,实现Controller接口,重写方法;
public class ControllerTest01 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 返回一个模型视图对象
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "ControllerTest01");
mv.setViewName("test");
return mv;
}
}
<bean name="/t1" class="com.study.controller.ControllerTest01"/>
WEB-INF/jsp 目录下编写,对应视图解析器:<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>test</title>
</head>
<body>
${msg}
</body>
</html>
配置 Tomcat,运行测试:

小结:
@Controller 注解类型,用于声明 Spring 类的实例,是一个控制器;
<!--设置扫描包-->
<context:component-scan base-package="com.study.controller"/>
// @Controller注解的类,会自动添加到Spring上下文中
// 该类中的所有方法,如果返回值是String,且有对应的页面跳,就会被视图解析器解析
@Controller
public class ControllerTest02 {
// 映射访问路径
@RequestMapping("/t2")
public String test(Model model) {
// Spring MVC会自动实例化一个Model对象,用于向视图中传值
model.addAttribute("msg", "ControllerTest02");
// 返回视图位置 WEB-INF/jsp/test.jsp
return "test";
}
}
运行测试:

两个请求都指向同一个视图,但是页面的结果不同,从这里可以看出,视图是被复用的,而控制器与视图之间,是 弱偶合关系;
测试
@Controller
public class ControllerTest03 {
@RequestMapping("/t3")
public String test(Model model) {
model.addAttribute("msg", "ControllerTest03");
return "test";
}
}
运行测试:
http://localhost:8080/项目名/t3;
同时注解类与方法:不推荐
@Controller
// 不推荐
@RequestMapping("/admin")
public class ControllerTest03 {
@RequestMapping("/t3")
public String test(Model model) {
model.addAttribute("msg", "ControllerTest03");
return "test";
}
}
http://localhost:8080/项目名/admin/t3;
@Controller
public class ControllerTest03 {
// 推荐:在方法上加完整路径
@RequestMapping("/admin/t3")
public String test(Model model) {
model.addAttribute("msg", "ControllerTest03");
// 视图位置 WEB-INF/jsp/admin/test.jsp
return "admin/test";
}
}
概念:Restful 就是一个资源定位,及资源操作的风格;
功能:
传统方式操作资源:通过不同的参数,实现不同的效果,方法单一(post 和 get);
http://localhost/item/queryItem.action?id=1 查询,GET;http://localhost/item/saveItem.action 新增,POST;http://localhost/item/updateItem.action 更新,POST;http://localhost/item/deleteItem.action?id=1 删除, GET 或 POST;使用 RESTful 操作资源:可以通过 不同的请求方式,实现不同的效果,请求地址一样,但功能可以不同;
http://localhost/item/1 查询,GET;http://localhost/item 新增,POST;http://localhost/item 更新,PUT;http://localhost/item/1 删除,DELETE;测试
@Controller
public class RestFulController {
}
@PathVariable 注解,让方法参数的值,对应绑定到一个 URL 模板变量上:@Controller
public class RestFulController {
// 传统方式:@RequestMapping("/add"),访问:http://localhost:8080/项目名/add?a=1&b=2
// RestFul:http://localhost:8080/项目名/a/b
// 映射访问路径
@RequestMapping("/add/{a}/{b}")
public String test(@PathVariable int a, @PathVariable int b, Model model) {
int result = a + b;
// Spring MVC会自动实例化一个Model对象,用于向视图中传值
model.addAttribute("msg", "结果:" + result);
// 返回视图位置
return "test";
}
}
运行测试:

使用路径变量的好处:
路径简洁;
获得参数更加方便,框架会自动进行类型转换;
通过路径变量的类型,可以约束访问参数,如果类型不一样,则访问不到对应的请求方法,如:访问路径是 /add/1/a,则路径与方法不匹配,而不会是参数转换失败:

修改对应的参数类型:
@Controller
public class RestFulController {
@RequestMapping("/add/{a}/{b}")
public String test(@PathVariable int a, @PathVariable String b, Model model) {
String result = a + b;
model.addAttribute("msg", "结果:" + result);
return "test";
}
}
再次测试:

使用 method 属性,指定请求类型:
// 映射访问路径,必须是POST请求
@RequestMapping(value = "/hello", method = {RequestMethod.POST})
public String test2(Model model) {
model.addAttribute("msg", "hello!");
return "test";
}
使用浏览器地址栏,进行访问,默认是 Get 请求,报错 405:

将 POST 修改为 GET:
// 映射访问路径,必须是GET请求
@RequestMapping(value = "/hello", method = {RequestMethod.GET})
public String test2(Model model) {
model.addAttribute("msg", "hello!");
return "test";
}
运行测试:正常

POST 提交方式测试:
// 映射访问路径,必须是POST请求
// @RequestMapping(value = "/add/{a}/{b}", method = {RequestMethod.POST})
// @RequestMapping(path = "/add/{a}/{b}", method = {RequestMethod.POST})
// 简化方式
@PostMapping("/add/{a}/{b}")
public String test3(@PathVariable int a, @PathVariable String b, Model model) {
String result = a + b;
model.addAttribute("msg", "POST 结果:" + result);
return "test";
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/add/1/a" method="post">
<input type="submit">
</form>
</body>
</html>
访问测试:

小结:
Spring MVC 的 @RequestMapping 注解,能够处理 HTTP 请求的方法, 如:GET、PUT、POST、DELETE、PATCH;
所有的地址栏请求,默认都会是 HTTP GET 类型的;
方法级别的注解变体(推荐使用):组合注解
@GetMapping:GET;@PostMapping:POST;@PutMapping:PUT;@DeleteMapping:DELETE;@PatchMapping:PATCH;@GetMapping:等同于 @RequestMapping(method = {RequestMethod.GET});
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
public class ControllerTest01 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 返回一个模型视图对象
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "ControllerTest01");
mv.setViewName("test");
return mv;
}
}
@Controller
public class ModelServlet {
@RequestMapping("/result/s1")
public void test1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.getWriter().println("s1");
}
@RequestMapping("/result/s2")
public String test2(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(req.getSession().getId());
return "test";
}
@RequestMapping("/result/s3")
public void test3(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// 重定向
resp.sendRedirect(req.getContextPath() + "/index.jsp");
}
@RequestMapping("/result/s4")
public void test4(HttpServletRequest req, HttpServletResponse resp) throws Exception {
req.setAttribute("msg","/result/s4");
// 转发
req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp);
}
}
方式 1:不配置视图解析器
@Controller
public class ModelSpringMVC {
@RequestMapping("/result/m1")
public String test1(Model model) {
model.addAttribute("msg", "ModelSpringMVC1");
// 转发方式1:未配置视图解析器,需要完整的路径名
return "/WEB-INF/jsp/test.jsp";
}
@RequestMapping("/result/m2")
public String test2(Model model) {
model.addAttribute("msg", "ModelSpringMVC2");
// 转发方式2:
return "forward:/WEB-INF/jsp/test.jsp";
}
@RequestMapping("/result/m3")
public String test3(Model model) {
model.addAttribute("msg", "ModelSpringMVC3");
// 重定向:
return "redirect:/index.jsp";
}
}
方式 2:配置视图解析器
@Controller
public class ModelSpringMVC02 {
@RequestMapping("/result/t1")
public String test01() {
// 配置视图解析器,转发
return "test";
}
@RequestMapping("/result/t2")
public String test02() {
// 重定向:无需配置视图解析器
return "redirect:/index.jsp";
// 重定向到另一个请求
// return "redirect:/result/t1";
}
}
- 提交的域名称,和处理方法的参数名一致
http://localhost:8080/s4/user/t1?name=t1;@GetMapping("user/t1")
public String test1(String name, Model model) {
// 1.接收前端参数
System.out.println("接收到的前端参数:" + name);
// 2.将返回的结果,返回给前端
model.addAttribute("msg", name);
// 3.视图跳转
return "test";
}
运行测试:

- 提交的域名称,和处理方法的参数名不一致
http://localhost:8080/s4/user/t2?username=t2;// @RequestParam("username"):username提交的域的名称
@GetMapping("/user/t2")
// 需要从前端接收的参数,建议都加上@RequestParam 注解
public String test2(@RequestParam("username") String name, Model model) {
model.addAttribute("msg", name);
return "test";
}
运行测试:

@RequestParam 注解,可以过滤无效的请求:
@RequestParam 注解;
- 提交的请求是一个对象
提交的表单域和对象的属性名一致时,可以使用对象做为参数;
创建实体类:
public class User {
private int id;
private String name;
private int age;
// 有参、无参构造
// get、set
// toString()
}
http://localhost:8080/s4/user/t3?id=1&name=t3&age=20 ;@GetMapping("/user/t3")
public String test3(User user, Model model) {
model.addAttribute("msg", user);
return "test";
}
运行测试:

小结:
public class ControllerTest01 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 返回一个模型视图对象
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "ControllerTest01");
mv.setViewName("admin/test");
return mv;
}
}
@GetMapping("/user/t4")
public String test4(@RequestParam("username") String name, ModelMap modelMap) {
// 封装要显示到视图中的数据
// 相当于req.setAttribute("msg",name);
modelMap.addAttribute("msg", name);
return "test";
}
@GetMapping("/user/t2")
// 需要从前端接收的参数,建议都加上@RequestParam 注解
public String test2(@RequestParam("username") String name, Model model) {
model.addAttribute("msg", name);
return "test";
}
测试
<form action="user/t5" method="post">
<input type="text" name="name">
<input type="submit">
</form>
@PostMapping("/user/t5")
public String test5(@RequestParam("name") String name, Model model) {
// 1.接收前端参数
System.out.println("接收到的前端参数:" + name);
// 2.将返回的结果,返回给前端
model.addAttribute("msg", name);
// 3.视图跳转
return "test";
}
运行,输入中文测试:乱码

查看控制台:乱码

解决方法:
web.xml 中配置;<!--SpringMVC 乱码过滤-->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<!--注意:使用/* 不能用/-->
<url-pattern>/*</url-pattern>
</filter-mapping>
server.xml:设置编码<Connector URIEncoding="utf-8" port="8080" protocol="HTTP/1.1"
connectionTimeout="20000" redirectPort="8443"/>
"" 包裹,使用冒号 : 分隔,然后紧接着是值:{"name":"ZhangSan"}
{"age":"18"}
{"sex":"男"}
JSON 和 JavaScript 对象的关系
// 这是一个对象:注意键名也是可以使用引号包裹的
var obj = {a:'Hello',b:'World'};
// 这是一个 JSON 字符串,本质是一个字符串
var json = '{"a":"Hello","b":"World"}';
JSON 和 JavaScript 对象互转:
JSON.parse():var obj = JSON.parse('{"a":"Hello","b":"World"}');
// 结果是 {a:'Hello',b:'World'}
JSON.stringify():var json = JSON.stringify({a:'Hello',b:'World'});
// 结果是 '{"a":"Hello","b":"World"}'
测试:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jsonTest</title>
<!--注意:script不能单标签闭合-->
<script type="text/javascript">
// 编写一个js的对象
let user = {
id: "1",
name: "张三",
age: 20
};
console.log(user)
// 将js对象转换成json字符串
let str = JSON.stringify(user);
console.log(str)
// 将json字符串转换为js对象
let obj = JSON.parse(str);
console.log(obj.id, obj.name, obj.age);
</script>
</head>
<body>
</body>
</html>
在 IDEA 中使用浏览器打开,查看控制台输出:

Jackson 测试
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2.2</version>
</dependency>
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">
<!--1. 注册servlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 启动顺序,数字越小,启动越早 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--所有请求都会被springmvc拦截,不包含.jsp -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--2. SpringMVC 乱码过滤-->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<!--注意:使用/* 不能用/-->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
springmvc-servlet.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.study.controller"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
// 需要导入lombok
@Data
// 有参构造
@AllArgsConstructor
// 无参构造
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
}
@ResponseBody:不去执行视图解析器,直接返回字符串,需要配合 @Controller 使用;@Controller
public class UserController {
@RequestMapping("/j1")
// @ResponseBody:不去执行视图解析器,直接返回字符串
@ResponseBody
public String json1() throws JsonProcessingException {
// 创建一个jackson的对象映射器,用来解析数据
ObjectMapper mapper = new ObjectMapper();
// 创建一个对象
User user = new User("张三", 20, "男");
// 将User对象解析成为json格式
String json = mapper.writeValueAsString(user);
// 使用@ResponseBody注解后,将str转成JSON格式返回
return json;
}
}
配置 Tomcat,运行测试:

解决 JSON 乱码(了解)
@RequestMaping 的 produces 属性,设置编码格式为 utf-8,以及返回类型:// produces:指定响应体返回类型和编码
@RequestMapping(value = "/j1",produces = "application/json;charset=utf-8")
再次运行测试:

<!--JSON乱码统一解决(固定代码)-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
@RestController:在类上添加,所有的方法都只返回 JSON 字符串,不用在每个方法中添加 @ResponseBody;@RestController;// @Controller:执行视图解析器
// @RestController:直接返回字符串
@RestController
public class UserController {
@RequestMapping("/j1")
// @ResponseBody:需要配合@Controller一起使用
public String json1() throws JsonProcessingException {
// 创建一个jackson的对象映射器,用来解析数据
ObjectMapper mapper = new ObjectMapper();
// 创建一个对象
User user = new User("张三", 20, "男");
// 将User对象解析成为json格式
String json = mapper.writeValueAsString(user);
// 使用@ResponseBody注解后,将str转成JSON格式返回
return json;
}
}
@RequestMapping("/j2")
public String json2() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
User user1 = new User("test01", 20, "男");
User user2 = new User("test02", 20, "男");
User user3 = new User("test03", 20, "男");
User user4 = new User("test04", 20, "男");
List<User> list = new ArrayList<>();
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
String json = mapper.writeValueAsString(list);
return json;
// return new ObjectMapper().writeValueAsString(list);
}
运行测试:

@RequestMapping("/j3")
public String json3() throws JsonProcessingException {
// 创建时间一个对象,java.util.Date
Date date = new Date();
// 返回json格式
return new ObjectMapper().writeValueAsString(date);
}

解决方案:
@RequestMapping("/j4")
public String json4() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
// 不使用时间戳的方式
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
// 自定义日期格式对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 指定日期格式
mapper.setDateFormat(sdf);
// 创建时间一个对象,java.util.Date
Date date = new Date();
String json = mapper.writeValueAsString(date);
// 返回json格式
return json;
}
运行测试:

public class JsonUtils {
// 方法重载
public static String getJson(Object object) {
return getJson(object, "yyyy-MM-dd HH:mm:ss");
}
public static String getJson(Object object, String dateFormat) {
// 创建一个jackson的对象映射器,用来解析数据
ObjectMapper mapper = new ObjectMapper();
// 不使用时间戳的方式
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
// 自定义日期格式对象
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
// 指定日期格式
mapper.setDateFormat(sdf);
try {
// 返回json字符串
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}
@RequestMapping("/j5")
public String json5(){
Date date = new Date();
String json = JsonUtils.getJson(date);
return json;
}
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.80</version>
</dependency>
Fastjson 三个主要的类:
JSONObject:代表 JSON 对象;
键:值 对的个数,和判断是否为空;JSONArray:代表 JSON 对象数组;
JSON:代表 JSONObject 和 JSONArray 的转化;
Fastjson 测试
// FastJson
@RequestMapping("/j6")
public String json6() {
User user1 = new User("FastJson01", 20, "男");
User user2 = new User("FastJson02", 20, "男");
User user3 = new User("FastJson03", 20, "男");
User user4 = new User("FastJson04", 20, "男");
List<User> list = new ArrayList<>();
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
System.out.println("===== Java对象 转 JSON字符串 =====");
String str1 = JSON.toJSONString(list);
System.out.println("JSON.toJSONString(list)==>" + str1);
String str2 = JSON.toJSONString(user1);
System.out.println("JSON.toJSONString(user1)==>" + str2);
System.out.println("\n===== JSON字符串 转 Java对象 =====");
User jp_user1 = JSON.parseObject(str2, User.class);
System.out.println("JSON.parseObject(str2,User.class)==>" + jp_user1);
System.out.println("\n===== Java对象 转 JSON对象 =====");
JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);
System.out.println("(JSONObject)JSON.toJSON(user2) == > " +
jsonObject1.getString("name"));
System.out.println("\n===== JSON对象 转 Java对象 =====");
User to_java_user = JSON.toJavaObject(jsonObject1, User.class);
System.out.println("JSON.toJavaObject(jsonObject1,User.class) ==>"
+ to_java_user);
return "FastJson 测试";
}
JDK 17;IDEA 2021.2;MySQL 8.0.28;Tomcat 9.0.60;Maven 3.8.4;CREATE DATABASE `ssmbuild`;
USE `ssmbuild`;
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
`bookID` INT NOT NULL AUTO_INCREMENT COMMENT '书id',
`bookName` VARCHAR(100) NOT NULL COMMENT '书名',
`bookCounts` INT NOT NULL COMMENT '数量',
`detail` VARCHAR(200) NOT NULL COMMENT '描述',
KEY `bookID` (`bookID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4;
INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!--数据库连接池 c3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!--Servlet-JSP-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.18</version>
</dependency>
<!--Spring Java 注解:JDK11 以上需要-->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<!--AOP 织入-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.8</version>
</dependency>
</dependencies>
<!--静态资源导出-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
database.properties
# mysql8 驱动
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8&useSSL=true
jdbc.username=用户名
jdbc.password=密码
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--1. 标准日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--2. 设置别名:扫描包的方式-->
<typeAliases>
<package name="com.study.pojo"/>
</typeAliases>
<!--3. 注册 Mapper.xml-->
<mappers>
<!--1. 接口类方式-->
<mapper class="com.study.dao.BookMapper"/>
<!--2. 资源文件方式-->
<!--<mapper resource="com/study/dao/BookMapper.xml"/>-->
<!--3. 扫描包方式-->
<!--<package name="com.study.dao"/>-->
</mappers>
</configuration>
Books
@Data
// 有参构造
@AllArgsConstructor
// 无参构造
@NoArgsConstructor
public class Books {
private String detail;
private int bookCounts;
private String bookName;
private int bookID;
}
dao 层
BookMapper
public interface BookMapper {
// 增加一个Book
int addBook(Books books);
// 根据id删除一个Book
int deleteBookById(@Param("bookID") int id);
// 更新Book
int updateBook(Books books);
// 根据id查询,返回一个Book
Books queryBookById(@Param("bookID") int id);
// 查询全部Book,返回list集合
List<Books> queryAllBook();
}
BookMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--对应接口类:每一个mapper对应一个接口文件-->
<mapper namespace="com.study.dao.BookMapper">
<!--1. 增加一个Book-->
<insert id="addBook" parameterType="Books">
insert into `books` (`bookName`, `bookCounts`, `detail`)
values (#{bookName}, #{bookCounts}, #{detail});
</insert>
<!--2. 根据id删除一个Book-->
<delete id="deleteBookById" parameterType="int">
delete
from `books`
where `bookID` = #{bookID};
</delete>
<!--3. 更新Book-->
<update id="updateBook" parameterType="Books">
update `books`
set `bookName`=#{bookName},
`bookCounts`=#{bookCounts},
`detail`=#{detail}
where `bookID` = #{bookID};
</update>
<!--4. 根据id查询,返回一个Book-->
<select id="queryBookById" resultType="Books">
select *
from `books`
where `bookID` = #{bookID};
</select>
<!--5. 查询全部Book-->
<select id="queryAllBook" resultType="Books">
select *
from `books`;
</select>
</mapper>
service 层
BookService
public interface BookService {
// 增加一个Book
int addBook(Books books);
// 根据id删除一个Book
int deleteBookById(int id);
// 更新Book
int updateBook(Books books);
// 根据id查询,返回一个Book
Books queryBookById(int id);
// 查询全部Book,返回list集合
List<Books> queryAllBook();
}
BookServiceImpl
public class BookServiceImpl implements BookService {
// service层调用dao层:组合dao
private BookMapper bookMapper;
// 设置set接口,方便Spring管理
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
@Override
public int addBook(Books books) {
// 调用dao层的方法
return bookMapper.addBook(books);
}
@Override
public int deleteBookById(int id) {
return bookMapper.deleteBookById(id);
}
@Override
public int updateBook(Books books) {
return bookMapper.updateBook(books);
}
@Override
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
@Override
public List<Books> queryAllBook() {
return bookMapper.queryAllBook();
}
}
mybatis-config.xml 中注册 Mapper.xml;Spring 整合 MyBatis 层
c3p0;spring-dao.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">
<!--配置整合mybatis-->
<!--1. 关联数据库文件:通过spring读取-->
<context:property-placeholder location="classpath:database.properties"/>
<!--2. 数据库连接池
dbcp:半自动化操作,不能自动连接
c3p0:自动化操作(自动的加载配置文件,并且设置到对象里面)
druid、hikari
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置c3p0连接池属性 -->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!--关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!--获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!--当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!--3. 配置SqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据源-->
<property name="dataSource" ref="dataSource"/>
<!--绑定Mybatis配置文件(spring整合Mybatis) 注意 value后面加classpath:-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--4. 配置扫描Dao接口包,动态实现Dao接口注入到spring容器中(不需要创建dao接口的实现类)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--要扫描Dao接口包-->
<property name="basePackage" value="com.study.dao"/>
</bean>
</beans>

Spring 整合 service 层
spring-service.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--1. 扫描service下的包-->
<context:component-scan base-package="com.study.service"/>
<!--2. 将所有的service业务类,注入到spring:通过配置或注解实现-->
<bean id="BookServiceImpl" class="com.study.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>
<!--3. 配置声明式事务 JDBC 事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--4. AOP 配置事务切入,需导入AOP织入包及导入头文件-->
<!--结合AOP实现事务的织入-->
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--给哪些方法配置事务-->
<!--配置事务的传播特性,propagation:传播-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置事务切入-->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.study.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>
把模块添加 web 框架支持:

配置 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">
<!--1. DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--注意:这里加载的是总的配置文件,总配置文件中,通过import引入其它配置文件-->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!--启动级别-1-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--所有请求都会被springmvc拦截,不包含.jsp -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--2. 乱码过滤-->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<!--注意:使用/* 不能用/-->
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--3. 设置Session过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--配置SpringMVC-->
<!-- 1. 开启SpringMVC注解驱动,注意导入mvc的头文件-->
<mvc:annotation-driven/>
<!--2. 静态资源过滤-->
<mvc:default-servlet-handler/>
<!--3. 扫描包:Controller-->
<context:component-scan base-package="com.study.controller"/>
<!--4. 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
创建对应的 jsp 目录:

整合 Spring 总配置文件:applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="springmvc-servlet.xml"/>
</beans>
BookController
@Controller
@RequestMapping("/book")
public class BookController {
// controller调service层
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
// 1. 查询全部书籍,并返回到书籍展示页面
@RequestMapping("/allBook")
public String list(Model model) {
List<Books> list = bookService.queryAllBook();
model.addAttribute("list", list);
return "allBook";
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
<style>
a {
text-decoration: none;
color: black;
font-size: 18px;
}
h3 {
width: 180px;
height: 38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background: deepskyblue;
border-radius: 4px;
}
</style>
</head>
<body>
<h3>
<a href="${pageContext.request.contextPath}/book/allBook">进入书籍页面</a>
</h3>
</body>
</html>
allbook.jsp:<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>书籍展示</title>
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>书籍列表 - 显示所有书籍</small>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary"
href="${pageContext.request.contextPath}/book/toAddBook">新增</a>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>书籍编号</th>
<th>书籍名字</th>
<th>书籍数量</th>
<th>书籍详情</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${list}">
<tr>
<td>${book.bookID}</td>
<td>${book.bookName}</td>
<td>${book.bookCounts}</td>
<td>${book.detail}</td>
<td>
<a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.bookID}">更改</a>
|
<%--RestFul风格--%>
<a href="${pageContext.request.contextPath}/book/delBook/${book.bookID}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
配置 Tomcat,进行测试:
注意:测试前,需要在 IDEA 的项目发布中,添加 lib 依赖,否则报错;
首页:

书籍列表页:

添加其它方法,配置对应的前端页面:
@Controller
@RequestMapping("/book")
public class BookController {
// controller调service层
// 自动装配Bean
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
// 1. 查询全部书籍,并返回到书籍展示页面
@RequestMapping("/allBook")
public String list(Model model) {
List<Books> list = bookService.queryAllBook();
model.addAttribute("list", list);
return "allBook";
}
// 2. 跳转到添加书籍
@RequestMapping("/toAddBook")
public String toAddBook() {
return "addBook";
}
// 3. 添加书籍
@RequestMapping("/addBook")
public String addBook(Books books) {
bookService.addBook(books);
// 重定向
return "redirect:/book/allBook";
}
// 4. 跳转到修改书籍
@RequestMapping("/toUpdateBook")
public String toUpdateBook(int id, Model model) {
Books books = bookService.queryBookById(id);
// System.out.println(books);
model.addAttribute("books", books);
return "updateBook";
}
// 5. 修改书籍
@RequestMapping("/updateBook")
public String updateBook(Books books) {
int i = bookService.updateBook(books);
if (i > 0) {
System.out.println("修改成功:" + books);
}
return "redirect:/book/allBook";
}
// 6. 删除书籍
// 设置RestFul风格
@RequestMapping("/delBook/{bookID}")
public String delBook(@PathVariable("bookID") int id) {
bookService.deleteBookById(id);
return "redirect:/book/allBook";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>新增书籍</title>
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>新增书籍</small>
</h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/book/addBook" method="post">
<div class="form-group">
<label for="name">书籍名称:</label>
<input type="text" class="form-control" name="bookName" id="name" placeholder="书籍名称" required>
</div>
<div class="form-group">
<label for="counts">书籍数量:</label>
<input type="text" class="form-control" name="bookCounts" id="counts" placeholder="书籍数量" required>
</div>
<div class="form-group">
<label for="detail">书籍详情:</label>
<input type="text" class="form-control" name="detail" id="detail" placeholder="书籍详情" required>
</div>
<button type="submit" class="btn btn-primary">添加</button>
</form>
</div>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改书籍</title>
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>修改书籍</small>
</h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/book/updateBook" method="post">
<%--设置隐藏文本,存储bookID的值--%>
<input type="hidden" name="bookID" value="${books.bookID}">
<div class="form-group">
<label for="name">书籍名称:</label>
<input type="text" class="form-control" name="bookName" id="name" value="${books.bookName}" required>
</div>
<div class="form-group">
<label for="counts">书籍数量:</label>
<input type="text" class="form-control" name="bookCounts" id="counts" value="${books.bookCounts}" required>
</div>
<div class="form-group">
<label for="detail">书籍详情:</label>
<input type="text" class="form-control" name="detail" id="detail" value="${books.detail}" required>
</div>
<button type="submit" class="btn btn-primary">修改</button>
</form>
</div>
</body>
</html>
项目结构图

allBook.jsp 增加输入框和查询按钮:<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>书籍展示</title>
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>书籍列表 - 显示所有书籍</small>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary"
href="${pageContext.request.contextPath}/book/toAddBook">新增</a>
</div>
<%--添加书籍查询功能--%>
<div class="col-md-8 column">
<form class="form-inline" action="${pageContext.request.contextPath}/book/queryBook" method="post"
style="float: right">
<input type="text" name="queryBookName" class="form-control" placeholder="输入查询书名" required>
<input type="submit" value="查询" class="btn btn-primary">
</form>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>书籍编号</th>
<th>书籍名字</th>
<th>书籍数量</th>
<th>书籍详情</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${list}">
<tr>
<td>${book.bookID}</td>
<td>${book.bookName}</td>
<td>${book.bookCounts}</td>
<td>${book.detail}</td>
<td>
<a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.bookID}">更改</a>
|
<%--RestFul风格--%>
<a href="${pageContext.request.contextPath}/book/delBook/${book.bookID}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
完整页面:

在 BookController 中,增加查询书籍的方法:
// 7. 按书籍名称查询
@RequestMapping("/queryBook")
public String queryBook(String queryBookName, Model model) {
System.out.println("要查询的书籍:" + queryBookName);
// 业务代码
return "allBook";
}
// 根据书名查询,返回一个Book
Books queryBookByName(String bookName);
Mapper.xml,添加对应的查询 sql:<!--6. 根据书名查询,返回一个Book-->
<select id="queryBookByName" resultType="Books">
select *
from `books`
where `bookName` = #{bookName};
</select>
// 根据书名查询,返回一个Book
Books queryBookByName(String bookName);
@Override
public Books queryBookByName(String bookName) {
return bookMapper.queryBookByName(bookName);
}
// 7. 按书籍名称查询
@RequestMapping("/queryBook")
public String queryBook(String queryBookName, Model model) {
System.out.println("要查询的书籍:" + queryBookName);
Books books = bookService.queryBookByName(queryBookName);
List<Books> list = new ArrayList<>();
list.add(books);
model.addAttribute("list", list);
return "allBook";
}
测试查询功能:

简单优化:
在前端添加显示全部书籍的按钮:

后端增加查询结果的判断:
// 7. 按书籍名称查询
@RequestMapping("/queryBook")
public String queryBook(String queryBookName, Model model) {
System.out.println("要查询的书籍:" + queryBookName);
//如果查询的数据存在空格,则优化
Books books = bookService.queryBookByName(queryBookName.trim());
List<Books> list = new ArrayList<>();
list.add(books);
// 如果没有查出来书籍,则返回全部书籍列表
if (books == null) {
list = bookService.queryAllBook();
// 将错误信息返回前端
model.addAttribute("error", "未查到结果");
}
model.addAttribute("list", list);
return "allBook";
}
将错误信息展示在前台页面:

前端页面 allBook.jsp 完整代码:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>书籍展示</title>
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>书籍列表 - 显示所有书籍</small>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary"
href="${pageContext.request.contextPath}/book/toAddBook">新增</a>
<a class="btn btn-primary" href="${pageContext.request.contextPath}/book/allBook">显示全部书籍</a>
</div>
<%--添加书籍查询功能--%>
<div class="col-md-8 column">
<form class="form-inline" action="${pageContext.request.contextPath}/book/queryBook" method="post"
style="float: right">
<%--前端返回错误信息--%>
<span style="color:red;font-weight: bold">
${error}
</span>
<input type="text" name="queryBookName" class="form-control" placeholder="输入查询书名" required>
<input type="submit" value="查询" class="btn btn-primary">
</form>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>书籍编号</th>
<th>书籍名字</th>
<th>书籍数量</th>
<th>书籍详情</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${list}">
<tr>
<td>${book.bookID}</td>
<td>${book.bookName}</td>
<td>${book.bookCounts}</td>
<td>${book.detail}</td>
<td>
<a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.bookID}">更改</a>
|
<%--RestFul风格--%>
<a href="${pageContext.request.contextPath}/book/delBook/${book.bookID}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
运行测试:

1.postman介绍Postman一款非常流行的API调试工具。其实,开发人员用的更多。因为测试人员做接口测试会有更多选择,例如Jmeter、soapUI等。不过,对于开发过程中去调试接口,Postman确实足够的简单方便,而且功能强大。2.下载安装官网地址:https://www.postman.com/下载完成后双击安装吧,安装过程极其简单,无需任何操作3.使用教程这里以百度为例,工具使用简单,填写URL地址即可发送请求,在下方查看响应结果和响应状态码常用方法都有支持请求方法:getpostputdeleteGet、Post、Put与Delete的作用get:请求方法一般是用于数据查询,
Ⅰ软件测试基础一、软件测试基础理论1、软件测试的必要性所有的产品或者服务上线都需要测试2、测试的发展过程3、什么是软件测试找bug,发现缺陷4、测试的定义使用人工或自动的手段来运行或者测试某个系统的过程。目的在于检测它是否满足规定的需求。弄清预期结果和实际结果的差别。5、测试的目的以最小的人力、物力和时间找出软件中潜在的错误和缺陷6、测试的原则28原则:20%的主要功能要重点测(eg:支付宝的支付功能,其他功能都是次要的)80%的错误存在于20%的代码中7、测试标准8、测试的基本要求功能测试性能测试安全性测试兼容性测试易用性测试外观界面测试可靠性测试二、质量模型衡量一个优秀软件的维度①功能性功
ES一、简介1、ElasticStackES技术栈:ElasticSearch:存数据+搜索;QL;Kibana:Web可视化平台,分析。LogStash:日志收集,Log4j:产生日志;log.info(xxx)。。。。使用场景:metrics:指标监控…2、基本概念Index(索引)动词:保存(插入)名词:类似MySQL数据库,给数据Type(类型)已废弃,以前类似MySQL的表现在用索引对数据分类Document(文档)真正要保存的一个JSON数据{name:"tcx"}二、入门实战{"name":"DESKTOP-1TSVGKG","cluster_name":"elasticsear
(本文是网络的宏观的概念铺垫)目录计算机网络背景网络发展认识"协议"网络协议初识协议分层OSI七层模型TCP/IP五层(或四层)模型报头以太网碰撞路由器IP地址和MAC地址IP地址与MAC地址总结IP地址MAC地址计算机网络背景网络发展 是最开始先有的计算机,计算机后来因为多项技术的水平升高,逐渐的计算机变的小型化、高效化。后来因为计算机其本身的计算能力比较的快速:独立模式:计算机之间相互独立。 如:有三个人,每个人做的不同的事物,但是是需要协作的完成。 而这三个人所做的事是需要进行协作的,然而刚开始因为每一台计算机之间都是互相独立的。所以前面的人处理完了就需要将数据
文章目录概念索引相关操作创建索引更新副本查看索引删除索引索引的打开与关闭收缩索引索引别名查询索引别名文档相关操作新建文档查询文档更新文档删除文档映射相关操作查询文档映射创建静态映射创建索引并添加映射概念es中有三个概念要清楚,分别为索引、映射和文档(不用死记硬背,大概有个印象就可以)索引可理解为MySQL数据库;映射可理解为MySQL的表结构;文档可理解为MySQL表中的每行数据静态映射和动态映射上面已经介绍了,映射可理解为MySQL的表结构,在MySQL中,向表中插入数据是需要先创建表结构的;但在es中不必这样,可以直接插入文档,es可以根据插入的文档(数据),动态的创建映射(表结构),这就
目录1关系运算符2运算符优先级3关系表达式的书写代码实例:下面是面试中可能遇到的问题:1关系运算符C++中有6个关系运算符,用于比较两个值的大小关系,它们分别是:运算符描述==等于!=不等于小于>大于小于等于>=大于等于这些运算符返回一个布尔值,即true或false。例如,当x等于y时,x==y的结果为true,否则结果为false。2运算符优先级在C++中,关系运算符的优先级高于赋值运算符,但低于算术运算符。以下是关系运算符的优先级,从高到低排列:运算符描述>,,>=,关系运算符==,!=相等性运算符&&逻辑与`如果在表达式中有多个运算符,则按照优先级顺序依次进行运算。3关系表达式的书写在
一.计算机组成原理 这本书利用组合逻辑、同步时序逻辑电路设计的相关知识,从逻辑门开始逐步构建运算器、存储器、数据通路和控制器,最终集成为完整的CU原型系统,使读者从设计者的角度理解计算机部件构成及运行的基本原理,掌握软硬件协同的概念。 全书共9章,主要内容包括计算机系统概述、数据信息的表示、运算方法与运算器、存储系统、指令系统、中央处理器、指令流水线、总线系统、输入输出系统。1.计算机系统概述1.1计算机发展历程 计算机是一种能够按照事先存储的程序,自动、高速、准确地对相关信息进行处理的电子设备。1946年2月,世界上第一台电子数字计算机ENIAC(ElectronicNum
其实现在基础的资料和视频到处都是,就是看你有没有认真的去找学习资源了,去哪里学习都是要看你个人靠谱不靠谱,再好的教程和老师,你自己学习不进去也是白搭在正式选择之前,大可以在各种学习网站里面找找学习资源先自己学习一下为什么选择学软件测试?同学们理由众多!大概分这几类:①不受开发语言、行业产品变化限制;②入门更简单,对零基础、女生都友好;③软件项目都需要测试人员,职业生涯稳;④学习周期短,但薪资并不低。要想“肩扛”一条线?需掌握三大技能:技能1:掌握测试流程,熟悉系统框架能提前与开发人员一起制定测试计划,通过测试左移,推动代码评审,代码审计,单元测试,自动化冒烟测试,来保证研发阶段的质量。技能2:
Asitcurrentlystands,thisquestionisnotagoodfitforourQ&Aformat.Weexpectanswerstobesupportedbyfacts,references,orexpertise,butthisquestionwilllikelysolicitdebate,arguments,polling,orextendeddiscussion.Ifyoufeelthatthisquestioncanbeimprovedandpossiblyreopened,visitthehelpcenter提供指导。已关闭8年。什么是学习ruby语言
html基础标签学习网站:https://www.acwing.com/学习查询网站:https://developer.mozilla.org/zh-CN/!+tab自动出现框架1.1文档结构html的所有标签都为树形结构,例如:Web应用课第一讲html标签HTML元素表示一个HTML文档的根(顶级元素),所以它也被称为根元素。所有其他元素必须是此元素的后代。head标签HTML元素规定文档相关的配置信息(元数据),包括文档的标题,引用的文档样式和脚本等。body标签HTML元素表示文档的内容。document.body属性提供了可以轻松访问文档的body元素的脚本。title标签HTML