博客
关于我
SpringMVC---使用
阅读量:333 次
发布时间: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/

    你可能感兴趣的文章
    php mysql procedure获取多个结果集
    查看>>
    php mysql query 行数,PHP和MySQL:返回的行数
    查看>>
    php mysql session_php使用MySQL保存session会话
    查看>>
    PHP mysql_real_escape_string() 函数防SQL注入
    查看>>
    php mysql优化方法_MySQL优化常用方法
    查看>>
    PHP OAuth 2.0 Server
    查看>>
    php odbc驱动,php常用ODBC函数集(详细)
    查看>>
    php openssl aes ecb,php openssl_encrypt AES-128-ECB iOS
    查看>>
    php paypal rest api,PayPal REST API指定网络配置文件PHP
    查看>>
    php pcntl 多进程学习
    查看>>
    PHP pcntl_fork不能在web服务器中使用的变通方法
    查看>>
    php private ,public protected三者的区别
    查看>>
    php PSR规范
    查看>>
    php rand() 重复,array_rand()函数从另外一个数组中随机取得的一定数量的数组的元素是否会重复?...
    查看>>
    php redis pub/sub(Publish/Subscribe,发布/订阅的信息系统)之基本使用
    查看>>
    php redis 集群扩展类文件
    查看>>
    php redis(2)
    查看>>
    PHP Redis分布式锁
    查看>>
    php redis的应用
    查看>>
    php rss,如何用PHP编写RSS
    查看>>