后端框架学习3------SpringMVC

springMVC学习笔记

官方文档地址:https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc

使用spring注解开发

步骤:
1、新建web项目(将简单的maven项目添加框架)
2、导入相关的jar包

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
    </dependency>
</dependencies>

3、编写web.xml文件,注册DispatcherServlet(类似mvc中的注册servlet)

<?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">
    <!--配置DispatchServlet;这个是springMVC的核心,请求分发器,前端控制器-->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <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中,/:代表匹配所有的请求,不包括jsp页面。/*:匹配所有的请求,包括jsp-->
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

4、编写springmvc配置文件

<?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
       http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!--自动扫描包,让指定包下的注解生效,有ioc容器统一管理-->
  <context:component-scan base-package="com.zheng.controller"/>
    <!--让springmvc 不处理静态资源-->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--BeanNameUrlHandlerMapping-->

</beans>

5、创建对应的控制类,controller

6、构造前端视图和controller之间的对应

//代表这个类会被spring接管,被这个注解的类中的所有方法,如果返回值是string,并且有具体页面可以跳转,那么就会被视图解析器解析
@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String hello(Model model){
        //封装数据
        model.addAttribute("msg","hello springMVC");
       return "hello";

    }
}

7、测试

8、项目结构
在这里插入图片描述
使用springMVC必须配置的三部分

  • 处理器映射器
  • 处理器适配器
  • 视图解析器

注意RequestMapper说明

@Controller
@RequestMapping("c3")
public class ControllerTest3 {
    @RequestMapping("/t3")
    public String tset3(Model model){
        model.addAttribute("msg","hello test3");
        return "test";
    }
}

如果RequestMapping写在类名之上,则在访问的时候需要带上它,例如/c3/t3.
一般而言只写在方法名上即可。

在测试的时候:访问出现404
1、查看控制台报的错误,缺少jar包,导入相应的jar包即可
2、jar包存在,在IDEA的项目中、添加lib依赖
【file–>project structure–>artifacts–>选中对应的项目–>Output layout–>WEB-INF–>新建lib文件夹–>点击上方的加号+添加library files所有的包到lib】

ResultFul风格

package com.zheng.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class ResultController {

 //   @GetMapping("/add/{a}/{b}")  //http://localhost:8080/springMVC/add/6/9
    public String test1(@PathVariable int a, @PathVariable String b, Model model){
        String res=a+b;//数字和字符串拼接
        model.addAttribute("msg","test1"+res);
        return "test";

    }

  @RequestMapping(path = "/add/{a}/{b}",method = RequestMethod.GET)  //http://localhost:8080/springMVC/add/4/7
    public String test2(@PathVariable int a,@PathVariable String b,Model model){
        String res=a+b;
        model.addAttribute("msg","test2:"+res);
        return "test";
    }
    //http://localhost:8080/springMVC/add?a=2&b=4
  //  @RequestMapping("/add")
    public String test(int a,int b, Model model){
        int res=a+b;//数字和字符串拼接
        model.addAttribute("msg","结果="+res);
        return "test";

    }
}

一般的资源访问是: //http://localhost:8080/springMVC/add?a=2&b=4
使用规范后的是://http://localhost:8080/springMVC/add/4/7

重定向和转发

package com.zheng.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;



@Controller
public class ModelTest {

//此时视图解析器被注释掉
    @RequestMapping("/m/t")
    public String test(Model model){
        model.addAttribute("msg","ModelTest1");
        return "forward:/WEB-INF/jsp/test.jsp";   //转发的形式,没有视图解析的情形,地址栏不变
    }

    @RequestMapping("/t")
    public String test2(Model model){
        model.addAttribute("msg","test2");
            return "redirect:/index.jsp";//重定向,没有视图解析的情形,地址栏改变

    }

}

乱码问题

配置tomcat,打开tomcat文件下的conf–》server。

  • 添加【URIEncoding=“UTF-8”】到对应的文件位置
<Connector port="8080" URIEncoding="UTF-8" redirectPort="8443" connectionTimeout="20000" protocol="HTTP/1.1"/>
posted on 2022-08-28 22:20  热爱技术的小郑  阅读(18)  评论(0编辑  收藏  举报