SyntaxHighlighter.all(); 스프링MVC - Form 데이터 받기 :: 게을러지고 싶어 부지런한 개발자

jsp에서 작성한 Form 데이터를 스프링의 Controller가 받아오는 방법이 있다. 



1) HttpServletRequest 클래스 이용






2) @RequestParam 어노테이션







3) 데이터 (Command) 객체 이용  

-기존의 @RequestParam의 개선방법


-2)의 @RequestParam을 사용하는 경우, 데이터를 많이 받게 되면 코드가 길어지고 복잡해짐.

따라서 2)과 같은 기존 방법보다는 데이터 (command) 객체를 이용하면 훨씬 간결해진다. 


 


3-1) Command 객체의 이름 변경

그리고 개발자는 @ModelAttribute 어노테이션을 이용하면 커맨드 객체의 이름도 변경 가능! 


만약 객체의 이름이 studentInformation인데, 이걸 좀 더 줄여서 studentInfo이라고 변경하고자 한다면 아래와 같이 변경가능 


Controller.java

1
2
3
4
5
@RequestMapping("/studentView")
    public String studentView(@ModelAttribute("studentInfo") StudentInformation studentInformation) {
    
    return "studentView";   //studentView.jsp
}
cs


studentView.jsp

1
2
3
4
5
6
7
8
9
10
11
12
이름: ${studentInfo.name} <br />
나이: ${studentInfo.age} <br />
학년: ${studentInfo.grade} <br />
반: ${studentInfo.gradeNum} <br />
 
 
<!--만약 studentInfo로 커맨드 객체명을 바꾸지 않았더라면 아래와 같이 사용...
이름: ${studentInformation.name} <br />
나이: ${studentInformation.age} <br />
학년: ${studentInformation.grade} <br />
반: ${studentInformation.gradeNum} <br /> -->
cs





출처: 인프런 Wiz Spring 

+ Recent posts