SpringMVC
1.SpringMVC项目搭建
1.1pom文件
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.aaa.springmvc</groupId>
<artifactId>SpringMVCDemo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringMVCDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- spring统一版本-->
<properties>
<spring.version>5.3.10</spring.version>
</properties>
<dependencies>
<!-- spring IOC的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- springmvc依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- springmvc依赖的第三方jar包 这个包是springmvc相应ajax请求时 将数据转换为json字符串包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<!-- spring面向切面编程依赖包 aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring数据库依赖包 事务操作使用-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- servlet-api 依赖包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
</dependencies>
</project>
1.2 Spring配置文件Spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启spring注解配置-->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.aaa"></context:component-scan>
<!-- 开启springmvc注解配置-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
1.3web.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">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<!-- /代表所有请求 不包括jsp /*包括jsp-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
1.4测试StudentController
package com.aaa.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author Grg
* @Date 2023/9/11 21:23
* @PackageName:com.aaa.springmvc.controller
* @ClassName: StudentController
* @Description: 又是码代码的一天
* @Version plus max 宇宙无敌终极版本
*/
@Controller
@RequestMapping("stu")
public class StudentController {
@RequestMapping("add")
public void addStu(){
System.out.println("添加学生");
}
@RequestMapping("delete")
public void delStu(){
System.out.println("删除学生");
}
}
在路径输入对应路径,可以看到控制台输出
2.配置启动优先级
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
加上这一句时,在启动猫咪的时候 就会直接加载类 就让第一次访问变快,但是启动猫咪的时间会变长
<load-on-startup>1</load-on-startup>
</servlet>
3.访问静态资源
在配置了<load-on-startup>1</load-on-startup> 访问不到静态资源.html文件
是因为和Tomcat的配置文件冲突了
老版本修改web.xml <!-- <url-pattern>*.do</url-pattern>-->
新版本修改spring配置文件 <mvc:default-servlet-handler></mvc:default-servlet-handler>
4.接收请求参数
4.1传统方式
@RequestMapping("add")
public void addStu(HttpServletRequest req, HttpServletResponse resp){
String id = req.getParameter("id");
}
4.2直接注入
@RequestMapping("delete")
//传过来的参数需要名字一致 不然就赋值为空 如果参数不能赋值为空就会报500
public void delStu(int id,String name){
System.out.println("删除学生");
}
@RequestMapping("update") //不匹配可以加这个@RequestParam("xxx")
public void updateStu(@RequestParam("sid") int id,@RequestParam("sname") String name) {
System.out.println("删除学生");
}
4.3对象注入
@RequestMapping("list")
//当传过来的参数 对应着StudentVO中的成员变量 就会自动给对象赋值
public void list(StudentVO s) {
System.out.println(s);
System.out.println("对象注入");
}
4.4地址栏传参
@RequestMapping("xixi/{id}/{name}")
public void getStu(@PathVariable("id") int id,@PathVariable("name") String name) {
System.out.println(id);
System.out.println(name);
System.out.println("地址栏传参");
}
5.跳转结果视图
5.1传统方式
@RequestMapping("haha")
public void haha(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
5.2ModelAndView
@RequestMapping("sdfd")
public ModelAndView sdfd() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/index.jsp");
return modelAndView;
}
5.3forward
@RequestMapping("xixi")
public String xixi() {
//请求转发
return "forward:/index.jsp";
}
@RequestMapping("xixi")
public String xixi() {
//重定向
return "redirect:/index.jsp";
}
6.共享数据
6.1传统方式
@RequestMapping("haha")
public void haha(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("msg","共享数据");
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
6.2ModelAndView
@RequestMapping("sdfd")
public ModelAndView sdfd() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","共享数据")
modelAndView.setViewName("/index.jsp");
return modelAndView;
}
6.3forward
@RequestMapping("xixi")
public String xixi(Model m) {
m.addAttribute("msg","共享数据");
return "forward:/index.jsp";
}
@Autowired
private HttpSession session;
@RequestMapping("hei")
public String hei(Model m) {
session.setAttribute("msg", "共享数据");
return "forward:/index.jsp";
}
7.响应Ajax
@RequestMapping("haha")
@ResponseBody
public AjaxResult haha(){
return AjaxResult.success();
}
@RestController //复合注解
@RequestMapping("ajax")
public class AjaxController {
@RequestMapping("haha")
public AjaxResult haha(){
return AjaxResult.success();
}
}