『 컨트롤러 클래스 작성

​<코드작성>

​@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);
       
    }


 

 

 


 

 

 



『 Command 클래스 작성

​<클래스작성>

​import java.io.Serializable;
 
import org.apache.commons.lang.builder.ToStringBuilder;
 
 
public class SampleCommand implements Serializable {
 
    /** serialVersionUID */
    private static final long serialVersionUID = 1L;
   
    private String name;
   
    private String mail;
   
    private Integer age;
   
    private Boolean confirmed;
   
    public SampleCommand() { }
   
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

      // getter,setter 생략
   
}

 

 

 


 

 



 Command(@ModelAttribute)에

의한 데이터 송수신


​▷ Command 클래스 작성
 컨트롤러 작성
JSP 작성
에러 메시지 정의


 


 

 

 


 

 



『 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>


 

 

 

 

 


 

 

 

 

『 JSON형식 취득하는 경우


<소스코드>

​@Controller
public class JsonController {
   
    @RequestMapping(value="/ajax/jsonOut1", method={RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<UserInfoViewDto> jsonOut (@RequestParam String cd) {
       
        if(StringUtils.isEmpty(departmentCd)) {
            return new ArrayList<UserInfoViewDto>();
        }
       
        List<UserInfoDto> list = /* Servlet/DAO로부터 취득 */;
        return list;
       
    }
}

 

 

 


 

+ Recent posts