Use @RequestMapping annotation | SpringMVC framework


A, RequestMapping role

Role: @RequestMapping annotation can be done url mapping requests.

It can act on the method.

	@RequestMapping("/addUI")
	public String addUI(Model model){
		return "addUI";
	}

It may also act on the Java class for refinement request path.

Use the annotations on the Java class path shall add a user before access path / addUI.

package com.gql.springmvc02;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserController {
	
	
	public UserController() {
		super();
		System.out.println("构造函数...");
	}

	//跳转到添加页面
	@RequestMapping("/addUI")
	public String addUI(Model model){
		//model.addAttribute("msg", "双笙");
		return "addUI";//forward写不写都是转发,redirect代表重定向.
	}
}

Two, RequestMapping property

Attributes Explanation
value Default properties, the actual address assignment request may be a specific address, dynamic access RESTful, using regular setting
method method specifies the type of request, into GET, POST, PUT, DELETE, etc.
params Request specified only certain parameter values ​​must be included so that the processing method, refinement request.
headers Specifies the request must contain certain specified header value, in order for this method to process the request.
consumes Specifying process submission type (Content-Type) request
produces Specify the content type of return, only when the head (the Accept) type request comprises a request to return the specified type only.

1.method property

After using the following method specify the type of the request, the request must be specified manner.

@RequestMapping(value="/user",method=RequestMethod.POST)

2.params property

Params uses the following attributes, request must be spliced on a name attribute and the age attribute is not equal to 20, for example:
http://localhost:8080/SpringMVC01/user/addUI.do?name=%22%E5%91%A8%E5%86%AC%E9%9B%A8%22&age=21can be accessed.

@RequestMapping(value="/user",method=RequestMethod.POST,params={"name","age!=20"})

3.headers property

The following RequestMapping specified headers attribute, only the specified request header can access.

@RequestMapping(value="/user",method=RequestMethod.GET,params={"name","age!=20"},headers={"Accept-Language=zh-CN,zh;q=0.9"})
Published 418 original articles · won praise 1088 · Views 240,000 +

Guess you like

Origin blog.csdn.net/weixin_43691058/article/details/104354630