SpringMVC

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.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>

1.2 Spring配置文件Spring.xml


<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>

1.3web.xml


<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>

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>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>

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();
    }
}

你可能感兴趣的:(Java学习,java,springMVC,spring)