[스프링-1] Spring 의 기본 속성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
 
 
 
 
 
 
    @RequestMapping(value = "/", method = RequestMethod.GET)   // value => 값은 index 페이지 method 는 form 양식처럼 post,get 여러가지사용가능
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);
        
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
        
        String formattedDate = dateFormat.format(date);
        
        model.addAttribute("serverTime", formattedDate );
        
        return "home"// 위에 value = "/" 값이 home.jsp 로 바로가게 하는 로직
     }
cs

 

 

 

@RequestMapping 은 JSP 에서 컨트롤러 역할을 하는 것이당

URL = 지정하면 String home 값이 등록된다

1
2
3
4
5
6
7
8
9
10
11
12
 
 
 
 
 
 
    @RequestMapping("/")
    public String main(Model model) {       // Model 클래스에 model 객체를사용 jsp에서 request 역할
        model.addAttribute("message""welcome!");  // >> jsp = request.setAttribute 같은역할 <key,value>
        return "main";  // main.jsp 로 뷰로 보
    }
 
cs

 

Model 에다가 값을 한개 의 값을 지정해주는

 

 

1.request.setAttribute 랑 비슷한 개념임

2. 스프링에서는 Model 에다가 넣는것만 바뀜

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 
 
 
 
 
@RequestMapping("move.do")
    public String move() throws Exception { 
    //    String name =URLEncoder.encode("kim", "utf-8");
        String name = URLEncoder.encode("kim""utf-8");  // utf-8로 쿼리파라미터 한글로 번역
        return "redirect:/result.do?name="+name+"&age=20"// Jsp에서는 sendridirect 같은역할
        
    }
    
    @RequestMapping("result.do")
    public String result(Model model, // model.attr < 사용하기위해서 ㅜ가
            @RequestParam(defaultValue = "noname"String name, //request parameter name 값 받기
            @RequestParam(defaultValue = "10"int age) throws Exception{ // request age age 값 받기
        name = URLDecoder.decode(name, "utf-8"); // encode 된거 뿌릴때는 decode 로 변환해서 뿌리자
        model.addAttribute("name", name); // 모델에다가 저장해주기 request.setattribute 임
        model.addAttribute("age", age);// 모델에다가 저장해주기 request.setattribute 임
        return "test/result";  // result.jsp 페이지로 가기
    }
    
    @RequestMapping("mav.do")
    public ModelAndView mav() {
        Map<String, Object> map = new HashMap<>(); // 여러가지 데이터를 저장하기 위함
        map.put("dto"new ProductDto("pen",1000)); // < key , value > key값을 호출하면 value 갑 얻기
        return new ModelAndView("test/mav_result","map",map); // url , key , value
    }
}
 
 
package com.example.spring01.model;
 
public class ProductDto {
 
    private String name;
    private double price;
    
    
    
    public ProductDto() {
        
    }
    
    public ProductDto(String name,double price) {
        this.name=name;
        this.price=price;
    }
    
    
    
    @Override
    public String toString() {
        return "ProductDto [name=" + name + ", price=" + price + "]";
    }
 
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
}
 
cs

 

 

Udemy Programming School 님의 강의를 듣고 내것으로 만들기 위해 작성햇어요 ~