[TIL] 2020.08.19_TIL

TO_DO

  • [수업] spring-mvc 복습

Spring-mvc 코드 실습

  • 메이븐 프로젝트 구조

Spring

  • HelloController 생성
package kr.co.mlec.hello;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {
	
	// hello/hello.do 라는 uri가 들어왔을 때 request를 처
	@RequestMapping("/hello/hello.do")
	public ModelAndView hello() {
		ModelAndView mav = new ModelAndView("hello/hello"); //WEB-INF > jsp > hello 폴더 밑에 hello.jsp로 forword
		mav.addObject("msg", "hi 스프링  MVC~~");	//msg라는 이름으로 공유영역에 등록
		return mav;
	}
}
  • index.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>
</head>
<body>
<a href="<%=request.getContextPath() %>/hello/hello.do" >
	hello
</a><br>

</body>
</html>
  • hello.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>
</head>
<body>
	서버에서 온 메시지 : ${msg} 
</body>
</html>
  • 실행결과

Spring

Spring

태그: ,

카테고리:

업데이트:

댓글남기기