1. 스프링 3.0 프로젝트 생성 및 경로 셋팅 


: 프로젝트의 이름을 써주고 Spring MVC project를 선택해서 생성해준다.


-프로젝트 경로


: 프로젝트의 이름은 "SpringMVC1" 으로 해주었지만 프로젝트 생성해줄때 패키지를 com.exe.springmvc로 설정해주었기 때문에

이게 스프링 웹 경로상의 주소는 "springmvc"가 된다. 

Web Project Settings에서 웹주소상의 경로를 확인할 수 있다.


2. 웹과의 연결 


-TestController.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
package com.exe.springmvc;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller("test.controller")
public class TestController {
    /*
     * @RequestMapping(value="/test/param.action",method = {RequestMethod.GET})
     * public String processGetRequest() {
     * 
     * System.out.println("GET 방식 Request");
     * 
     * return "paramResult";// /WEB-INF/views/paramResult.jsp }
     * 
     * @RequestMapping(value = "/test/param.action",method = {RequestMethod.POST})
     * public String processPostRequest() { System.out.println("post 방식 Request");
     * 
     * return "paramResult"; }
     */
    
    @RequestMapping(value = "/test/param.action",method = {RequestMethod.POST,RequestMethod.GET})
    public String processPostRequest(String name,HttpServletRequest request) {
        System.out.println("get/post 방식 Request");
        
        System.out.println(name);
        System.out.println(request.getParameter("phone"));
        
        return "paramResult";
    }
}
cs

: 주석 처리안한 메소드는 get,post방식을 하나의 메소드로 만든것이다.

/test/param.action의 경로의 주소를 입력하면 위의 메소드가 실행된다.

system.out.println()의 결과는 콘솔에 찍히고, 마지막에 paramResult.jsp로 보내준다.


**여기서 주목할 점은 Controller의 경로에는 "/test/param.action"이라고 "/"를 앞에 붙이지만

밑에 JSP파일에서는 "test/param.action" 이라고 쓴다. 헷갈리지 않게 명심!


-paramResult.jsp

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
<%@ page 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>
cs

: home에서 넘어온 매개변수를 request.getParameter로 받아서 <%=name%>으로 각각 출력해주는 역을 한다.


-home.jsp

: index.jsp 같은 역할을 한다.

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
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" pageEncoding="UTF-8" %>
<html>
<head>
    <title>Home</title>
</head>
<body>
<h1>
    Hello world!  
</h1>
 
<P>  The time on the server is ${serverTime}. </P>
 
<h3><a href="hello.action">Spring 환영 메세지</a></h3>
<h3><a href="test/param.action?name=suzi&phone=010-123-1234&email=suzi@naver.com">GET방식 테스트</a> </h3>
 
<h3>2. 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"><br/>
<input type="submit" value="전송"/><br/>
 
</form>
</body>
</html>
cs

매개변수를 넘겨주는 역할을 하는 jsp페이지이다.


-실행화면

주소는 localhost/springmvc/로 들어가서 링크를 타고 들어가면 된다.



+ Recent posts