JAVA

JSP 서빙/ MAVEN

Strickland 2024. 5. 4. 11:37

pom.xml 에 의존성 추가

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

 

 

controller 생성

package com.hyukjin.springboot.myfirstwebapp.hello;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class SayHelloController {
 

    @RequestMapping("say-hello-html")
    @ResponseBody
    public String sayHelloHtml() {

        StringBuffer sb = new StringBuffer();
        sb.append("<html>");

        sb.append("<head>");
        sb.append("<title>My first HTML Page - changed</title>");
        sb.append("</head>");

        sb.append("<body>");
        sb.append("My first html page with body");
        sb.append("</body>");
        sb.append("</html>");
        return sb.toString();
    }

    @RequestMapping("say-hello-jsp")
    // src/main/resources/META-INF/resources/WEB-INF/jsp/sayHello.jsp
    public String sayHelloJsp() {
        return "sayHello";
    }




}

 

sayHelloJsp 엔 @ResponseBody 어노테이션 삭제 -> 템플릿엔진을 사용 위함

 

application.properties 수정

spring.application.name=myfirstwebapp
#server.port=8081
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
logging.level.org.springframework=debug

 

스프링 부트는 src/main/resources/META-INF/resources 까지는 이미 알고 있음.

 

src/main/resources/META-INF/resources/WEB-INF/jsp/sayHello.jsp 생성

<html>
<head>
    <title>My first HTML Page</title>
</head>
    <body>
        My first html page with body
    </body>
</html>

 

 

localhost:8081/say-hell-jsp 요청

서빙 성공

'JAVA' 카테고리의 다른 글

JSTL 사용법/ MAVEN  (0) 2024.05.05
@SessionAttributes  (0) 2024.05.05
JDBC / Maven  (0) 2024.05.04
@ConfigurationProperties  (0) 2024.05.03
@Lazy  (0) 2024.05.01