-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[이서현] 3주차 과제 - complete #29
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
### 3주차 Server S-Day 과제 - 이서현 | ||
> 과제(spring): 스프링 프로젝트 생성, 주어진 HTML(index.html, members.html, newMember.html)을 프로젝트에 포함시키기, SampleController 파일을 만들고 코드 작성 | ||
|
||
* SampleController.class의 소스 코드 : | ||
``` | ||
package gdscstudy.serverstudy3.controllers; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
@Controller | ||
public class SampleController { | ||
@GetMapping("/") //root url 경로 | ||
public String home(Model model){ | ||
model.addAttribute("description", "메인 페이지입니다."); | ||
//key-value 쌍을 모델에 추가 (attributeName "des...", attributeValue "메인...") | ||
return "index"; //html 파일명 | ||
} | ||
|
||
@GetMapping("/members") //url 경로 | ||
public String showMembers(Model model){ //Model은 데이터를 View로 전달하기 위해 사용됨 | ||
model.addAttribute("member1", "Seohyun Lee"); | ||
model.addAttribute("member2", "Haeseung Jeon"); | ||
model.addAttribute("member3", "Hyuna Kim"); | ||
return "members"; //html 파일명 | ||
} | ||
|
||
@GetMapping("/members/new") //url 경로 | ||
public String showNewMember(@RequestParam(name = "name", defaultValue = "guest") String name, | ||
//@RequestParam은 쿼리 스트링 방식으로 url을 통해 파라미터로 값을 받아옴 | ||
//"?name=value1&email=value2": query string(쿼리 파라미터), '&' 연산자 사용해 쿼리 스트링 여러개 | ||
//"name"은 쿼리 파라미터의 key, name 매개변수에 value 받아옴. "guest"은 value 없을 경우 기본값. | ||
String email, //RequestParam 생략 (자동으로 데이터 파싱), email 매개변수에 value 받아옴 | ||
Model model){ | ||
model.addAttribute("name", name); //"name"은 View의 변수 이름(key), name에 value 있음. 모델에 추가 | ||
model.addAttribute("email", email); //"email"은 View의 변수 이름(key), email에 value 있음. 모델에 추가 | ||
return "newMember"; //html 파일명 | ||
} | ||
} | ||
//동영상 : https://drive.google.com/file/d/1kaPrxyZM4b8yTSnxNVRAOqSYwahvLMST/view?usp=sharing | ||
//참고자료 : | ||
// [Spring] @RequestParam 사용법 https://dangdangee.tistory.com/entry/Spring-RequestParam-%EC%82%AC%EC%9A%A9%EB%B2%95 | ||
// [Spring] @RequestParam - 요청 파라미터 데이터 파싱하기 https://amy-it.tistory.com/108 | ||
// [Spring] Spring MVC: Controller에서 parameter를 받아오는 방법 https://ooeunz.tistory.com/99 | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아주 꼼꼼하게 쿼리 스트링에 대해서 공부해주셨네요! 쿼리 스트링은 URL에서
?
로 리소스와 구분해주고,key=value
로 파라미터를 구분해주고, 그리고 여러 개의 쿼리 스트링을 만들고 싶을 경우에는&
연산자를 쓰면 된답니다. 자세하게 공부해주신 점 아주 좋아요👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
해당 부분이 가장 헷갈리더라고요.. 감사합니다!!