본문 바로가기
개발 수업/Spring Boot

[SpringBoot] index, redirect

by 오늘 하루s 2023. 8. 28.
728x90
더보기

Day83. 230828

index이용 메인페이지,redirect

 

MainController>

package com.mycom.app;

import com.mycom.app.question.entity.Question;
import com.mycom.app.question.repository.QuestionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class MainController {
    @Autowired
    private QuestionRepository questionRepository;

    //요청주소 http://localhost:8090/
    //메인화면이 보여줘야한다.
    //요청주소 http://localhost:8090/question/list
    //일단 여기에서는 질의목록페이지(question_list.html)를 출력
    @GetMapping("/")
    //@ResponseBody
    public String index(){
        return "index"; //templates폴더하위 index.html문서
    }
}

redirect : 페이지 전환 주체가 클라이언트로,서버에서 클라이언트에게 요청한 url을 다른 url로 재접속하라고 명령을 보내는 것이다.

 

 

index.html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
  <h1>INDEX</h1>
  <h2>index화면</h2>
  <ul>
      <li><a href="/question/list">질의응답게시판(목록조회)</a></li>
  </ul>
</body>
</html>

 

localhost:8090 으로 요청했을 때 redirect요청으로 index화면으로 간다.

728x90