SpringMVC 의 경로 매개 변수 와 URL 매개 변수 인 스 턴 스 기반

1.SpringMVC 의 경로 매개 변 수 는 경로 에 파 라 메 터 를 추가 하여 의사 정적 을 실현 하 는 것 이 좋 습 니 다.
2.경로 매개 변수 실현 방식(하나의 Controller 방법)

@RequestMapping(value="/page/{name}/{age}",method=RequestMethod.GET)
public String getName(ModelMap map,@PathVariable("name") String name,@PathVariable("age") int age)
{
  map.addAttribute("name",name);
  map.addAttribute("age",age);
  return "name";
}
3.name.jsp 파일 만 들 기

<%@page pageEncoding="UTF-8"%>
<html>
<head>
  <meta charset="UTF-8">
  <title>test</title>
</head>
<body>
<div>
    :${name}<br/>
    :${age}
</div>
</body>
</html>
4.브 라 우 저 에서 이 controller 를 요청 합 니 다.
http://localhost:8080/page/xiaoming/18
주의해 야 할 것 은 제 가 사용 하 는 편집 기 는 IDEA 플래그 십 버 전 입 니 다.

5.contrller 에서 요청 매개 변 수 를 받 아들 이 는 실현(contrller)

@RequestMapping(value="/result",method=RequestMethod.GET)
public String resultParam(ModelMap map,@RequestParam String name,@RequestParam int age)
{
  map.addAttribute("name",name);
  map.addAttribute("age",age);
  return "result";
}
6.result.jsp 파일 만 들 기

<%@page pageEncoding="UTF-8">
<html>
<head>
  <meta charset="UTF-8">
  <title>  </title>
</head>
<body>
  :${name}<br/>
  :${age}
</body>
</html>
6.브 라 우 저 에서 이 controller 요청
http://localhost:8080/result?name=xiaoming&age=20

보충:spring mvc 의 선택 가능 한 경로 파라미터
spring mvc 에서@PathVariable 을 주석 하면 경로 인 자 를 얻 을 수 있 지만 경로 인 자 를 선택 하고 싶다 면?

  @GetMapping({"/get/{offset}/{count}","/get/{offset}","/get/{offset}","/get"})
  public void getGoods(@PathVariable(required = false) Integer offset,@PathVariable(required = false) Integer count){
    System.out.println("offset:"+offset+"
count:"+count+"
"); }
이 예 에서 offset 과 count 는 모두 선택 할 수 있 지만 count 가 존재 할 때 offset 는 반드시 존재 해 야 합 니 다.
이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.만약 잘못 이 있 거나 완전히 고려 하지 않 은 부분 이 있다 면 아낌없이 가르침 을 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기