반응형

[스프링(Spring)] Hello World (2)


I Hello World (2)

Spring_MVC; application.properties; 컨트롤러 (Controller)


1. application.properties 수정




1
2
3
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
 

cs



2. HelloController.java





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.shl.spring.hello.web;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class HelloController {
    
    @RequestMapping(value="/hello")
    public String helloSpringBoot(Model model) {        
        model.addAttribute("name""이정완");            
        return "hello";
    }
}
 

cs



3. webapp 

폴더 생성     src/main/webapp/




1) src/main/webapp/    WEB-INF 폴더 생성




2) src/main/webapp/WEB-INF     views 폴더 생성




2) src/main/webapp/WEB-INF/views    hello.jsp 파일 생성

hello.jsp





4. hello.jsp




1
2
3
4
5
6
7
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 
<html>
<body>
    Hello, ${name}!    
</body>
</html>

cs


5. 서버 실행 결과 확인

1) 서버 실행 결과




2) 웹페이지 확인




6. HelloController.java 수정



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.shl.spring.hello.web;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class HelloController {
    
    @RequestMapping(value="/hello")
    public String helloSpringBoot(Model model) {
        
        model.addAttribute("name""이정완");
        model.addAttribute("age""30");
        model.addAttribute("tel""010-1234-5678");
        model.addAttribute("email""jungwan@naver.com");
        
        return "hello";
    }
}
 
cs



7. hello.jsp 수정



1
2
3
4
5
6
7
8
9
10
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 
<html>
<body>
    Hello, ${name}!
    <br> age   : ${age}
    <br> tele  : ${tel}
    <br> email : ${email}
</body>
</html>
cs



8. 서버실행





반응형

'스프링 (Spring) > Hello World' 카테고리의 다른 글

[스프링 (Spring)]_ Hello World (1)  (0) 2019.03.07