Я хочу использовать формат select2 ajax.
вот мой код.
DAO
public interface SearchUserDAO {
public List<Map<String, Object>> nationalityList();
Mapper
<select id="nationalityList" resultType="map">
SELECT nationality_num, nationality_en_name
FROM nationality_dtb
</select>
Услуги
public class SearchService {
@Autowired
private SearchUserDAO dao;
public List<Map<String, Object>> getInterestList(){
return dao.interestList();
}
Контроллер
@Controller
@RequestMapping("/search")
public class SearchController {
@Autowired
private SearchService service;
@RequestMapping("/home")
public String search(Model model, @RequestParam(value= "page", defaultValue = "1") int page) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("page", page); // not for select2
model.addAllAttributes(service.getViewData(params));
Gson gson = new Gson();
String nationality = gson.toJson(service.getNationalityList());
model.addAttribute("nationality", nationality); // my data
return "search/main";
}
}
1017 * JSP *
<div class="form-group">
<label class="col-sm-3 control-label" for="id_city">거주 도시</label>
<div class="col-sm-9">
<select class="form-control select-city" name="city">
<option></option>
</select>
<script>
$(document).ready(function(){
$(".select-city").select2({
allowClear: true,
ajax:{
url: "/search/home",
dataType: 'json',
delay: 250,
data: {"id":"NATIONALITY_NUM", "text":"NATIONALITY_EN_NAME"}
},
placeholder: "선택"
});
});
</script>
мои данные выглядят как
[
{"NATIONALITY_EN_NAME":"Afghanistan","NATIONALITY_NUM":1},
{"NATIONALITY_EN_NAME":"Aland Islands","NATIONALITY_NUM":2},
{"NATIONALITY_EN_NAME":"Albania","NATIONALITY_NUM":3},
{"NATIONALITY_EN_NAME":"Algeria","NATIONALITY_NUM":4},
{"NATIONALITY_EN_NAME":"American Samoa","NATIONALITY_NUM":5},
{"NATIONALITY_EN_NAME":"Andorra","NATIONALITY_NUM":6},
{"NATIONALITY_EN_NAME":"Angola","NATIONALITY_NUM":7},
{"NATIONALITY_EN_NAME":"Anguilla","NATIONALITY_NUM":8},
{"NATIONALITY_EN_NAME":"Antarctica","NATIONALITY_NUM":9},
{"NATIONALITY_EN_NAME":"Antigua and Barbuda","NATIONALITY_NUM":10},
…
…
…
{"NATIONALITY_EN_NAME":"Uruguay","NATIONALITY_NUM":242},
{"NATIONALITY_EN_NAME":"Uzbekistan","NATIONALITY_NUM":243},
{"NATIONALITY_EN_NAME":"Vanuatu","NATIONALITY_NUM":244},
{"NATIONALITY_EN_NAME":"Vatican","NATIONALITY_NUM":245},
{"NATIONALITY_EN_NAME":"Venezuela","NATIONALITY_NUM":246},
{"NATIONALITY_EN_NAME":"Vietnam","NATIONALITY_NUM":247},
{"NATIONALITY_EN_NAME":"Wallis and Futuna","NATIONALITY_NUM":248},
{"NATIONALITY_EN_NAME":"Western Sahara","NATIONALITY_NUM":249},
{"NATIONALITY_EN_NAME":"Yemen","NATIONALITY_NUM":250},
{"NATIONALITY_EN_NAME":"Zambia","NATIONALITY_NUM":251},
{"NATIONALITY_EN_NAME":"Zimbabwe","NATIONALITY_NUM":252}
]
Пожалуйста, помогите мне.
Я не знаю код AJAX