1.Controller

控制器controller,通常通过接口定义或注解定义两种方法实现。

1.1实现Controller接口

  • 实现接口Controller定义控制器是较老的办法
  • 缺点是:一个控制器中只有一个方法,如果要多个方法则需要定义多个Controller;定义的方式比较麻烦;

1.2使用注解@Controller

内部模块的maven配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <parent>
  7. <artifactId>SpringMVC</artifactId>
  8. <groupId>org.example</groupId>
  9. <version>1.0-SNAPSHOT</version>
  10. </parent>
  11. <modelVersion>4.0.0</modelVersion>
  12. <artifactId>spring-annotation</artifactId>
  13. <build>
  14. <resources>
  15. <resource>
  16. <directory>src/main/java</directory>
  17. <includes>
  18. <include>**/*.properties</include>
  19. <include>**/*.xml</include>
  20. </includes>
  21. <filtering>false</filtering>
  22. </resource>
  23. <resource>
  24. <directory>src/main/resources</directory>
  25. <includes>
  26. <include>**/*.properties</include>
  27. <include>**/*.xml</include>
  28. </includes>
  29. <filtering>false</filtering>
  30. </resource>
  31. </resources>
  32. </build>
  33. </project>

外层模块的maven配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>org.example</groupId>
  8. <artifactId>SpringMVC</artifactId>
  9. <packaging>pom</packaging>
  10. <version>1.0-SNAPSHOT</version>
  11. <modules>
  12. <module>springmvc-01</module>
  13. <module>spring-annotation</module>
  14. </modules>
  15. <dependencies>
  16. <dependency>
  17. <groupId>junit</groupId>
  18. <artifactId>junit</artifactId>
  19. <version>4.12</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework</groupId>
  23. <artifactId>spring-webmvc</artifactId>
  24. <version>5.1.9.RELEASE</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>javax.servlet</groupId>
  28. <artifactId>servlet-api</artifactId>
  29. <version>2.5</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>javax.servlet.jsp</groupId>
  33. <artifactId>jsp-api</artifactId>
  34. <version>2.2</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>javax.servlet</groupId>
  38. <artifactId>jstl</artifactId>
  39. <version>1.2</version>
  40. </dependency>
  41. </dependencies>
  42. <build>
  43. <resources>
  44. <resource>
  45. <directory>src/main/java</directory>
  46. <includes>
  47. <include>**/*.properties</include>
  48. <include>**/*.xml</include>
  49. </includes>
  50. <filtering>false</filtering>
  51. </resource>
  52. <resource>
  53. <directory>src/main/resources</directory>
  54. <includes>
  55. <include>**/*.properties</include>
  56. <include>**/*.xml</include>
  57. </includes>
  58. <filtering>false</filtering>
  59. </resource>
  60. </resources>
  61. </build>
  62. </project>

web.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <!--1.注册servlet-->
  7. <servlet>
  8. <servlet-name>SpringMVC</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <!--通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
  11. <init-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:springmvc-servlet.xml</param-value>
  14. </init-param>
  15. <!-- 启动顺序,数字越小,启动越早 -->
  16. <load-on-startup>1</load-on-startup>
  17. </servlet>
  18. <!--所有请求都会被springmvc拦截 -->
  19. <servlet-mapping>
  20. <servlet-name>SpringMVC</servlet-name>
  21. <url-pattern>/</url-pattern>
  22. </servlet-mapping>
  23. </web-app>

springmvc-servlet.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. https://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  12. <!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理-->
  13. <context:component-scan base-package="com.yuan.controller"/>
  14. <mvc:default-servlet-handler/>
  15. <mvc:annotation-driven/>
  16. <!--视图解析器:DispatcherServlet给他的ModelAndView-->
  17. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  18. id="internalResourceViewResolver">
  19. <!--前缀-->
  20. <property name="prefix" value="/WEB-INF/jsp/"/>
  21. <!--后缀-->
  22. <property name="suffix" value=".jsp"/>
  23. </bean>
  24. </beans>

com.yuan.controller包下的HelloController的注解写法

  1. package com.yuan.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. /**
  6. * @author 袁梓为
  7. * @Description
  8. * @date Created in 15:05 2022/7/14
  9. */
  10. @Controller
  11. public class HelloController {
  12. //此处@RequestMapping的"hello"就是url对应访问的地方
  13. //即是:localhost:8080/hello/h1
  14. //如果此处类上没有@RequestMapping,访问路径是:localhost:8080/h1
  15. @RequestMapping("/h1")
  16. public String hello(Model model){
  17. System.out.println("1234");
  18. model.addAttribute("msg","Hello,SpringMVCAnnotation");
  19. //会被视图解析器处理,即访问hello.jsp
  20. return "hello";
  21. }
  22. }

2.RestFul 风格

Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

功能
资源:互联网所有的事物都可以被抽象为资源
资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。
分别对应 添加、 删除、修改、查询。

3.重定向与请求转发(很多坑🕳)

3.1关于WEB-INF目录

//WEB-INF目录下的页面和资源是不能通过浏览器进行直接访问的

但是后台可以通过转发或者重定向进行访问,视图解析器默认使用转发。

3.2关于重定向和转发走不走视图解析器的争论

重定向不走视图解析器,重定向不能访问WEB-INF下的内容,所以重定向里面的路径不能在WEB-INF下。

如果显示的去调用forward和redirect,其实都不会走视图解析器,只有两个都不写(指没有forward和redirect关键词)才会走视图解析器

代码测试时,记得删除视图解析器再测试

  1. package com.yuan.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. /**
  6. * @author 袁梓为
  7. * @Description
  8. * @date Created in 16:41 2022/7/14
  9. */
  10. //记得删除视图解析器再测试
  11. @Controller
  12. @RequestMapping("forward")
  13. public class ForwardController {
  14. //此处如果加入了视图解析器,f2是可以的,但是f1会报404,因为视图解析器会导致f1下的路径错误
  15. @RequestMapping("f1")
  16. public String forward1(Model model){
  17. model.addAttribute("msg","forward 01");
  18. return "/WEB-INF/jsp/test.jsp";
  19. }
  20. @RequestMapping("f2")
  21. public String forward2(Model model){
  22. model.addAttribute("msg","forward 02");
  23. //此处一定要注意第一个”/“是在web下,所以一定要加上”/WEB-INF“
  24. return "forward:/WEB-INF/jsp/test.jsp";
  25. }
  26. @RequestMapping("d1")
  27. public String redirect(Model model){
  28. //重定向
  29. model.addAttribute("msg","redirect01");
  30. return "redirect:/index.jsp";
  31. }
  32. }

4.数据处理

提交的域名称和处理方法的参数名不管一致还是不一致,最好都把@RequeParam加上

  1. http://localhost:8081/user/t1?username=yuanziwei
  1. package com.yuan.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. /**
  7. * @author 袁梓为
  8. * @Description
  9. * @date Created in 18:12 2022/7/14
  10. */
  11. @Controller
  12. @RequestMapping("/user")
  13. public class UserController {
  14. @RequestMapping("/t1")
  15. public String test01(@RequestParam("username") String name, Model model){
  16. System.out.println("接收到的前端数据是"+name);
  17. model.addAttribute("msg",name);
  18. return "test";
  19. }
  20. }

数据显示到前端的三种方式

1.通过ModelAndView

  1. public class ControllerTest1 implements Controller {
  2. public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
  3. //返回一个模型视图对象
  4. ModelAndView mv = new ModelAndView();
  5. mv.addObject("msg","ControllerTest1");
  6. mv.setViewName("test");
  7. return mv;
  8. }
  9. }

2.通过Model

  1. @Controller
  2. public class HelloController {
  3. //此处@RequestMapping的"hello"就是url对应访问的地方
  4. //即是:localhost:8080/hello/h1
  5. //如果此处类上没有@RequestMapping,访问路径是:localhost:8080/h1
  6. @RequestMapping("/h1")
  7. public String hello(Model model){
  8. System.out.println("1234");
  9. model.addAttribute("msg","Hello,SpringMVCAnnotation");
  10. //会被视图解析器处理,即访问hello.jsp
  11. return "hello";
  12. }
  13. }

3.通过ModelMap

  1. @RequestMapping("/hello")
  2. public String hello(@RequestParam("username") String name, ModelMap model){
  3. //封装要显示到视图中的数据
  4. //相当于req.setAttribute("name",name);
  5. model.addAttribute("name",name);
  6. System.out.println(name);
  7. return "hello";
  8. }

@ResponseBody

@ResponseBody是配合@Controller使用的

在方法体上加@ResponseBody,它就不会走视图解析器,会直接返回一个字符串

使用@RestController

使用【@RestController】可以代替【ResponseBody+@Controller】

5.整合ssm

maven项目依赖

  1. <dependencies>
  2. <!--Junit-->
  3. <dependency>
  4. <groupId>junit</groupId>
  5. <artifactId>junit</artifactId>
  6. <version>4.12</version>
  7. </dependency>
  8. <!--数据库驱动-->
  9. <dependency>
  10. <groupId>mysql</groupId>
  11. <artifactId>mysql-connector-java</artifactId>
  12. <version>8.0.16</version>
  13. </dependency>
  14. <!-- 数据库连接池 -->
  15. <dependency>
  16. <groupId>com.mchange</groupId>
  17. <artifactId>c3p0</artifactId>
  18. <version>0.9.5.2</version>
  19. </dependency>
  20. <!--Servlet - JSP -->
  21. <dependency>
  22. <groupId>javax.servlet</groupId>
  23. <artifactId>servlet-api</artifactId>
  24. <version>2.5</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>javax.servlet.jsp</groupId>
  28. <artifactId>jsp-api</artifactId>
  29. <version>2.2</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>javax.servlet</groupId>
  33. <artifactId>jstl</artifactId>
  34. <version>1.2</version>
  35. </dependency>
  36. <!--Mybatis-->
  37. <dependency>
  38. <groupId>org.mybatis</groupId>
  39. <artifactId>mybatis</artifactId>
  40. <version>3.5.2</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.mybatis</groupId>
  44. <artifactId>mybatis-spring</artifactId>
  45. <version>2.0.2</version>
  46. </dependency>
  47. <!--Spring-->
  48. <dependency>
  49. <groupId>org.springframework</groupId>
  50. <artifactId>spring-webmvc</artifactId>
  51. <version>5.1.9.RELEASE</version>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.springframework</groupId>
  55. <artifactId>spring-jdbc</artifactId>
  56. <version>5.1.9.RELEASE</version>
  57. </dependency>
  58. </dependencies>

一个快捷写crud语句的方式

  1. 不出现提示信息的方法:在settings-->Languages &Frameworks-->SQL Dialects中配置数据库类型,将项目添加进来,配置完成后Apply-OK

之前一直没有将idea和navicat绑定,今天学习了将数据库和idea绑定起来,在写sql语句时会有关键词提示,能够提升速度