[Spring] 스프링 웹(jsp)에 데이터출력하기#3

studentView.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>
student: ${studentId}

</body>
</html>

저번에 EL(Expression Language) 를 사용해서 jsp파일에서 값을 출력해보았습니다.

컨트롤러에서 url자체에 입력한 값을 보내보겠습니다.

package com.co.mvc05_01;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class HomeController {

	@RequestMapping("/student/{studentId}")
	public String etStudent(@PathVariable String studentId, Model model) {
		System.out.println(studentId);
		model.addAttribute("studentId",studentId);
		return "student/studentView";
	}
}

@RequestMapping안에 {}를 사용하고 @PathVariable 어노테이션을 쓰면 url에 입력한 것을 변수처럼 받아올 수 있습니다. 그러면 studentView에서 ${studentID} 이부분에 값이 들어가게 됩니다. 

EX ) http://localhost:9090/mvc05_01/student/30  이렇게 입력하면 studentId에 30이라는 값이 들어가게 됩니다.

 

결과

 

댓글

Designed by JB FACTORY