SpringMVC 프로젝트에서 get 과 post 방식으로 값을 넘기는 예제이다.


get 방식 예제)


1. student1.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="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>
    <form action = "student" method = "get">
        student id : <input type = "text" name = "id"><br/>
        <input type = "submit" value = "전송">
    </form>
</body>
</html>
cs


2. student2.jsp

1
2
3
4
5
6
7
8
9
10
11
12
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="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>
    StudentId : ${studentId}
</body>
</html>
cs


3. HomeController.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.java.ex;
 
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
 
import javax.servlet.http.HttpServletRequest;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
 
/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
    
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
    /**
     * Simply selects the home view to render by returning its name.
     */
 
    @RequestMapping("student1")
    public String gostudent1(){
        return "/student/student1";
    }
    
    @RequestMapping(value = "/student", method = RequestMethod.GET)
    public String goStudent(HttpServletRequest httpServletRequest, Model model) {
        
        System.out.println("RequestMethod.GET");
        
        String id = httpServletRequest.getParameter("id");
        System.out.println("id : " + id);
        model.addAttribute("studentId", id);
        
        return "student/student2";
    }
}    
cs


*실행결과




=> HomeController.java 는 @Controller 어노테이션을 클래스에 적용하고, 

@RequestMapping 어노테이션을 이용하여 클라이언트의 요구에 따라 처리할 메서드를 지정하였다.

(내부적으로 스프링에 속해있는 Dispatcher(web.xml 에 설정되어 있음) 가 

클라이언트의 요청과 컨트롤러를 통한 결과 값 그리고 뷰페이지 생성에 관여함)

따라서 student1 라는 url 이 왔을 때 student 폴더에 들어있는 student1.jsp 뷰 페이지를 보여주고 

여기에 작성한 student id 가 Controller 를 거쳐 GET 방식으로 student2.jsp 뷰 페이지에서 불러지게 된다.

get 방식이기 때문에 student2 의 url 에서 값이 보이는 것을 볼 수 있다.


post 방식 예제)

1. student1.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="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>
    <form action = "student" method = "post">
        student id : <input type = "text" name = "id"><br/>
        <input type = "submit" value = "전송">
    </form>
</body>
</html>
cs


2. student2.jsp

1
2
3
4
5
6
7
8
9
10
11
12
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="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>
    StudentId : ${studentId}
</body>
</html>
cs


3. HomeController.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.java.ex;
 
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
 
import javax.servlet.http.HttpServletRequest;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
 
/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
    
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
    /**
     * Simply selects the home view to render by returning its name.
     */
 
    @RequestMapping("student1")
    public String gostudent1(){
        return "/student/student1";
    }
    
    @RequestMapping(value = "/student", method = RequestMethod.POST)
    public ModelAndView goStudent(HttpServletRequest httpServletRequest) {
        
        System.out.println("RequestMethod.POST");
        
        String id = httpServletRequest.getParameter("id");
        System.out.println("id : " + id);
        
        ModelAndView mav = new ModelAndView();
        mav.setViewName("student/student2");
        mav.addObject("studentId", id);
        
        return mav; 
    }
}
cs


*실행결과




=> get 방식과는 다르게 ModelAndView 를 이용하였다. post 방식으로 값과 경로를

모두 관리하기 위헤서 ModelAndView 객체를 따로 생성하여 값을 student/student2 로 

보내겠다고 지정하였다. post 방식이기 때문에 url 에 값이 보이지 않는 것을 확인할 수 있다.



'Spring' 카테고리의 다른 글

SpringMVC(JDBC 사용)  (0) 2016.05.19
SpringMVC 게시판  (0) 2016.05.18
SpringMVC  (0) 2016.05.17
AOP 구현(@Aspect 어노테이션 기반)  (0) 2016.05.13
AOP 구현(XML 스키마 기반 Advice 종류 예제)  (0) 2016.05.13
,