SpringMVC-04-RestFul风格及返回数据乱码问题

慕阳文
2023-12-01

1.编写controller

package yyf.controller;//package com.yyf.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;

@Controller
public class RestFulController {

//  方式一  @RequestMapping (value = "/add/{a}/{b}",method = RequestMethod.GET)
   // 方式二
        @PostMapping("/add/{a}/{b}")
    public String test(@PathVariable int a, @PathVariable int b, Model model){
        int res=a+b;
        model.addAttribute ("msg","结果为"+res);
        return "test";
    }
}

2.乱码问题

<!--    乱码问题-->
    <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>
 类似资料: