1、RequestMapping
1.1 Role
- Action indicates the first stage based on the access directory;
- Action represents the second level of access to the directory on the method.
Note: The path can not write / presentation applications start at the root
1.2 Properties
- url given request path: path
- value: value attribute and path attribute is the same
- method: given request of the method
- params: Specifies restriction request condition parameter
- Request header request sent must be included: headers
1.3, RequestMapping use
/**
* RequestMapping注解
* @return
*/
@RequestMapping(value = "/testRequestMapping",params = {"username=heihei"},headers = {"Accept"})
public String testRequestMapping() {
System.out.println("测试RequestMapping注解...");
return "success";
}
2、RequestParam
2.1 Role
- Parameter specifies the name of the transfer request to the controller of the assigned parameter, parameter names commonly used in the solution and the request parameter name inconsistencies.
2.2 Properties
- value: Request parameter name
- equired: Request parameter whether this parameter must be provided, the default value is true, it must be provided
2.3, RequestParam use annotations
/**
* RequestParam注解:
* @param username
* @return
*/
@RequestMapping("/testRequestParam")
public String testRequestParam(@RequestParam(name="name") String username){
System.out.println(username);
return "success";
}
3、RequestBody
3.1 Role
- A request for acquiring the content thereof (Note: get methods can not);
- Converting the character string into json JavaBean object.
3.2 Properties
- equired: Is it necessary to have the request body, the default value is true
3.3, RequestBody use annotations
/**
* RequestBody注解:获取到请求体的内容
* @param body
* @return
*/
@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestBody String body){
System.out.println(body);//username=zhangsan&password=123
return "success";
}
4, PathVariable
4.1 Role
- Binding has the url placeholder.
4.2 Properties
- value: Specifies the name url placeholder
4.3, PathVariable use annotations
/**
* PathVariable注解:
* @return
*/
@RequestMapping(value="/testPathVariable/{sid}")
public String testPathVariable(@PathVariable(name="sid") String id){
System.out.println(id);
return "success";
}
5、RequestHeader
5.1 Role
- Gets the value specified in the request header
5.2 Properties
- value: request header name
5.3, RequestHeader use annotations
/**
* RequestHeader注解:获取请求头的值
* @param header
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping(value="/testRequestHeader")
public String testRequestHeader(@RequestHeader(value="Accept") String header, HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println(header);
return "success";
// response.sendRedirect(request.getContextPath()+"/anno/testCookieValue");
//return "redirect:/param.jsp";
}
6, Cookie Value
6.1 Role
- Gets the value for the cookie name
6.2 Properties
- value: cookie name
6.3, CookieValue use annotations
/**
* CookieValue注解:获取Cookie的值
* @param cookieValue
* @return
*/
@RequestMapping(value="/testCookieValue")
public String testCookieValue(@CookieValue(value="JSESSIONID") String cookieValue){
System.out.println(cookieValue);
return "success";
}
7, ModelAttribute
7.1 Role
- Acting on the method that is the current front-line approach uses the method in the controller;
- Acting on the parameters, gets the specified data to the parameter assignment.
7.2, ModelAttribute use
/**
* ModelAttribute注解:作用在方法时,表示当前方法会在控制器方法执行前执行。
* @param username
* @param map
*/
@ModelAttribute
public void showUser(String username, Map<String,UserInfo> map){
UserInfo userInfo = new UserInfo();
userInfo.setUsername(username);
userInfo.setPassword("123");
userInfo.setDate(new Date());
map.put("userInfo",userInfo);
}
8、SessionAttribute
8.1 Role
- Parameters for performing the control method of sharing among multiple
8.2 Properties
- value: Specifies the name of the property into
8.3, SessionAttribute use annotations
package com.wedu.springmvc.web;
import com.wedu.springmvc.domain.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
/**
* springmvc常用注解的使用
*/
@Controller
@RequestMapping("/annotation")
@SessionAttributes(value = {"msg"})
public class AnnoController {
/**
* SessionAttributes注解:用于多次执行控制器方法间的参数共享
* 向session中存储数据
* @return
*/
@RequestMapping(value="/testSessionAttributes")
public String testSessionAttributes(Model model){
// 底层会存储到request域对象中
model.addAttribute("msg","测试OK");
return "success";
}
/**
* 从session中获取值
* @param modelMap
* @return
*/
@RequestMapping(value="/getSessionAttributes")
public String getSessionAttributes(ModelMap modelMap){
String msg = (String) modelMap.get("msg");
System.out.println(msg);
return "success";
}
/**
* 将session中的值清除
* @param status
* @return
*/
@RequestMapping(value="/delSessionAttributes")
public String delSessionAttributes(SessionStatus status){
System.out.println("将session中的值清除...");
status.setComplete();
return "success";
}
}