最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC?Restful風(fēng)格與中文亂碼問(wèn)題解決方案介紹

 更新時(shí)間:2022年10月09日 16:01:21   作者:Decade0712  
Restful就是一個(gè)資源定位及資源操作的風(fēng)格,不是標(biāo)準(zhǔn)也不是協(xié)議,只是一種風(fēng)格,是對(duì)http協(xié)議的詮釋,下面這篇文章主要給大家介紹了關(guān)于SpringMVC對(duì)Restful風(fēng)格支持的相關(guān)資料,需要的朋友可以參考下

基本要點(diǎn)

1、定義

根據(jù)百度百科的定義,RESTFUL是一種網(wǎng)絡(luò)應(yīng)用程序的設(shè)計(jì)風(fēng)格和開(kāi)發(fā)方式

2、傳統(tǒng)方式與Restful風(fēng)格的區(qū)別

在我們學(xué)習(xí)restful風(fēng)格之前,我們請(qǐng)求接口,都是使用http://localhost:8080/controller?method=add這種方式攜帶接口所需要的參數(shù)

而調(diào)用restful風(fēng)格的接口時(shí),我們可以改成http://localhost:8080/controller/add這種類(lèi)型

3、如何使用Restful風(fēng)格

我們通過(guò)一個(gè)代碼demo來(lái)了解一下它的使用方法

首先,我們?cè)O(shè)置當(dāng)前module為web項(xiàng)目,在web.xml中配置一下DispatcherServlet

<?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">
    <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:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

然后我們配置一下springmvc的配置文件springmvc-servlet.xml

這里使用<mvc:annotation-driven/>,會(huì)在Spring MVC上下文中定義一個(gè)org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,它會(huì)像一個(gè)檢查員,對(duì)DispatcherServlet的請(qǐng)求進(jìn)行處理,如果該請(qǐng)求已經(jīng)作了映射,那么會(huì)接著交給后臺(tái)對(duì)應(yīng)的處理程序,如果沒(méi)有作映射,就交給 WEB 應(yīng)用服務(wù)器默認(rèn)的 Servlet 處理,從而找到對(duì)應(yīng)的靜態(tài)資源,只有再找不到資源時(shí)才會(huì)報(bào)錯(cuò)

<?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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 開(kāi)啟自動(dòng)掃描,讓指定包下的注解生效,由IOC容器統(tǒng)一管理 -->
    <context:component-scan base-package="com.decade3.controller"/>
    <!-- 支持mvc注解驅(qū)動(dòng) -->
    <mvc:annotation-driven/>
    <!-- 讓Spring MVC不處理靜態(tài)資源 -->
    <mvc:default-servlet-handler/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

接著我們?cè)赪EB-INF下新建一個(gè)jsp文件夾,在下面新建一個(gè)rest.jsp頁(yè)面,寫(xiě)一個(gè)form表單,點(diǎn)擊按鈕觸發(fā)post方法

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
	${msg}
<form action="/restful/add/spring-/mvc" method="post">
    <input type="submit">
</form>
</body>
</html>

和一個(gè)rest2.jsp頁(yè)面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
	${msg}
</body>
</html>

最后我們寫(xiě)一個(gè)控制器類(lèi)HelloController.java,使用相同的路徑但是使用不同的請(qǐng)求方法

package com.decade3.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/restful")
public class HelloController {
    @RequestMapping(value = "/add/{a}/", method = RequestMethod.GET)
    public String test1(@PathVariable(value = "a") int a, @PathVariable(value = "b")int b, Model model) {
        int result = a + b;
        model.addAttribute("msg", result);
        return "rest";
    }
    @PostMapping(value = "/add/{a}/")
    public String test2(@PathVariable(value = "a") String a, @PathVariable(value = "b")String b, Model model) {
        String result = a + b;
        model.addAttribute("msg", result);
        return "rest2";
    }
}

最后我們啟動(dòng)tomcat驗(yàn)證一下,如果出現(xiàn)報(bào)錯(cuò)請(qǐng)參考我之前的博客SpringMVC執(zhí)行過(guò)程詳細(xì)講解

在地址欄直接輸入url是get請(qǐng)求,所以我們走的是test1方法,頁(yè)面會(huì)跳轉(zhuǎn)到rest.jsp

我們點(diǎn)擊提交按鈕,會(huì)觸發(fā)調(diào)用post方法,走test2,跳轉(zhuǎn)到rest2.jsp

如圖所示,結(jié)果符合我們的預(yù)期

  • 關(guān)于@PathVariable注解

我們可以通過(guò)@PathVariable將url中的參數(shù)與方法上的參數(shù)綁定起來(lái)

  • 關(guān)于請(qǐng)求方法類(lèi)型

我們可以使用POST、DELETE、PUT、GET,使用不同方法對(duì)資源進(jìn)行操作。

分別對(duì)應(yīng) 添加、 刪除、修改、查詢(xún)

我們可以通過(guò)限制method類(lèi)型,來(lái)實(shí)現(xiàn)url請(qǐng)求地址的復(fù)用

如上圖中,我們可以都使用http://localhost:8080/restful/add/a/b的形式,通過(guò)限制方法去調(diào)用不同的接口

  • 控制器類(lèi)中的@PostMapping(value = “/add/{a}/”)和@RequestMapping(value = “/add/{a}/”, method = RequestMethod.POST)是一樣

除了@PostMapping之外,常用的還有

@GetMapping

@PutMapping

@DeleteMapping

@PatchMapping

4、為什么要用restful

我個(gè)人認(rèn)為,使用restful風(fēng)格的接口,使得我們的請(qǐng)求路徑更加簡(jiǎn)潔,而且相同的接口可以通過(guò)限制請(qǐng)求方式實(shí)現(xiàn)不同的功能,增加了代碼的復(fù)用性,最后,restful風(fēng)格的參數(shù)是直接拼接在url上的,我們不需要對(duì)參數(shù)做出解釋?zhuān)嵘税踩?/p>

5、亂碼問(wèn)題

有時(shí)候我們使用post請(qǐng)求時(shí),如果參數(shù)中攜帶中文漢字,可能會(huì)出現(xiàn)解析亂碼的情況

這個(gè)時(shí)候,我們就可以使用spring提供的過(guò)濾器來(lái)解決,我們需要在web.xml中增加如下配置

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

到此這篇關(guān)于SpringMVC Restful風(fēng)格與中文亂碼問(wèn)題解決方案介紹的文章就介紹到這了,更多相關(guān)SpringMVC Restful內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

和平区| 金秀| 黑水县| 梅河口市| 沐川县| 仁化县| 吉安市| 咸丰县| 成都市| 壶关县| 亚东县| 莲花县| 阿拉尔市| 罗江县| 平湖市| 醴陵市| 五家渠市| 呈贡县| 孟连| 卓资县| 凤凰县| 武强县| 伊川县| 毕节市| 兴仁县| 麻阳| 伽师县| 巴青县| 普洱| 湾仔区| 英吉沙县| 临汾市| 报价| 三亚市| 法库县| 任丘市| 横山县| 万山特区| 太白县| 临沂市| 扎囊县|