Controllerにメソッドを用意する
GETでも良いが、以下のようにPOSTを使ってFormで受け取ることができる
@Controller
@RequestMapping("/test")
public class TestController {
@Autowired
private TestService testService;
@ResponseBody
@RequestMapping("/getdata")
public String getData(TestForm form) {
return testService.getData(form.getCd(), form.getName());
}
}
サーバで取得した値をHTMLに返す
$.ajax({
url: $(location).attr("host") + "/test/getdata",
data: $("#TestForm").serialize(),
type: "POST",
dataType: "text"
}).done(function(data) {
$("#data").val(data);
});