Spring Path Parameter 예제

■ 참고 : Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable
 
 
 

■ Spring Controller

package net.iotinfra.pilot.angularjs;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PathParamController {

  @RequestMapping(
    value="/student/byname/{userName}/{userAge}",
    method=RequestMethod.GET)
  @ResponseBody
  public String  text(
    @PathVariable("userName") final String userName,
    @PathVariable("userAge") final int userAge,
    @RequestParam("studentId") String studentId) 
        throws Exception {

      System.out.println( ">>> userName : " + userName);
      System.out.println( ">>> userAge : " + userAge);
      System.out.println( ">>> studentId : " + studentId);
		
      return "Hello, "+userName;
  }
}

 
 
 

■ jquery 클라이언트

<!DOCTYPE html>
<html ng-app="myApp">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8">
<head>
<title></title>
<script src='/js/jquery/jquery-1.11.0.min.js'></script>
<script>
$(document).ready(function() {

  var userName= "nextName";
  var userAge= 9;
  var userId 	= "nextId";
	
  $.ajax({
    url       : '/student/byname/'+userName+'/'+userAge,
    type      : 'GET',
    data      : 'studentId='+userId,
      success   : function(data) {
        console.log('return string : ' + data);
	$("#log").append(data);
      },
      error : function() {
        console.log('error');
      }
  });
 
});
</script>
</head>
<body>
  <h1>
    Spring Path Parameter<br/>
  </h1>
  <hr/>
  <div id="log"></div>
</body>
</html>

 
 
 

■ dispatcher-servlet.xml (Spring 설정파일)

...
<context:component-scan base-package="net.iotinfra.pilot.angularjs"/>
<context:annotation-config/>
...

 
 
 

■ 크롬 브라우져 실행 결과

 
 
 

■ 이클립스 콘솔 실행 결과

 
 
 

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다