HomeController.java

package kh.spring.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		return "home";
	}
}

매서드 구분을 하지 않는다면 (인자값이 하나라면)

	@RequestMapping("/")

 

 

package kh.spring.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home() {
		return "home";
	}
}

 


제이쿼리 템플릿에 추가

home.jsp 새로 만들기

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
</head>
<body>

</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
</head>
<body>
	<a href="input">toInput</a>
	<a href="output">toOutput</a>
</body>
</html>

 


서버 가동

 

server.xml의 포트번호와 path명 입력

http://localhost:8085/controller/

 


toInput

package kh.spring.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	@RequestMapping("/")
	public String home() {
		return "home";
	}
	
	@RequestMapping("input")
	public String toInput() {
		return "input";
	}
}

 

input.jsp 생성

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
</head>
<body>
  여기가 Input
</body>
</html>

잘 포워드 된 것 확인


주소 단순화 하기

 

path 비우기

 

포트번호 80으로

톰캣 설정파일을 변경했을 때는 서버 재시동 해야 함

 

 


폴더 생성

 

messages 폴더에 input.jsp 넣기

 

컨트롤러 경로 변경

package kh.spring.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	@RequestMapping("/")
	public String home() {
		return "home";
	}
	
	@RequestMapping("input")
	public String toInput() {
		return "messages/input";
		// WEB-INF/views/messages/input.jsp
	}
}

 

forward/redirect

포워드는 서버가 보여주는 값만 변환, 클라이언트측 에서는 요청한 url로 유지

forward - 클라이언트에게 가라는 것이 아니라 서버가 내부적으로 페이지를 전환하는 것. 그래서 client가 보는 페이지 주소는 유지됨

 

리다이렉트는 클라이언트 url을 바꾸는 명령 : 클라이언트의 주소창이 바뀐다. request 초기화

포워드는 서버쪽 리스펀스에다 해당 페이지를 담아서 보내주는 것 : 클라이언트의 주소창 유지. request 유지

 

redirect:home 으로 보내면 home에 맵핑되는 페이지 없으므로 에러


input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
</head>
<body>
 
 	<form action="inputProc" method="post">
 		<input type=text name="writer">
 		<input type=text name="message">
 		<button>전송</button>
 	</form>
 
</body>
</html>

버튼의 기본타입은 submit

 

 

package kh.spring.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	@RequestMapping("/")
	public String home() {
		return "home";
	}
	
	@RequestMapping("input")
	public String toInput() {
		return "messages/input";
		// WEB-INF/views/messages/input.jsp
	}
	
	@RequestMapping("inputProc")
	public String inputProc() {
		return "home";
	}
}

입력이 끝나면 다시 홈으로


값을 받는법?

	@RequestMapping("inputProc")
	public String inputProc() {
		return "home";
	}

request라는 객체 없음

 

스프링에서는 필요하면 매개변수 만들고 필요없으면 매개변수 안만들면된다.

	@RequestMapping("inputProc")
	public String inputProc(HttpServletRequest request) {
		String writer = request.getParameter("writer");
		String message = request.getParameter("message");
		System.out.println(writer+" : "+message);
		
		return "home";
	}

 

한글은 깨짐


포워드로 돌아왔으므로 request 유지됨

그 상태에서 F5를 하면 계속 제출

 

리다이렉트로 리퀘스트 초기화 해야함!

	@RequestMapping("inputProc")
	public String inputProc(HttpServletRequest request) {

		String writer = request.getParameter("writer");
		String message = request.getParameter("message");
		System.out.println(writer+" : "+message);
		
		return "redirect:/";
	}

매개변수

 

	@RequestMapping("inputProc")
	public String inputProc(HttpServletRequest request) {

		String writer = request.getParameter("writer");
		String message = request.getParameter("message");
		System.out.println(writer+" : "+message);
		
		return "redirect:/";
	}

 

	@RequestMapping("inputProc")
	public String inputProc(String writer, String message) {

		System.out.println(writer+" : "+message);
		return "redirect:/";
	}

보내는 값의 name값과 매개변수 이름과 동일해야 자동으로 대입 된다.

 

 

	@RequestMapping("inputProc")
	public String inputProc(HttpServletRequest requset,String writer, String message) {

		System.out.println(writer+" : "+message);
		return "redirect:/";
	}

원하는 만큼 매개변수를 만들 수 있다.

 


MessagesDTO 생성

package kh.spring.dto;

public class MessagesDTO {

	private int seq;
	private String writer;
	private String message;
	
	
	public MessagesDTO() {}
	public MessagesDTO(int seq, String writer, String message) {
		super();
		this.seq = seq;
		this.writer = writer;
		this.message = message;
	}
	public int getSeq() {
		return seq;
	}
	public void setSeq(int seq) {
		this.seq = seq;
	}
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	
}

 

HomeController

	@RequestMapping("inputProc")
	public String inputProc(MessagesDTO dto) {

		System.out.println(dto.getWriter()+" : "+dto.getMessage());
		return "redirect:/";
	}

 

 

매개변수로 바로 dto로 넣어 줄 수 있음

- request.getParameter("") 코드 필요 없음!

 

멤버필드 이름과 클라이언트에서 보내주는 네임이 같다면

Dynamic Type Binding

다이나믹 타입 바인딩 

 

MessagesDTO에서 값이 주어지지 않은 seq에는 뭐가 들어갈까?

- null도 객체이므로 아님

- 쓰레기값은 스텍메모리를 사용할때 들어감

- 아무값도 아니라면 0 값이 들어간다.

 

(MessagesDTO 디폴트생성자 필요!)


 

+ Recent posts