1.Controller
控制器controller,通常通过接口定义或注解定义两种方法实现。
1.1实现Controller接口
- 实现接口Controller定义控制器是较老的办法
- 缺点是:一个控制器中只有一个方法,如果要多个方法则需要定义多个Controller;定义的方式比较麻烦;
1.2使用注解@Controller
内部模块的maven配置
<?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">
<parent>
<artifactId>SpringMVC</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-annotation</artifactId>
<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>
</project>
外层模块的maven配置
<?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>org.example</groupId>
<artifactId>SpringMVC</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springmvc-01</module>
<module>spring-annotation</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</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>
</project>
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拦截 -->
<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.yuan.controller"/>
<mvc:default-servlet-handler/>
<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>
com.yuan.controller包下的HelloController的注解写法
package com.yuan.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 袁梓为
* @Description
* @date Created in 15:05 2022/7/14
*/
@Controller
public class HelloController {
//此处@RequestMapping的"hello"就是url对应访问的地方
//即是:localhost:8080/hello/h1
//如果此处类上没有@RequestMapping,访问路径是:localhost:8080/h1
@RequestMapping("/h1")
public String hello(Model model){
System.out.println("1234");
model.addAttribute("msg","Hello,SpringMVCAnnotation");
//会被视图解析器处理,即访问hello.jsp
return "hello";
}
}
2.RestFul 风格
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
功能
资源:互联网所有的事物都可以被抽象为资源
资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。
分别对应 添加、 删除、修改、查询。
3.重定向与请求转发(很多坑🕳)
3.1关于WEB-INF目录
//WEB-INF目录下的页面和资源是不能通过浏览器进行直接访问的,
但是后台可以通过转发或者重定向进行访问,视图解析器默认使用转发。
3.2关于重定向和转发走不走视图解析器的争论
重定向不走视图解析器,重定向不能访问WEB-INF下的内容,所以重定向里面的路径不能在WEB-INF下。
如果显示的去调用forward和redirect,其实都不会走视图解析器,只有两个都不写(指没有forward和redirect关键词)才会走视图解析器
代码测试时,记得删除视图解析器再测试
package com.yuan.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 袁梓为
* @Description
* @date Created in 16:41 2022/7/14
*/
//记得删除视图解析器再测试
@Controller
@RequestMapping("forward")
public class ForwardController {
//此处如果加入了视图解析器,f2是可以的,但是f1会报404,因为视图解析器会导致f1下的路径错误
@RequestMapping("f1")
public String forward1(Model model){
model.addAttribute("msg","forward 01");
return "/WEB-INF/jsp/test.jsp";
}
@RequestMapping("f2")
public String forward2(Model model){
model.addAttribute("msg","forward 02");
//此处一定要注意第一个”/“是在web下,所以一定要加上”/WEB-INF“
return "forward:/WEB-INF/jsp/test.jsp";
}
@RequestMapping("d1")
public String redirect(Model model){
//重定向
model.addAttribute("msg","redirect01");
return "redirect:/index.jsp";
}
}
4.数据处理
提交的域名称和处理方法的参数名不管一致还是不一致,最好都把@RequeParam加上
http://localhost:8081/user/t1?username=yuanziwei
package com.yuan.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author 袁梓为
* @Description
* @date Created in 18:12 2022/7/14
*/
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/t1")
public String test01(@RequestParam("username") String name, Model model){
System.out.println("接收到的前端数据是"+name);
model.addAttribute("msg",name);
return "test";
}
}
数据显示到前端的三种方式
1.通过ModelAndView
public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//返回一个模型视图对象
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ControllerTest1");
mv.setViewName("test");
return mv;
}
}
2.通过Model
@Controller
public class HelloController {
//此处@RequestMapping的"hello"就是url对应访问的地方
//即是:localhost:8080/hello/h1
//如果此处类上没有@RequestMapping,访问路径是:localhost:8080/h1
@RequestMapping("/h1")
public String hello(Model model){
System.out.println("1234");
model.addAttribute("msg","Hello,SpringMVCAnnotation");
//会被视图解析器处理,即访问hello.jsp
return "hello";
}
}
3.通过ModelMap
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name, ModelMap model){
//封装要显示到视图中的数据
//相当于req.setAttribute("name",name);
model.addAttribute("name",name);
System.out.println(name);
return "hello";
}
@ResponseBody
@ResponseBody是配合@Controller使用的
在方法体上加@ResponseBody,它就不会走视图解析器,会直接返回一个字符串
使用@RestController
使用【@RestController】可以代替【ResponseBody+@Controller】
5.整合ssm
maven项目依赖
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</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.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
一个快捷写crud语句的方式
不出现提示信息的方法:在settings-->Languages &Frameworks-->SQL Dialects中配置数据库类型,将项目添加进来,配置完成后Apply-OK
之前一直没有将idea和navicat绑定,今天学习了将数据库和idea绑定起来,在写sql语句时会有关键词提示,能够提升速度