구멍가게 코딩단의 Spring 책을 참고로 공부를 하고 있다. 

책을 그대로 따라하면서 주석도 추가로 달고 있다. 


sampleController2.java

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
/*
 * SampleContoller2 by using annotation 'ModelAttribute'
 */
 
package org.zerock.web;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class SampleController2 {
 
    private static final Logger logger =
        // check the log
        LoggerFactory.getLogger(SampleController2.class);
    
    @RequestMapping("doC")
    public String doC( @ModelAttribute("msg"String msg){
        logger.info("################## doC ##################");
        
        return "result"// try to find '/WEB-INF/views/result.jsp'
    }
}
 
cs


result.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <!-- automatically send obj to view -->
  <span>Hello World ${msg}</span>
  
</body>
</html>
cs

${msg} 객체 값을 view로 보낸다. 


결과  : http://localhost:8080/doC?msg=Moondol  입력 시 Hello World Moondol 출력

+ Recent posts