반응형

Spring - Static Pages Example


(원문 위치 : http://www.tutorialspoint.com/spring/spring_static_pages_example.htm)

이번 강좌는 Spring framework를 사용하여 <mvc:resources> 태그의 도움으로 동적(dynamic)페이지와 마찬가지로 정적(static)페이지를 접근할 수 있는 간단한 웹기반 프로그램을 작성하는 법을 알아본다.


[21-1] 강좌를 참고하여 프로젝트를 생성한다.

프로젝트 구성은 아래와 같은 구성을 갖게 될 것이다.




WebController.java :

package com.tutorialspoint.hellowebstatic;


import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;


@Controller

public class WebController {

@RequestMapping(value="/index", method=RequestMethod.GET)

public String index() {

return "index";

}

@RequestMapping(value="/staticPage", method=RequestMethod.GET)

public String redirect() {

return "redirect:/pages/final.html";

}

}


servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns="http://www.springframework.org/schema/mvc"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:beans="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->

<annotation-driven />


<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->

<resources mapping="/resources/**" location="/resources/" />


<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<beans:property name="prefix" value="/WEB-INF/views/" />

<beans:property name="suffix" value=".jsp" />

</beans:bean>

<context:component-scan base-package="com.tutorialspoint.hellowebstatic" />

<mvc:resources mapping="/pages/**" location="/WEB-INF/pages/" />

<mvc:annotation-driven/>

</beans:beans>

<mvc:resorcces.../> 태그는 정적페이지를 맵핑하기 위해 사용되고 있다. mapping 속성은 http 요청(request)의 URL 패턴을 표시하는 Ant 패턴이어야 한다. location 속성은 이미지, stylesheet, javascript, 그밖의 다른 컨텐츠를 포함하는 정적 페이지를 갖는 하나 또는 그 이상의 유효한 리소스 디렉토리 위치를 표시하여야 한다. 다수의 리소스 위치는 comma(,)를 사용하여 표시된다.

index.jsp :

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<html>

<head>

    <title>Spring Landing Page</title>

</head>

<body>

<h2>Spring Landing Pag</h2>

<p>Click below button to get a simple HTML page</p>

<form:form method="GET" action="/hellowebstatic/staticPage">

<table>

    <tr>

    <td>

    <input type="submit" value="Get HTML Page"/>

    </td>

    </tr>

</table>  

</form:form>

</body>

</html>


WEB-INF/pages/final.htm

<html>

<head>

    <title>Spring Static Page</title>

</head>

<body>


<h2>A simple HTML page</h2>


</body>

</html>


실행 결과는 아래와 같다.





반응형

+ Recent posts