『 JSP 샘플 작성 』

​<소스코드>

<form:form modelAttribute="sampleCommand" action="${appUrl}/test/form2.html" method="post">

    <p>         <form:label path="name">이름</form:label>
        <form:input path="name" />
        <form:errors path="name" cssClass="errors" />
    </p>
    <p>
        <form:label path="mail">메일 주소</form:label>

        <form:input path="mail" />        
        <form:errors path="mail" cssClass="errors" />
    </p>
    <p>
        <form:label path="age">나이</form:label>
        <form:input path="age" />
        <form:errors path="age" cssClass="errors" />
    </p>
    <p>
        <form:label path="confirmed">확인</form:label>
        <form:checkbox path="confirmed" />
        <form:errors path="confirmed" cssClass="errors" />
    </p>
    <input type="submit"/>
</form:form>


 

 

 


 

 



『 JSP 샘플 작성 』

​<소스코드>

​<%-- Spring커스텀 태그 정의 --%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>  
<spring:hasBindErrors name="sampleCommand">
<div>
    <font color="red"><spring:message code="error.input" /></font>
    <%-- 공통 에러메시지 표시 --%>
    <font color="red"><c:forEach items="${errors.globalErrors}" var="error">
        <spring:message message="${error}" /><br/>     </c:forEach></font>
</div>
</spring:hasBindErrors>


 

 

 

 

'자바 > JAVA...Spring' 카테고리의 다른 글

Spring 3.2 & MyBatis] 에러 메시지 정의  (0) 2015.12.18
Spring 3.2 & MyBatis] JSP 샘플 작성(2)  (0) 2015.12.18
Cross-Browsing  (0) 2015.12.18
XMLHttpRequest 객체 생성  (0) 2015.12.18
XMLHttpRequest의 개요  (0) 2015.12.18

 


『 컨트롤러 클래스 작성(2)

​<코드작성>

// ③post로 보내는 경우
    @RequestMapping(method=RequestMethod.POST)
    public ModelAndView doAction(@ModelAttribute("sampleCommand") SampleCommand command, BindingResult bindingResult) {
       
        System.out.println(command);
        // command 각 항목 에러 체크
        if(StringUtils.isEmpty(command.getName())) {
            bindingResult.rejectValue("name", "error.required");
        }
       
        // 공통 에러 체크
        if(bindingResult.hasErrors()) {
            bindingResult.reject("error.message");
        }
       
        //에러가 있는 경우(원래 화면으로 돌아감)
        if(bindingResult.hasErrors()) {            
            ModelAndView mav = new ModelAndView();
            mav.getModel().putAll(bindingResult.getModel());
            return mav;
        }
       
        ModelAndView mav = new ModelAndView("forward:/hello.html");
        return mav;
       
    }
}

 

 

 

 

 

 



『 컨트롤러 클래스 작성

​<코드작성>

​@Controller
@RequestMapping("/test/form2")
public class Form2Controller {
   
    // ①command 초기 객체 취득
    @ModelAttribute("sampleCommand")
    public SampleCommand createInitCommand() {
        SampleCommand command = new SampleCommand();
 return command;    
     }
   
    // ②초기값 설정
    @RequestMapping(method=RequestMethod.GET)
    public void setupForm(Model model) {
       
        SampleCommand command = createInitCommand();
        command.setAge(1);
        model.addAttribute("sampleCommand", command);
       
    }


 

 

 


 



『 Form 데이터 송수신

​▷ @RequestParam에 의한 데이터 송수신


Command(@ModelAttribute)에 의한 데이터 송수신 


독자 데이터 바인드(@InitBinder)

 

 


 

 

 


 

 


전이되는 곳을 생략하는 경우

(View의 패스가 없음) 

​<소스코드>

​@Controller
public class AppointmentsController
   
    @RequestMapping("/sample")
    public ModelAndView doAction() {
   
        ModelAndView mav = new ModelAndView();
        model.addObject("message1", "aaaa");
        return mav;
    }
}

 

 

 


전이되는 곳을 생략하는 경우

(메서드 리턴값이 void)


<소스코드>


​@Controller
public class AppointmentsController
   
    @RequestMapping("/sample")
    public void doAction(ModelMap model) {
        model.addAttribute("message1", "aaaa");
    }

 

 


}

 


 

 

 



『 JSON형식 취득하는 경우(클라이언트측)

​<소스코드>

<script type="text/javascript">
$(document).ready(function(){
    $('#jsonOut1').click(function(){
        $.ajax({
            type: "POST",
            url : "${appUrl}/ajax/jsonOut1.html",
            data : {"cd": "syasin"},
            // 수신시의 미디어 타입
            dataType: "json",
            success:function(data){
                //TODO:
                alert(data);
            }
        });
    });
});
</script>
<ol>
    <li> <a href="${appUrl}/ajax/jsonOut1.html?cd=aaa">JSON형식 취득(GET으로 취득)</a></li>
    <li> <span id="jsonOut1">JSON형식 취득(jQuery을 이용하여 취득)</span></li>
</ol>


 

 

 

 

 


 

 

+ Recent posts