主頁 > 後端開發 > SpringMVC

SpringMVC

2023-07-13 07:45:16 後端開發

SpringMVC

Spring集成web環境

集成步驟

  1. 匯入相關的坐標,spring的和web的

      <dependencies>
          <!--        spring-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.3.6</version>
        </dependency>
        <dependency>
          <!--            mysql-->
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.32</version>
        </dependency>
        <!--        資料源一:c3p0-->
        <dependency>
          <groupId>c3p0</groupId>
          <artifactId>c3p0</artifactId>
          <version>0.9.1.2</version>
        </dependency>
        <!--        資料源二:druid-->
        <dependency>
          <groupId>cn.6tail</groupId>
          <artifactId>nlf-mini-plugin-druid</artifactId>
          <version>1.0.0</version>
        </dependency>
          <!--       junit-->
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
        <!--        spring集成junit坐標:-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>5.3.6</version>
        </dependency>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter</artifactId>
          <version>RELEASE</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>RELEASE</version>
          <scope>compile</scope>
        </dependency>
       <!--  servlet-->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>4.0.1</version>
        </dependency>
       <!--  jsp-->
        <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>javax.servlet.jsp-api</artifactId>
          <version>2.3.3</version>
        </dependency>
      </dependencies>
    
  2. spring相關配置

  3. 添加一個web包,用來寫javaweb

  4. 在web.xml配置相關類

  5. 配置tomcat,部署專案運行

ApplicationContext應用背景關系的獲取方式

之前每次我們都是通過new的方式創建的應用背景關系,這樣的弊端是在大專案中組態檔會加載多次,應用背景關系被創建多次,繁瑣且不節省資源

所以我們運用監聽器來只創建一次,所有的用這一個容器,所以:

創建一個實作ServletContext監聽器的類:

public class ContextLoader implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //將這個應用背景關系物件存盤在servletContext域中
        ServletContext servletContext = sce.getServletContext();
        servletContext.setAttribute("app",app);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }
}

web層中:

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = req.getServletContext();
        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        UserService userService= app.getBean(UserService.class);
        userService.save();
    }

web.xml:

<!--配置監聽器-->
<listener>
  <listener-class>com.myspring.listener.ContextLoader</listener-class>
</listener>
<!--配置全域引數-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

Spring提供的獲得應用背景關系的工具

沒錯,上面又白寫了,,,

上面的不用手動實作,Spring提供了一個監聽器ContextLoaderListener就是對該功能的封裝

我們只需要做兩件事:

  1. 在web.xml中配置ContextLoaderListener監聽器(需要先匯入spring-web坐標)

    pom.xml:

    <!-- spring-web-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.3.6</version>
        </dependency>
    

    web.xml:

    <!--配置監聽器-->
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
  2. 通過WebApplicationCintextUtils(一個客戶端工具)獲得應用背景關系物件ApplicationContext

    web層中:

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
        ServletContext servletContext = req.getServletContext();
    //關鍵
        WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService= app.getBean(UserService.class);
        userService.save();
    }
    

SpringMVC簡介

  • SpringMVC 是一種基于 Java 的實作 MVC 設計模型的請求驅動型別的輕量級 Web 框架,屬于SpringFrameWork的后續產品,已經融合在 Spring Web Flow 中, SpringMVC已經成為目前最主流的MVC框架之一,

  • SpringMVC的作用:作為前端控制器,控制請求的共有行為,在傳統的JavaEE技術中,只使用Servlet作為控制器,當每個功能都需要一個servlet程式相似功能也無法分開封裝,另外,傳統的開發模式也存在其它使用不便利的做法,

    SpringMVC解決了V-C互動的問題,即V(View:視圖)和C(Controller:控制器)之間的互動問題,具體表現在:用戶可以通過視圖將請求資料提交給服務器端的控制器,而控制器可以接收到相關資料后進行處理,最終,給予客戶端某個視圖,使得客戶端得到回應結果,當然springMVC作為控制器還是需要servlet

SpringMVC開發步驟

  1. 匯入SpringMVC

    <!--    spring-mvc-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.3.6</version>
        </dependency>
    
  2. 配置servlet在web.xml

      <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--    告訴控制層組態檔位置-->
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
    <!--    服務器加載時就啟動本servlet-->
    <!--    當值為0或者大于0時,表示容器在應用啟動時就加載這個servlet;-->
    <!--    當是一個負數時或者沒有指定時,則指示容器在該servlet被選擇時才加載,-->
    <!--    正數的值越小,啟動該servlet的優先級越高,-->
        <load-on-startup></load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
    <!--    所有請求都會經過servlet-->
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
  3. 撰寫pojo(controller)

  4. 將controller使用注解(@Controller)配置到Spring容器中

    //表明是一個控制類bean實體
    @Controller
    public class UserController {
        //地址映射,當跳轉/quick,訪問此方法
        @RequestMapping("/quick")
        public  String save(){
            System.out.println("Controller save running");
    //        要跳轉的視圖
            return "success.jsp";
        }
    }
    
  5. 配置組件掃描(配置到Spring-mvc.xml:springmvc的組態檔,需要自己在resources創建)

    <!--    組件掃描:掃描controller-->
        <context:component-scan base-package="com.myspring.controller"/>
    
  6. 發送請求測驗

流程:

SpringMVC的組件決議

SpringMVC內部執行流程

SpringMVC注解決議

@RequestMapping(""):地址映射

常用引數:value:就是路徑

? method:可以指定請求方式,值為列舉方式在RequestMethod

? params:指定請求引數條件,例如:

?

@Controller
@RequestMapping("/user")
public class UserController {
    //當地址為localhost:8080/user/quick時訪問該方法
    @RequestMapping("/quick")
    public  String save(){
        System.out.println("Controller save running");
//        要跳轉的視圖:localhost:8080/user/success.jsp,當前的前一級目錄下的該資源(/success.jsp.就代表當前web應用下,即webapp目錄下)
        return "success.jsp";
    }
}

SpringMVC配置決議

在上面的代碼中,最后我們renturn了jsp頁面

return "success.jsp";
//實際上本句為
return "forword:success.jsp";
//forword在本處意思為轉發(不改變url地址訪問資源),默認為forword
return "redirect:/success.jsp"
//redirect:代表重定向

我們還可以在springmvc中配置視圖決議器,來避免寫前綴后綴

<!--    配置視圖決議器-->
    <bean id="viewResolver" >
<!--        前綴-->
        <property name="prefix" value="https://www.cnblogs.com/jsp/"></property>
<!--        后綴-->
        <property name="suffix" value="https://www.cnblogs.com/rainaftersummert/archive/2023/07/12/.jsp"></property>
    </bean>

這樣一來我們可以這樣寫:
return "/success"
代表:/jsp/success.jsp

總結:

SpringMVC的資料回應

SpringMVC的資料回應方式:

  1. 頁面跳轉
    • 直接回傳字串
    • 通過ModelAndView物件回傳
  2. 回寫資料
    • 直接回傳字串
    • 回傳物件或集合

頁面跳轉

  1. 直接回傳字串:此種方式會將回傳的字串與視圖決議器的前后綴拼接后跳轉:不多說,上面例子就是用的這種

  2. 通過ModelAndView物件回傳

        @RequestMapping("/quick2")
        public ModelAndView save2(){
            ModelAndView modelAndView = new ModelAndView();
    //        設定模型資料:可以在jsp中通過el運算式獲得
          modelAndView.addObject("username","lihua");
    //        設定視圖名稱
            modelAndView.setViewName("index.jsp");
            return modelAndView;
        }
    
    
    //這種也可以,和上面一樣只不過是springmvc為你注入了一個bean實體
        @RequestMapping("/quick2")
        public ModelAndView save2(ModelAndView modelAndView){
    //        設定模型資料
            modelAndView.addObject("username","lihua");
    //        設定視圖名稱
            modelAndView.setViewName("index");
            return modelAndView;
        }
    

? 回傳字串的形式也可以傳遞引數:

    @RequestMapping("/quick")
    public  String save(Model model){
        model.addAttribute("username","lihua");
//        要跳轉的視圖
        return "success";
    }

//另外代替Model,還可以傳HttpServletRequest等作為引數

? 在index.jsp中

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
這個isELIgnored="false"表示el運算式可用
<html>
<body>
<h2>Hello World!${username}</h2>
</body>
</html>

回寫資料

  1. 直接回傳字串

    在web階段我們需要回寫字串直接response.getWriter().print("hello world"),在Controller直接通過引數注入response物件,但是還有更加簡單的方法:回傳字串

    @RequestMapping("/quick")
    //告知springmvc回傳的用于回寫資料的字串,不要進行頁面跳轉
    @ResponseBody
    public  String save(HttpServletResponse response){
        return "helloworld";
    }
    
  2. 回傳物件或集合

    當我們需要傳遞一個物件或者集合時,可以先轉成json格式字串再return

        @RequestMapping("/quick")
    //    還是要寫這個注解,因為還是回傳的字串資料
        @ResponseBody
        public  String save(HttpServletResponse response) throws IOException {
            UserServiceImpl userService = new UserServiceImpl();
            userService.setAge(10);
            userService.setName("lihua");
    //        使用json轉換工具將物件轉換為json格式字串再回傳
            ObjectMapper objectMapper = new ObjectMapper();
            String string = objectMapper.writeValueAsString(userService);
            return string;
    

    需要匯入的json轉換工具坐標:

    <!--    json工具核心-->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
          <version>2.11.4</version>
        </dependency>
    <!--    json資料系結-->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.11.4</version>
        </dependency>
    <!--    json和注解相關-->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
          <version>2.11.4</version>
        </dependency>
    

? springmvc不需要我們自己來轉json字串,有更加簡便的方式:

? 回傳物件和集合,

? 首先需要我們在spring組態檔中進行一些配置(配置處理器映射器):

<!--    配置處理器映射器-->
    <bean  >
        <property name="messageConverters">
            <list>
                <bean >
                </bean>
            </list>
        </property>
    </bean>

? 控制類中:

    @RequestMapping("/quick")
//    還是要寫這個注解,因為還是回傳的字串資料
    @ResponseBody
    public UserServiceImpl save(HttpServletResponse response) throws IOException {
        UserServiceImpl userService = new UserServiceImpl();
        userService.setAge(10);
        userService.setName("lihua");
        return userService;
    }

配置太麻煩了,可以通過mvc的注解驅動代替上述配置,沒錯,又又又有更加簡便的操作:

在springmvc的組態檔中:

<!--        mvc注解驅動-->
        <mvc:annotation-driven/>
        
注意:注解驅動對應的命名空間必須是
xmlns:mvc="http://www.springframework.org/schema/mvc"
其他兩個不行

SpringMVC獲取請求資料

獲取請求引數

  1. 獲取基本型別引數:直接通過引數獲得:

    @RequestMapping("/quick")
    @ResponseBody
    public void save(String username,int age) throws IOException {
        System.out.println(username);
        System.out.println(age);
    }
    

    url:

    結果:

  2. 獲得pojo型別引數:pojo引數的屬性名和請求引數的name一致,引數值會自動映射匹配

    @Controller
    public class UserController {
        private int age;
        private String name;
    //    還需要getter和setter
    
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @RequestMapping("/quick")
        @ResponseBody
        public void save(UserController userController) throws IOException {
            System.out.println(userController.getAge());
            System.out.println(userController.getName());
        }
    
    }
    

    輸入與結果:

  1. 獲得陣列型別引數:和基本資料型別完全一致,函式引數和請求的引數一致就可以

  1. 獲得集合型別引數:需要被包裝到一個pojo類中才可以

    public class VO {
        public List<String> getStringList() {
            return stringList;
        }
    
        public void setStringList(List<String> stringList) {
            this.stringList = stringList;
        }
    
        private List<String> stringList;
    }
    
@RequestMapping("/quick")
@ResponseBody
public void save(VO vo) throws IOException {
    System.out.println(vo.getStringList());
}

當請求為Ajax可以通過指定contenttype為json格式,通過注解直接獲取集合資料:

ajax:

@RequestMapping("/quick")
@ResponseBody
public void save(@RequestBody List<String> userList) throws IOException {
    System.out.println(userList);
}

開放靜態資源;

無法訪問的js檔案等靜態資源是配置
<!--    開放資源的訪問權限:請求為mapping時開放location的靜態資源-->
    <mvc:resources mapping="js/**" location="/js/"></mvc:resources>

這樣也可以:在springmvc找不到資源是交給tomcat處理

<mvc:default-servlet-handler/>

解決低版本tomcat請求資料為中文亂碼問題

如果tomcat版本較低,當配置post請求時,資料為中文會出現亂碼,所以我們通過配置過濾器來解決

<filter>
  <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

引數系結注解@requestParam

當請求的引數名稱和controller方法引數不一致時,就需要通過@requestParam注解顯示

@RequestMapping("/quick")
@ResponseBody
//當我在url中輸入的是name
public void save(@RequestParam("name") String username) throws IOException {
    System.out.println(username);
}

@requestParam注解的引數:

獲得Restful風格的引數

Restful是一種軟體架構風格、設計風格,而不是標準,只是提供一組設計原則和約束條件,主要用于客戶端和服務器互動類的軟體,基于這個風格設計的軟體可以更加簡潔,更有層次等

HTTP協議中四個表示操作方式的動詞:

具體實體:

自定義型別轉換器

SpringMVC實際上已經具備了一些常用的型別轉換器,例如客戶端提交的字串被轉換成int型進行引數設定

但是可能并不完全滿足我們的需求,所以我可以自定義型別轉換器:
開發步驟:

  1. 定義轉換器實作Converter介面

    public class DataConverter implements Converter<String,Date> {
        public Date convert(String dateStr) {
            //將日期字串轉換為日期物件
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = format.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        }
    }
    
  2. 在組態檔中宣告轉換器

    <!--    宣告這個轉換器-->
        <bean id="ConversionService" >
            <property name="converters">
                <list>
                    <bean ></bean>
                </list>
            </property>
        </bean>
    
  3. 在中參考轉換器

    <!--    配置處理器映射器-->
        <mvc:annotation-driven conversion-service="ConversionService"/>
    

    結果:

獲得Servlet的相關API

前面說過,直接在相關方法引數位置注入,就不在贅述

獲取請求頭

  1. 使用@RequestHeader

        @RequestMapping("/quick")
        @ResponseBody
    //    將請求頭的user-agent一行賦值給引數
        public void save(@RequestHeader(value = "https://www.cnblogs.com/rainaftersummert/archive/2023/07/12/User-Agent") String userAgent) throws IOException {
            System.out.println(userAgent);
        }
    
  2. @CookieValue獲得Cookie的值:

    @RequestMapping("/quick")
    @ResponseBody
//    通過cookie的id獲得cookie的值
    public void save(@CookieValue(value = "https://www.cnblogs.com/rainaftersummert/archive/2023/07/12/JSESSIONID") String cookie) throws IOException {
        System.out.println(cookie);
    }

檔案上傳

檔案上傳客戶端要求:

單檔案上傳步驟:

  1. 匯入fileupload和io坐標

    <!--    fileupload-->
        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.1</version>
        </dependency>
    <!--    commons-io-->
        <dependency>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
          <version>2.5</version>
        </dependency>
    
  2. 組態檔上傳決議器

        <bean id="multipartResolver" >
    <!--        上傳檔案總大小最大-->
            <property name="maxUploadSize" value="https://www.cnblogs.com/rainaftersummert/archive/2023/07/12/5242800"/>
    <!--        上傳單個檔案大小最大-->
            <property name="maxUploadSizePerFile" value="https://www.cnblogs.com/rainaftersummert/archive/2023/07/12/5242800"/>
    <!--            生成檔案編碼型別-->
            <property name="defaultEncoding" value="https://www.cnblogs.com/rainaftersummert/archive/2023/07/12/UTF-8"/>
        </bean>
    
  3. 撰寫檔案上傳代碼

        @RequestMapping("/quick")
        @ResponseBody
        //引數名必須與上傳檔案表單的各項name相同
        public void save(String username, MultipartFile uploadFile) throws IOException {
    //        獲得檔案名稱
            String originalFilename = uploadFile.getOriginalFilename();
    //        保存檔案到
            uploadFile.transferTo(new File("D:\\"+originalFilename));
    

我在創建multipartResolver 實體時報500錯誤,后來發現是tomcat中沒有commons-fileupload和commons-io的jar包,需要向tomcat下的lib目錄手動匯入上面那個兩個坐標的包,另外可能jsp不識別el運算式需要在開頭設定 isELIgnored="false"

多檔案上傳同理:

    @RequestMapping("/quick")
    @ResponseBody
    //引數名必須與上傳檔案表單的各項name相同
    public void save(String username, MultipartFile uploadFile1,MultipartFile uploadFile2) throws IOException {
//        獲得檔案名稱
        String originalFilename = uploadFile1.getOriginalFilename();

        String originalFilename2 = uploadFile2.getOriginalFilename();
//        保存檔案到
        uploadFile2.transferTo(new File("D:\\"+originalFilename));
        uploadFile1.transferTo(new File("D:\\"+originalFilename));
    }

當然也可以把引數換成一個陣列

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/557103.html

標籤:其他

上一篇:用Python撰寫網頁自動答題工具,滿分輕松到手,你就是全班最靚的仔!

下一篇:返回列表

標籤雲
其他(162443) Python(38274) JavaScript(25531) Java(18294) C(15241) 區塊鏈(8275) C#(7972) AI(7469) 爪哇(7425) MySQL(7296) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5876) 数组(5741) R(5409) Linux(5347) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4616) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2439) ASP.NET(2404) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) HtmlCss(1998) .NET技术(1987) 功能(1967) Web開發(1951) C++(1942) python-3.x(1918) 弹簧靴(1913) xml(1889) PostgreSQL(1883) .NETCore(1863) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • SpringMVC

    # SpringMVC ## Spring集成web環境 ### 集成步驟 1. 匯入相關的坐標,spring的和web的 ```xml org.springframework spring-context 5.3.6 mysql mysql-connector-java 5.1.32 c3p0 c ......

    uj5u.com 2023-07-13 07:45:16 more
  • 用Python撰寫網頁自動答題工具,滿分輕松到手,你就是全班最靚的仔!

    最近自動答題的外包很多,來給大家分享一下如何用Python來實作自動答題。 好了話不多說,我們開始操作。 首先你需要準備這些 環境使用 Python 3.8 解釋器 Pycharm 編輯器 模塊使用 import requests > 資料請求模塊 pip install requests impo ......

    uj5u.com 2023-07-13 07:44:52 more
  • Java入門12(多執行緒)

    ## 多執行緒 ### 執行緒的實作方式 1. 繼承 Thread 類:一旦繼承了 Thread 類,就不能再繼承其他類了,可拓展性差 2. 實作 Runnable 介面:仍然可以繼承其他類,可拓展性較好 3. 使用執行緒池 #### 繼承Thread 類 ? 不能通過執行緒物件呼叫 run() 方法,需要 ......

    uj5u.com 2023-07-13 07:44:47 more
  • python學習筆記:繼承與超類

    與java類似,繼承的出現是為了提高代碼的重復利用率,避免多次輸入同樣的代碼。而超類就是java中的父類。 # 1.繼承 要指定超類,可在定義類時,在class陳述句中的類名后加上超類名 * 基類就是超類,派生類就是子類 格式 ``` class Dog: # pass class Bobo(Dog) ......

    uj5u.com 2023-07-13 07:43:17 more
  • SpringMVC

    # SpringMVC ## Spring集成web環境 ### 集成步驟 1. 匯入相關的坐標,spring的和web的 ```xml org.springframework spring-context 5.3.6 mysql mysql-connector-java 5.1.32 c3p0 c ......

    uj5u.com 2023-07-13 07:09:00 more
  • C語言:資料結構之單鏈表(三)

    上篇談了談尾插法和頭插法,這篇談談中間插入元素和洗掉。 1、中間插入元素 既然談到了要從中間插入那就得確定插入的位置是否合法了,我總不能鏈表總長為5,但是插入的位置是60,這就不對了。所以得先確定這個鏈表的長度為多少。這個比較簡單,就是在尋找尾部的程序中計數,直到走到最后一個節點。 代碼如下: in ......

    uj5u.com 2023-07-12 09:05:29 more
  • 一文了解 io.LimitedReader型別

    # 1. 引言 `io.LimitedReader` 提供了一個有限的讀取功能,能夠手動設定最多從資料源最多讀取的位元組數。本文我們將從 `io.LimitedReader` 的基本定義出發,講述其基本使用和實作原理,其次,再簡單講述下具體的使用場景,基于此來完成對`io.LimitedReader` ......

    uj5u.com 2023-07-12 09:05:18 more
  • 一文了解 io.LimitedReader型別

    # 1. 引言 `io.LimitedReader` 提供了一個有限的讀取功能,能夠手動設定最多從資料源最多讀取的位元組數。本文我們將從 `io.LimitedReader` 的基本定義出發,講述其基本使用和實作原理,其次,再簡單講述下具體的使用場景,基于此來完成對`io.LimitedReader` ......

    uj5u.com 2023-07-12 09:04:05 more
  • C語言:資料結構之單鏈表(三)

    上篇談了談尾插法和頭插法,這篇談談中間插入元素和洗掉。 1、中間插入元素 既然談到了要從中間插入那就得確定插入的位置是否合法了,我總不能鏈表總長為5,但是插入的位置是60,這就不對了。所以得先確定這個鏈表的長度為多少。這個比較簡單,就是在尋找尾部的程序中計數,直到走到最后一個節點。 代碼如下: in ......

    uj5u.com 2023-07-12 08:58:20 more
  • 【經典爬蟲案例】用Python爬取微博熱搜榜!

    [toc] # 一、爬取目標 您好,我是[@馬哥python說](https://www.zhihu.com/people/13273183132),一名10年程式猿。 本次爬取的目標是: [微博熱搜榜](https://s.weibo.com/top/summary?cate=realtimeho ......

    uj5u.com 2023-07-12 08:08:36 more