@RequestParam 목록 바인딩
다음과 같은 방법으로 폼에서 몇 가지 파라미터를 전송합니다.
myparam[0] : 'myValue1'
myparam[1] : 'myValue2'
myparam[2] : 'myValue3'
otherParam : 'otherValue'
anotherParam : 'anotherValue'
...
다음과 같은 파라미터를 추가하면 컨트롤러 메서드의 모든 파라미터를 얻을 수 있습니다.
public String controllerMethod(@RequestParam Map<String, String> params){
....
}
파라미터 myParam[](다른 파라미터는 제외)를 목록 또는 배열(인덱스 순서를 유지하는 임의의 파라미터)에 바인드하고 싶기 때문에 다음과 같은 구문을 사용해 보았습니다.
public String controllerMethod(@RequestParam(value="myParam") List<String> myParams){
....
}
그리고.
public String controllerMethod(@RequestParam(value="myParam") String[] myParams){
....
}
myParams를 묶는 건 하나도 없어요맵에 값을 추가해도 파라미터를 바인드할 수 없습니다.
public String controllerMethod(@RequestParam(value="myParam") Map<String, String> params){
....
}
목록 속성을 가진 @ModelAttribute로 객체를 작성할 필요 없이 목록 또는 배열에 일부 파라미터를 바인드하는 구문이 있습니까?
고마워요.
아니면 그렇게 할 수도 있습니다.
public String controllerMethod(@RequestParam(value="myParam[]") String[] myParams){
....
}
예를 들어 다음과 같은 폼에서 사용할 수 있습니다.
<input type="checkbox" name="myParam[]" value="myVal1" />
<input type="checkbox" name="myParam[]" value="myVal2" />
이것이 가장 간단한 해결책입니다.
어레이 위치@RequestParam
는 같은 이름의 여러 파라미터를 바인딩하기 위해 사용됩니다.
myparam=myValue1&myparam=myValue2&myparam=myValue3
바인드할 필요가 있는 경우@ModelAttribute
- 스타일 인덱스된 매개 변수, 필요한 경우@ModelAttribute
어쨌든.
바질이 질문 자체에 대해 코멘트에서 말한 것을 구독하는 것은, 만약method = RequestMethod.GET
사용할 수 있습니다.@RequestParam List<String> groupVal
.
다음으로 파라미터 목록을 사용하여 서비스를 호출하는 방법은 다음과 같습니다.
API_URL?groupVal=kkk,ccc,mmm
Donal Fellows가 말한 것을 보완하는 것만으로 List with @RequestParam을 사용할 수 있습니다.
public String controllerMethod(@RequestParam(value="myParam") List<ObjectToParse> myParam){
....
}
도움이 됐으면 좋겠다!
이를 실현하기 위한 한 가지 방법(해킹한 방법으로)은, 다음과 같이 하기 위한 래퍼 클래스를 작성하는 것입니다.List
. 이렇게.
class ListWrapper {
List<String> myList;
// getters and setters
}
컨트롤러 방식 시그니처는 다음과 같습니다.
public String controllerMethod(ListWrapper wrapper) {
....
}
를 사용할 필요가 없습니다.@RequestParam
또는@ModelAttribute
요청에 전달한 컬렉션 이름이 래퍼 클래스의 컬렉션 필드 이름과 일치하는 경우 주석. 이 예에서는 요청 매개 변수가 다음과 같이 표시됩니다.
myList[0] : 'myValue1'
myList[1] : 'myValue2'
myList[2] : 'myValue3'
otherParam : 'otherValue'
anotherParam : 'anotherValue'
Collection을 요청 매개 변수로 받아들일 수 있지만 소비자 측에서는 Collection 항목을 쉼표로 구분된 값으로 전달해야 합니다.
예를 들어 서버 측 API가 다음과 같은 경우:
@PostMapping("/post-topics")
public void handleSubscriptions(@RequestParam("topics") Collection<String> topicStrings) {
topicStrings.forEach(topic -> System.out.println(topic));
}
수집을 다음과 같이 RequestParam으로 RestTemplate에 직접 전달하면 데이터가 파손됩니다.
public void subscribeToTopics() {
List<String> topics = Arrays.asList("first-topic", "second-topic", "third-topic");
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForEntity(
"http://localhost:8088/post-topics?topics={topics}",
null,
ResponseEntity.class,
topics);
}
대신,
public void subscribeToTopics() {
List<String> topicStrings = Arrays.asList("first-topic", "second-topic", "third-topic");
String topics = String.join(",",topicStrings);
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForEntity(
"http://localhost:8088/post-topics?topics={topics}",
null,
ResponseEntity.class,
topics);
}
완전한 예는 여기서 찾을 수 있습니다.이것이 누군가의 두통을 덜어주기를 바랍니다:)
아래와 같이 확인란 전환으로 숨김 필드 값 변경...
HTML:
<input type='hidden' value='Unchecked' id="deleteAll" name='anyName'>
<input type="checkbox" onclick="toggle(this)"/> Delete All
스크립트:
function toggle(obj) {`var $input = $(obj);
if ($input.prop('checked')) {
$('#deleteAll').attr( 'value','Checked');
} else {
$('#deleteAll').attr( 'value','Unchecked');
}
}
언급URL:https://stackoverflow.com/questions/4596351/binding-a-list-in-requestparam
'sourcecode' 카테고리의 다른 글
JDK와 JRE의 차이점은 무엇입니까? (0) | 2022.08.31 |
---|---|
vue-router의 "$router.push()"에 사용자 지정 데이터 전달 (0) | 2022.08.31 |
Java Array List는 특정 인덱스로 대체됩니다. (0) | 2022.08.31 |
함수에서 char*를 반환하는 것과 char[]를 반환하는 것의 차이점은 무엇입니까? (0) | 2022.08.31 |
VueX를 통한 데이터 액세스 (0) | 2022.08.31 |