博客
关于我
SpringMVC---使用
阅读量:324 次
发布时间:2019-03-04

本文共 2229 字,大约阅读时间需要 7 分钟。

关键词解析

@RequestMapping

在Spring MVC中,@RequestMapping注解用于定义控制器方法的URL映射。通过在方法或类中添加该注解,可以将浏览器发送的请求 URL 与控制器中的方法相关联。例如:

public class TestController {    @RequestMapping("/one")    public String handleRequest() {        return "success";    }}

当访问 http://localhost:8080/one 时,会调用 handleRequest 方法,返回的字符串会被视图解析器解析并跳转到相应的页面。

@ResponseBody

@ResponseBody注解用于将方法返回的数据直接作为HTTP响应体发送到客户端,而不是映射到视图解析器。例如:

public class TestController {    @ResponseBody    @RequestMapping("/api/data")    public Map
getData() { return new HashMap<>(); }}

如果没有使用@ResponseBody,返回的数据会被视图解析器处理,通常会转换为视图文件。


实现转发和重定向

在Spring MVC中实现转发和重定向有两种常用方式:redirectforward

  • redirect:用于重定向,返回一个新的URL。例如:
  • public class TestController {    @RequestMapping("/test-redirect")    public String redirectAction() {        return "redirect:/other-url";    }}
    1. forward:用于转发,会将请求转发到指定的URL。例如:
    2. public class TestController {    @RequestMapping("/test-forward")    public String forwardAction() {        return "forward:/another-url";    }}

      需要注意的是,forward 返回的字符串会被视图解析器拼接到当前请求的URL上。


      没有视图解析器的情况

      如果没有启用视图解析器,可以直接在方法返回字符串或路径来实现转发或重定向。例如:

      public class TestController {    @RequestMapping("/test")    public String forward() {        return "/path/to/forward.jsp";    }}

      默认情况下,返回字符串会被视图解析器处理,因此需要禁用解析器才能直接返回路径。


      数据处理

      传递参数

      如果需要将前端传递的数据传递给控制器,可以使用@ModelAttribute 或直接在方法参数中定义。例如:

      public class UserController {    @RequestMapping("/user")    public String showUser(@ModelAttribute User user) {        System.out.println("显示用户:" + user.getName());        return "user-page";    }}

      User对象需要有相应的字段(如 nameage),前端需要通过查询参数传递这些数据。

      提交域名名称与处理方法参数不一致

      如果前端提交的域名名称与方法参数名称不一致,可以使用@RequestParam注解指定域名字段名。例如:

      public class UserController {    @RequestMapping("/user")    public String showUser(@RequestParam("username") String name) {        System.out.println("用户名:" + name);        return "user-page";    }}

      乱码问题

      为了解决中文乱码问题,可以在web.xml中配置Spring提供的CharacterEncodingFilter。例如:

      CharacterEncodingFilter
      org.springframework.web.filter.CharacterEncodingFilter
      encoding
      UTF-8
      CharacterEncodingFilter
      /*

      通过以上配置,可以确保请求参数和响应内容都使用UTF-8编码。

    转载地址:http://crlq.baihongyu.com/

    你可能感兴趣的文章
    npm包管理深度探索:从基础到进阶全面教程!
    查看>>
    npm升级以及使用淘宝npm镜像
    查看>>
    npm发布包--所遇到的问题
    查看>>
    npm发布自己的组件UI包(详细步骤,图文并茂)
    查看>>
    npm和package.json那些不为常人所知的小秘密
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm如何清空缓存并重新打包?
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错TypeError: this.getOptions is not a function
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>