Spring MVC 参数绑定-基本类型参数 / 以及Spring mvc 过滤器解决字符乱码问题

addbook.jsp

<%--
  Created by IntelliJ IDEA.
  User: 管 理 员
  Date: 2021年11月03日 0003
  Time: 13:12:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>addbook</title>
</head>
<body>
<h1>添加图书</h1>
<form action="/book/addbook" method="post">
    <table>
        <tr>
            <td>图书名称</td>
            <td><input type="text" name="bookname"></td>
        </tr>
        <tr>
            <td>图书作者</td>
            <td><input type="text" name="author"></td>
        </tr>
        <tr>
            <td>图书价格</td>
            <td><input type="text" name="price"></td>
        </tr>
        <tr>
            <td><input type="submit" value="添加"></td>
        </tr>
    </table>
</form>
</body>
</html>

BookController.java

package org.javaboy.springmvc02.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.naming.Name;

/**
 * @author 邓雪松 (づ ̄ 3 ̄)づ)
 * @create 2021-11-02-19-13
 */
@Controller
@RequestMapping("/book")
public class BookController {
    //http://localhost:8080/book/getbook
    @RequestMapping("/getbook")
    public ModelAndView getBook(){
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("name","BookController");
        return mv;
    }

    @GetMapping("/book")
    public String book(){
        return "addbook";
    }

    @PostMapping(value = "/addbook",produces = "text/html;charset=utf-8")
    @ResponseBody//为了防止找视图
    public String addBook(@RequestParam("bookname") String name, String author, Double price){
        System.out.println("name = "+name);
        return name+">>>"+author+">>>"+price;
    }
}

web.xml spring-mvc过滤器解决乱码问题

<?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_3_1.xsd"
         version="3.1">
    <!--在web.xml中加载spring和springmvc的配置文件-->
    <!--先加载spring的-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--然后是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:spring-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--springmvc自带的过滤器解决乱码问题-->
    <filter>
        <filter-name>encodingFilter</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>
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

运行效果图

posted @ 2021-11-03 14:14  ╰(‵□′)╯  阅读(37)  评论(0编辑  收藏  举报