SpringMVC_2023_11_27_2 SpringMVC_入门(注解形式)

发布时间 2023-11-29 10:10:04作者: Kbaor

SpringMVC_入门---(注解形式)

2023-11-28 16:31:09 星期二

常用的注解:
@Controller :标注当前类为:处理器
@RequestMapping:设置请求链接

SpringMVC注解项目的搭建

a) 依赖的引入

<dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- spring基础依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.20</version>
        </dependency>
        <!--  springweb依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.20</version>
        </dependency>
        <!--springmvc依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.20</version>
        </dependency>
    </dependencies>

b) 创建springmvc核心的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

    <!--处理器映射器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <!--处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
    <!--
      处理器:(普通的类,添加了@Controller注解)
      加载:@Controller注解的类
  -->
    <context:component-scan base-package="com.bboy.controller"/>
    <!--
        视图解析器
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    </bean>



</beans>

c) 配置前端控制器

<?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">
    <!--SpringMVC前端控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--
        在前端控制器中加载springmvc的配置文件,例如:spring-mvc.xml
    -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--
             配置哪些请求进入到:前端控制器
        -->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

d) Controller的创建

package com.bboy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * SpringMVC的处理器(普通的类)
 * @作者:秦帅
 * @时间:2023/11/27 0027 17:06:03
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    @RequestMapping("/login.do")
    public ModelAndView login(){
        System.out.println("===>login");
        return null;
    }
    @RequestMapping("/reg.do")
    public ModelAndView reg(){
        System.out.println("===>reg");
        return null;
    }
    @RequestMapping("/listStudents.do")
    public ModelAndView listStudents(){
        System.out.println("===>listStudents");
        return null;
    }
}

自定义Controller中的方法

a) 第一种:返回值为 ModelAndView

 /**
     * 方式一:返回ModelAndView
     *  Model:存储数据
     *  View:指定视图(页面)
     *  如何获取,前端页面的内容?
     */
    @RequestMapping("/test_1.do")
    public ModelAndView test_1(HttpServletRequest request){
        //-获取前端内容
        String uname = request.getParameter("uname");
        ModelAndView mav = new ModelAndView();
        //-:Model 添加数据
        mav.addObject("info","ceshi message");
        //-:指定跳转
        mav.setViewName("/WEB-INF/show.jsp");
        return mav;
    }

b) 第二种:返回值为void

/**
     * 方式二:没有返回值的
     *  类似于:servlet的doGet/doPost
     */
    @RequestMapping("/test_2.do")
    public void test_2(HttpServletRequest request, HttpServletResponse response){
        //-:获取内容,使用 request
        //-:响应:使用response
    }

c) 第三种:返回值为 String

 /**
     * 方式三:返回字符串
     *  一般返回的是 跳转的路径,包括 重定向与转发
     *  如果是转发,如何携带数据呢?
     *   使用Model 类进行数据的携带
     */
    @RequestMapping("/test_3.do")
    public String test_3(Model model){
        //-:携带数据
        model.addAttribute("uname","tom");
        model.addAttribute("sex","男");
        //-:转发:
        //return "forward:/WEB-INF/show.jsp";
        //return "/WEB-INF/show.jsp";
        //-:重定向: /WEB-INF/下的内容不能进行重定向
        //return "redirect:/WEB-INF/show.jsp";
        return "redirect:/result.jsp";
    }

页面

result.jsp

<%--
  Created by IntelliJ IDEA.
  User: new
  Date: 2023/11/27
  Time: 15:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
   外部结果页面-展示页面<br/>
${uname}-${sex}
</body>
</html>

show.jsp

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2023/11/29 0029
  Time: 9:24:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
展示页面<br/>

${uname}-${sex}
</body>
</html>

接收页面传递的数据

a) 第一种方式:request
b) 第二种方式:定义参数

package com.bboy.controller;

import com.bboy.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/29 0029 9:44:05
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    @RequestMapping("/login")
    public String login(String uname, String upassword, Model model){
        model.addAttribute("uname",uname);
        model.addAttribute("password",upassword);


        return "/WEB-INF/show.jsp";
    }
    @RequestMapping("/reg")
    public String reg(Student stu, Model model){
        System.out.println(stu);
        model.addAttribute("stu",stu);
        return "/WEB-INF/show.jsp";
    }

    public String deleteStudentById(int id){
        return null;
    }
    public String deleteBatch(int[] ids){
        return null;
    }

}

编码过滤

a) Web.xml 中配置编码过滤器

  <!--编码过滤器-->
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>