SpringMVC mapping rules

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43378945/article/details/102696460
https://www.jianshu.com/p/fbc6953e5af4

Two maps
simultaneously on the annotation @RequestMapping upper classes and methods, column address corresponding to the address of two.

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("one")
public class TestController {
    @RequestMapping("two")
    public String test(){
        return "index";
    }
}

//如上注解后,映射地址为:http://localhost:8080/xx/one/two

Method type accept
@RequestMapping notes in brackets, there is method property as an alternative to the type of price increases accepted

@RequestMapping(value="/getName",method={RequestMethod.GET, RequestMethod.POST})
//如果没有指定method,则默认为所有请求类型都接受

Rule matching parameters
when the response to the request, parameter rule submitted for verification, if it does not regularly arranged, the request will not be accepted

param1 = value1 - This parameter must contain the request and the specified value
param2 - request must contain the parameter values for any
! param3 - not included in the request parameter must

@RequestMapping(value="/getName",method=RequestMethod.GET,params={"id=123","name","!age")
//上述规则定义了,只能响应get请求,并且请求的参数必须包含id=123,必须包含name,不能包含age
//根据上述规则此地址合法:http://localhost:8080/xx?id=123&name=abc

Meaning parameter binding
1. The so-called parameter binding, is how to get value pass over the front page, usually with according to the parameter name (key) to get the value;
Key page biography values bind the values to mapping method the parameters, as shown in the following procedure

//页面端提交请求的程序
$.post("../hc.v",
    {
        name : "shoji",
        price : "8888"
    },
    function(d) {
        alert(d);
    }
)
//后台响应上面ajax的post请求的代码
//通过两段代码里“name”和“price”的相同,把“shouji”和“8888”传到hc方法里
//问号传值的方式同样适用    ?name=shoji&price=8888
...
@RequestMapping("hc")
public String hc(String name,String price){
    return "test";
}
...

2. PO standard to the binding properties
js code page requests submitted by end supra

//新建一个标准的PO,同时当前类的属性名应该跟前台代码里的KEY对应上 public class PO{ private String name;//和key值一样 private Stirng price;//和key值一样 //省略各自的set get } //后台响应上面ajax的post请求的代码 //通过PO里的“name”和“price”属性名和前台js代码里的key相同,把“shouji”和“8888”传到hc方法里 //问号传值的方式同样适用 ?name=shoji&price=8888作者:_NineSun旭_
链接:https://www.jianshu.com/p/fbc6953e5af4
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
...
@RequestMapping("hc")
public String hc(PO po){
    //po.getName() 取出shoujie
    //po.getPrice() 取出8888
    return "test";
}
...

3. Notes @RequestParam to bind
the page to submit a request end js code above

//后台响应上面ajax的post请求的代码 //通过注解里的“name”和“price”参数名和前台js代码里的key相同,把“shouji”和“8888”传到hc方法里 //问号传值的方式同样适用 ?name=shoji&price=8888
...
@RequestMapping("hc")
public String hc(@RequestParam("name") String p1,@RequestParam("price") String p2){
    //p1 取出shoujie
    //p2 取出8888
    return "test";
}
...

4. Annotation @PathVariable by
the present value of the notes to pass to the background program, the address in the binding
address requested page end becomes submitted: dianhua / name / shoji / price / 8888

//后台响应上面ajax的post请求的代码
//@RequestMapping注解的映射改变,地址里值的部分用{}括起来
//在方法的参数上使用注解@PathVariable
//通过PathVariable注解后面的“idvalue”和“pricevalue”和RequestMapping注解里{}部分相同,把“shouji”和“8888”传到hc方法里

@RequestMapping("dianhua/name/{idvalue}/price/{pricevalue}")
public String hc(@PathVariable String idvalue,@PathVariable String pricevalue){
    //idvalue 取出shoujie
    //pricevalue 取出8888
    return "test";
}
...

Mapping method return type
1. Return ModelAndView

<!-- springmvc配置文件代码片段 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
</bean>
@RequestMapping("/szw/jintiancailai") public ModelAndView ccc(String namee) { List list = new ArrayList(); list.add("zhiweizhi"); list.add("luchang"); System.out.println("hello name=" + namee); ModelAndView mav = new ModelAndView(); mav.addObject("myList", list); mav.addObject("mySize", "2"); mav.setViewName("first"); //first是一个页面文件的名字 //在springmvc的配置文件指定页面文件所在的路径是/WEB-INF/views/ //在springmvc的配置文件指定页面文件的类型是.jsp //我们ModelAndView所展示的页面会做如下解析 prefix+setViewName+suffix //即 /WEB-INF/views/first.jsp return mav;

ModelAndView springmvc framework is a file wrapper interface jsp class, and commonly used methods addObject setViewName
addObject has two parameters, the type of Object type, the first parameter is the key, the second parameter is the value; the main purpose, the java after the data is brought to the foreground in the jsp page
setViewName have a parameter type is String; jsp parameter is the name of the file; the purpose is to tell ModelAndView show which jsp page document.
2. The return value is a String
return value, only a string

public String ddd() {
    return "first";
}
//这样返回同上面程序注释的过程

and redirect forward before returning the string value, and there is a colon between the forward and strings and redirect

public String eee() { return "redirect:../first.jsp"; } public String fff() { return "forward:../first.jsp"; } //这样返回,我们转去的地址不在被springmvc管理,需要转去的地址能直接在地址栏访问

@ResponseBody notes
when the return value is a String, and this method has top notes, the returned string will not be resolved to springmvc of view (ie jsp file), the string will be demonstrated directly in the browser.

@RequestMapping("/test")
@ResponseBody
public String hhh() {
    return "first";
}
//first将被输出到浏览器里

Return void
requires human in the parameter list of the method, the join request and the response of these two parameters, all operations rely on these two parameters to complete, is very similar to the standard Servlet.
Ajax and Controller of interaction
incoming key-value key to output JSON
1. in ajax submit a request, the data format is transmitted to the background of kv

//$.ajax $.post $.get 都是jquery的请求,需要引入jquery.js
//$.post和$.get都是$.ajax的简写方式
    $.ajax({
        type : "post",
        url : "../tm.v",
        contentType:"application/json; charset=utf-8",//请求成功后,后台返回的数据格式,即success : function(r)里“r”的格式
        data : "name=shoji&price=8888",//此处就是标题中提到的传入key-value键值对
        success : function(r) {
            //r 直接就是jsonObject
            alert(r.name);
        }
    })

2. In the process of the return value in the controller, the data format is transmitted to front json

@RequestMapping("tm")
    @ResponseBody
    public BYQ tm(BYQ byq2){
        System.out.println(byq2.getName());
        System.out.println(byq2.getPrice());
        return byq2;
    }

Incoming json, json output
1. In ajax submit a request, the data format is transmitted to the background json string

$.ajax({
        type : "post",
        url : "../tm.v",
        contentType:"application/json; charset=utf-8",
        data:'{"name":"shouji","price":"8888"}',//此处就是标题中提到的传入的json格式串
        success : function(r) {
            //r 直接就是jsonObject
            //如果返回的r是字符串,在调用r.price之前需要把字符串转为json对象,var jsonObject = JSON.parse(r);
            alert(r.price);
        }
    })

2, the return value of the method of the controller, the data format is transmitted to the front json, and the received parameters in the method are string json

//@ResponseBody注解和方法的返回值被置为BYQ,是为了返回值是json格式
//@RequestBody注解和方法的参数是BYQ类型,是为了接收前台提交过了的json格式
    @RequestMapping("tm")
    @ResponseBody
    public BYQ tm(@RequestBody BYQ byq2){
        System.out.println(byq2.getName());
        System.out.println(byq2.getPrice());
        return byq2;
    }

restful

Introduction
reflected in the browser address; each address represents a background resource (instance), the address can not appear in the verb, the specific-function operation method to distinguish the type of submission.
Mapping Template
traditional way: http: // localhost: 8080 / ssm / updateuser id = 123?

restful: http: // localhost: 8080 / ssm / user / 123 do not see through this address additions and deletions, correspond to the following specific operation.
Submission method corresponding to the type (value method of)
GET - inquiry

post - Modify

put - Add

delete - Delete
springmvc in support of restful
address to support the rest of the style by notes @PathVariable, see

above springmvc4 version, @RestController provide annotation support.
When mapped SpringMVC / address need not be mapped approach
when mapped to /, i.e. springmvc introduced in web.xml, url-pattern value tag /, i.e. all addresses are intercepted at mvc site.

...
  <servlet>
    <servlet-name>springmvcservlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:mymvc.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvcservlet</servlet-name>
    <url-pattern>/</url-pattern><!-- 被标识为/ -->
  </servlet-mapping>
<!-- 地址被全部拦截后,出现的问题是,.js、.css、图片文件等等静态资源文件都被拦截了,不能直接访问 -->
  ...

One way to solve the static resource intercepted, add the following code SpringMVC profile

<!-- 地址里含有js和img的,都不被springmvc拦截 -->
    <mvc:resources location="/js/" mapping="/js/**"/>
    <mvc:resources location="/img/" mapping="/img/**"/>

Guess you like

Origin blog.csdn.net/qq_43378945/article/details/102696460