폼 데이터로 봄 mvc에서 컨트롤러를 호출하고 있습니다.
저장하기 전에 ID가 특정 범위인지 확인합니다. ID가 범위 내에 있지 않으면 같은 페이지에라는 메시지를 표시해야합니다 The id selected is out of Range, please select another id within range
.
인터넷에서 샘플을 찾았는데 문제가 발생하면 실패 jsp로 리디렉션 할 수 있습니다. 하지만 내 경우에는 어떻게해야합니까?
@RequestMapping(value = "/sendMessage")
public String sendMessage(@ModelAttribute("message") Message message,
final HttpServletRequest request) {
boolean check = userLoginService.checkForRange(message.getUserLogin());
if(!check){
return ""; //What Should I do here??????
}
}
간단한 접근 방식은 오류 메시지를 모델 속성으로 추가하는 것입니다.
@RequestMapping(value = "/sendMessage")
public String sendMessage(@ModelAttribute("message") Message message,
final HttpServletRequest request, Model model) {
boolean check = userLoginService.checkForRange(message.getUserLogin());
if(!check){
model.addAttribute("error", "The id selected is out of Range, please select another id within range");
return "yourFormViewName";
}
}
그러면 jsp가 "error"속성이있는 경우 표시 할 수 있습니다.
<c:if test="${not empty error}">
Error: ${error}
</c:if>
다음은 ajax에 대한 대략적인 테스트되지 않은 검증 구현입니다. JQuery가 가정했습니다.
적중 할 ajax에 대한 요청 매핑을 추가합니다.
@RequestMapping("/validate")
@ResponseBody
public String validateRange(@RequestParam("id") String id) {
boolean check = //[validate the id];
if(!check){
return "The id selected is out of Range, please select another id within range";
}
}
클라이언트 측에서 양식 제출을 가로 채서 다음을 확인합니다.
$(".myForm").submit(function(event) {
var success = true;
$.ajax({
url: "/validate",
type: "GET",
async: false, //block until we get a response
data: { id : $("#idInput").val() },
success: function(error) {
if (error) {
$("#errorContainer").html(error);
success = false;
}
}
});
return success;
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다