<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.0modelVersion>
<groupId>com.aaa.springmvcgroupId>
<artifactId>SpringMVCDemoartifactId>
<packaging>warpackaging>
<version>1.0-SNAPSHOTversion>
<name>SpringMVCDemo Maven Webappname>
<url>http://maven.apache.orgurl>
<properties>
<spring.version>5.3.10spring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.14.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.33version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.23version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.1version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.28version>
dependency>
dependencies>
project>
<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">
<context:annotation-config>context:annotation-config>
<context:component-scan base-package="com.aaa">context:component-scan>
<mvc:annotation-driven>mvc:annotation-driven>
beans>
<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 Applicationdisplay-name>
<servlet>
<servlet-name>DispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>DispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
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("删除学生");
}
}
在路径输入对应路径,可以看到控制台输出
<servlet>
<servlet-name>DispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring.xmlparam-value>
init-param>
加上这一句时,在启动猫咪的时候 就会直接加载类 就让第一次访问变快,但是启动猫咪的时间会变长
<load-on-startup>1load-on-startup>
servlet>
在配置了<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>
@RequestMapping("add")
public void addStu(HttpServletRequest req, HttpServletResponse resp){
String id = req.getParameter("id");
}
@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("删除学生");
}
@RequestMapping("list")
//当传过来的参数 对应着StudentVO中的成员变量 就会自动给对象赋值
public void list(StudentVO s) {
System.out.println(s);
System.out.println("对象注入");
}
@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("地址栏传参");
}
@RequestMapping("haha")
public void haha(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
@RequestMapping("sdfd")
public ModelAndView sdfd() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/index.jsp");
return modelAndView;
}
@RequestMapping("xixi")
public String xixi() {
//请求转发
return "forward:/index.jsp";
}
@RequestMapping("xixi")
public String xixi() {
//重定向
return "redirect:/index.jsp";
}
@RequestMapping("haha")
public void haha(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("msg","共享数据");
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
@RequestMapping("sdfd")
public ModelAndView sdfd() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","共享数据")
modelAndView.setViewName("/index.jsp");
return modelAndView;
}
@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";
}
@RequestMapping("haha")
@ResponseBody
public AjaxResult haha(){
return AjaxResult.success();
}
@RestController //复合注解
@RequestMapping("ajax")
public class AjaxController {
@RequestMapping("haha")
public AjaxResult haha(){
return AjaxResult.success();
}
}