본문 바로가기
STUDY/SPRING

스프링(3.0) 간단한 웹 예제

by Anne of Green Galbes 2019. 4. 15.

스프링 흐름 이해 ( GET방식 & POST방식)


[GET 방식]

1. 컨트롤러 생성

○ 주소에 /test/param.action이 오면 processGetRequest메소드 를 처리하고 paramResult.jsp로 넘어감

package com.exe.springmvc;


@Controller("test.controller")

public class TestController {

@RequestMapping(value = "/test/param.action",method = RequestMethod.GET)

public String processGetRequest() {

System.out.println("GET방식 Request");

return "paramResult";

}

}


2. jsp파일 작성

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<%

request.setCharacterEncoding("UTF-8");

String cp = request.getContextPath();

String name = request.getParameter("name");

String phone = request.getParameter("phone");

String email = request.getParameter("email");

%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>


<body>

<h2>Param Result</h2>

이름 : <%=name %><br/>

전화 : <%=phone %><br/>

이메일 : <%=email %><br/>

</body>


</html>


3. 값 넘기기

○ home.jsp에서 값 넘김

<a href="test/param.action?name=suzi&phone=010-123-1234&email=suzi@naver.com">GET방식 테스트</a>



[POST 방식]

1. 컨트롤러 생성

@RequestMapping(value="/test/param.action",method=RequestMethod.POST)

public String processPostRequest() {

System.out.println("POST방식 Request");

return "paramResult";

}


2. 값 넘기기

○ home.jsp에서 값 넘김

<h3>POST 방식 테스트</h3>


<form action="test/param.action" method="post">

이름 : <input type="text" name="name"><br/>

전화 : <input type="text" name="phone"><br/>

이메일 : <input type="text" name="email">

<input type="submit" value="전송">

</form>



[GET/POST 방식]

1. 컨트롤러 생성

@RequestMapping(value = "/test/param.action",method = {RequestMethod.GET,RequestMethod.POST})

public String processGetPostRequest(String name, HttpServletRequest request) {

System.out.println("Get/Post방식 Request");

//스프링에 원하는 값을 요청하면 알아서 값을 가져다준다

System.out.println(name); //스프링이 다음 값을 가져옴

System.out.println(request.getParameter("phone")); //request에서 원하는 값을 가져옴

return "paramResult";

}


'STUDY > SPRING' 카테고리의 다른 글

스프링(3.0) Java에서 DB사용  (0) 2019.04.16
스프링(3.0) DTO사용, 한글처리, ModelAndView  (0) 2019.04.16
스프링(3.0) 톰캣서버 연결  (0) 2019.04.15
스프링(3.0) 의존성 주입  (0) 2019.04.15
스프링(3.0) 예제  (0) 2019.04.15

댓글