• SpringMVC框架之最佳实践(演示在开发中最为基础的代码演示)


    61c73300c3404464a914021786082a21.jpg

     

    下面链接为新手刚接触时做的配置,但是这种方法并不是在实际开发所需要的代码格式,接下来演示项目中基础代码的实现流程

     项目中出现Spring和SpringMVC时,如何进行配置?_不想睡醒的梦的博客-CSDN博客

    演示在开发过程中的代码演示,在日常的开发过程中我们并不会像为每个类创建方法,这些内容几乎都是靠注解的方式去实现,再通过组件扫描功能为每个类自动去创建对象,在SpringMVC的开发过程中我们要配置处理器适配器 处理器映射器等,今天讲解的就是在SpringMVC框架下的最终实现,基本上以下内容就是在日常开发中所使用的代码的最终实践。

     

    处理器映射器:HandlerMapping

    之前在使用处理器映射器时,使用的BeanNameUrlHandlerMapping 处理器映射器,但是这种方法不能实现注解方式,所以我们需要通过新的类去实现在项目中创建的注解对象RequestMappingHandlerMapping这个类可以匹配@RequestMapping()

     

    处理器适配器:HandlerAdapter

    在注解方法中使用RequestMappingHandlerAdapter来实现处理器适配器

     

    代码实现

    配置在SpringMVC框架的环境配置

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    6. <context:component-scan base-package="controller" >
    7. context:component-scan>
    8. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" id="handlerMapping">bean>
    9. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" id="handlerAdapter">bean>
    10. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="modelAndView">
    11. <property name="prefix" value="/jsp/">property>
    12. <property name="suffix" value=".jsp">property>
    13. bean>
    14. beans>

    创建Controller类,并通过注解的方式

    1. @Controller //注解通过扫描组件为这各类创建对象
    2. public class Controller4 {
    3. @RequestMapping("/hello") //注解方法实现请求,/hello4是这个请求的URL
    4. public ModelAndView demo1(){
    5. ModelAndView modelAndView = new ModelAndView("hello");
    6. modelAndView.addObject("name","springMVC");
    7. return modelAndView;
    8. }
    9. }

    然后就是在web.XML中配置servlet和servletMapping

    1. "1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <servlet>
    7. <servlet-name>springmvcservlet-name>
    8. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    9. <init-param>
    10. <param-name>contextConfigLocationparam-name>
    11. <param-value>classpath:springmvc_demo1.xmlparam-value>
    12. init-param>
    13. servlet>
    14. <servlet-mapping>
    15. <servlet-name>springmvcservlet-name>
    16. <url-pattern>/url-pattern>
    17. servlet-mapping>
    18. web-app>

    在pom.XMl中导入SpringMVC中需要的各种依赖

    创建一个jsp文件,让项目中的内得以输出

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: lenovo
    4. Date: 2022/11/2
    5. Time: 20:06
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. head>
    12. <body>
    13. hello ${name}
    14. body>
    15. html>

    执行项目:

    去浏览执行hello的URL

    78fed8c721e64955852a9231997ad82f.png

    以上就是在实际开发过程中最最最为基础的框架实现代码

     

    请求窄化

    在开发的过程中Controller类可能有很多,怎么通过URL去调用到具体的类呢?请求窄化这个方法就可以更加精准的定位到我们所需要的类,具体实现那就是在类的上面再加一个@RequestMapping注解,并为这个类加一个更加精准的URl

    代码:

    1. @Controller
    2. @RequestMapping("/web") //请求窄化的实现代码
    3. //区别在于访问的URL不同,之前是http://localhost:8080/hello,
    4. //但是如果使用的请求窄化这个方法URL为:http://localhost:8080/web/hello
    5. public class controller_Demo1 {
    6. @RequestMapping("/hello")
    7. public ModelAndView demo1(){
    8. ModelAndView modelAndView = new ModelAndView("hello");
    9. modelAndView.addObject("name","springMVC");
    10. return modelAndView;
    11. }
    12. }

     

    结果:注意荧光笔部分

    81bfe39335a240f595c84a4d54761fb1.png

     请求方法限定:

    在HTTP请求分为很多种,例如:get方法.post方法等等,在注解@RequestMapping中含有一项参数可以设置那些请求类型能请求这个方法的内容

    注意一点:jsp请求只能使用 GET POST HEAD这三种方法,除了这三种方法请求jsp文件,其他的都不能显示jsp的内容

    代码:

    1. @Controller
    2. @RequestMapping("/web")
    3. public class controller_Demo1 {
    4. //@RequestMapping(value = "/hello",method = RequestMethod.GET) 这种方法表明只能get方法才能输出 @RequestMapping(value = "/hello",method ={RequestMethod.GET,RequestMethod.HEAD}) 这种数组表示只能使用数组的请求方法才能请求数据
    5. //也可以这样编写
    6. @GetMapping("/hello11")
    7. public ModelAndView demo1(){
    8. ModelAndView modelAndView = new ModelAndView("hello");
    9. modelAndView.addObject("name","springMVC");
    10. return modelAndView;
    11. }
    12. }

     

     

  • 相关阅读:
    智能文档控制——文档的智能归档、捕获、索引、访问和协作
    PP-yoloE论文的理解
    Golang环境搭建Win10(简洁版)
    Python字符串类型详解(三)——字符串类型的格式化
    9.11作业
    【MySQL】用户管理
    AE custom flow
    【AI+编程】工作日常场景随时可以AI编程,记一个问答SQL快速导出数据日常示例
    2023华为杯D题——基于Kaya模型的碳排放达峰实证研究
    全球明星百科查询易语言代码
  • 原文地址:https://blog.csdn.net/m0_52479012/article/details/127669647