关于Spring MVC注解

关于Spring MVC注解

  • @RequestMapping
    Spring MVC 通过 @RequestMapping 注解将 URL 请求与业务方法进行映射,在 Handler 的类定义处以及方法定义处都可以添加 @RequestMapping ,在类定义处添加,相当于客户端多了一层访问路径。
  • @Controller
    @Controller 在类定义处添加,将该类交个 IoC 容器来管理(结合 springmvc.xml 的自动扫描配置使用),同时使其成为一个控制器,可以接收客户端请求。
package com.zyc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/hello")//表示指定请求的地址
public class HelloHandler {
    
    

    @RequestMapping("/index")
    public String index(){
    
    
        System.out.println("执行了index...");
        return "index";
    }
}

关于@RequestMapping 相关参数
1、value:指定 URL 请求的实际地址,是 @RequestMapping 的默认值

@RequestMapping("/index")
public String index(){
    
    
    System.out.println("执行了index...");
    return "index";
}

上述代码等同于:

@RequestMapping(value="/index")
public String index(){
    
    
    System.out.println("执行了index...");
    return "index";
}

2、method:指定请求的 method 类型,GET、POST、PUT、DELETE

@RequestMapping(value = "/index",method = RequestMethod.GET)
public String index(){
    
    
    System.out.println("执行了index...");
    return "index";

上述代码表示index方法只能接收get请求
3、params:指定请求中必须包含某些参数,否则无法调用该方法。

@RequestMapping(value = "/index",method = RequestMethod.GET,params = {
    
    "name","id=10"})
public String index(){
    
    
    System.out.println("执行了index...");
    return "index";

上述代码表示index方法只能接收get请求,且该请求中必须包含name和id两个参数,同时id值必须为10.

关于参数绑定,在形参列表中通过添加 @RequestParam 注解完成 HTTP 请求参数与业务方法形参的映射。

@RequestMapping(value = "/index",method = RequestMethod.GET,params = {
    
    "name","id=10"})
public String index(@RequestParam("name") String str,@RequestParam("id") int age){
    
    
    System.out.println(str);
    System.out.println(age);
    System.out.println("执行了index...");
    return "index";
}

上述代码表示将请求的参数 name 和 id 分别赋给了形参 str 和 age ,同时自动完成了数据类型转换,将 “10” 转为了 int 类型的 10,再赋给 age,这些工作都是由 HandlerAdapter 来完成的。

当然,Spring MVC 也支持 RESTful 风格的 URL:
传统类型:http://localhost:8080/hello/index?name=rgz&id=10
REST:http://localhost:8080/hello/index/rgz/10

@RequestMapping("/rest/{name}/{id}")
public String rest(@PathVariable("name") String name,@PathVariable("id") int id){
    
    
    System.out.println(name);
    System.out.println(id);
    return "index";
}

上述代码通过 @PathVariable 注解完成请求参数与形参的映射。

- 映射 Cookie
Spring MVC 通过映射可以直接在业务方法中获取 Cookie 的值。

@RequestMapping("/cookie")
public String cookie(@CookieValue(value = "JSESSIONID") String sessionId){
    
    
    System.out.println(sessionId);
    return "index";
}

- 使用 JavaBean 绑定参数
Spring MVC 会根据请求参数名和 JavaBean 属性名进行自动匹配,自动为对象填充属性值,同时支持及联属性。

package com.southwind.entity;

import lombok.Data;

@Data
public class Address {
    
    
    private String value;
}
package com.southwind.entity;

import lombok.Data;

@Data
public class User {
    
    
    private long id;
    private String name;
    private Address address;
}

JSP

<%--
  Created by IntelliJ IDEA.
  User: southwind
  Date: 2019-03-13
  Time: 15:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="/hello/save" method="post">
        用户id:<input type="text" name="id"/><br/>
        用户名:<input type="text" name="name"/><br/>
        用户地址:<input type="text" name="address.value"/><br/>
        <input type="submit" value="注册"/>
    </form>
</body>
</html>
@RequestMapping(value = "/save",method = RequestMethod.POST)
public String save(User user){
    
    
    System.out.println(user);
    return "index";
}

关于出现中文乱码问题
只需在 web.xml 添加 Spring MVC 自带的过滤器即可。

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

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

- JSP 页面的转发和重定向:
1、转发

@RequestMapping("/forward")
public String forward(){
    
    
    return "forward:/index.jsp";
    //        return "index";
}

2、重定向

@RequestMapping("/redirect")
public String redirect(){
    
    
    return "redirect:/index.jsp";
}

猜你喜欢

转载自blog.csdn.net/weixin_47723535/article/details/108953294