'spring html'에 해당되는 글 2건

# [Spring MVC] thymeleaf 적용하기

1. pom.xml 

<dependency>
	<groupId>org.thymeleaf</groupId>
	<artifactId>thymeleaf-spring5</artifactId>
	<version>3.0.11.RELEASE</version>
</dependency>

2. servlet-context.xml 설정 

 # 만약 thymelead 한글 깨진 다면,  아래의 UTF-8 들어간 부분을 확인하시길 바랍니다.

<beans:property name="characterEncoding" value="UTF-8" />

<beans:property name="fileEncoding" value="UTF-8" />

	<!-- 기존 jsp로 화면 설계 부분 -->
	<!-- 
	<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> 
	-->
	
	<!-- thymeleaf 설정 부분  -->
	<beans:bean id="templateResolver"
		class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".html" />
		<beans:property name="templateMode" value="HTML5" />
		<beans:property name="cacheable" value="false" />
		<beans:property name="characterEncoding" value="UTF-8" />
	</beans:bean>

	<beans:bean id="templateEngine"
		class="org.thymeleaf.spring5.SpringTemplateEngine">
		<beans:property name="templateResolver"
			ref="templateResolver" />
	</beans:bean>

	<beans:bean
		class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
		<beans:property name="templateEngine"
			ref="templateEngine" />
		<beans:property name="characterEncoding" value="UTF-8" />
	</beans:bean>

	<beans:bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<beans:property name="location"
			value="/resources/config/application.config" />
		<beans:property name="fileEncoding" value="UTF-8" />
	</beans:bean>
	<!-- thymeleaf 설정 부분  -->

3. 페이지 

<!DOCTYPE html>

<!-- thymelead 불러오기 -->
<html lang="ko" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
	<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>파일업로드</title>
</head>

<body>
	<div th:if="${msg!=null}" style="">
		<h1>파일 업로드 결과</h1>
		<h2 th:text="${msg}">${msg}</h2>
	</div>

	<h1>단일 파일 보내기</h1>
	<form method="post" enctype="multipart/form-data" action="" th:action="@{fileUpload}">
		<label>file : </label> <input type="file" name="file"> <label>name
			: </label> <input type="text" name="name"> <input type="submit" value="upload">
	</form>

	<h2>여러개 파일 보내기</h2>
	<form method="post" enctype='multipart/form-data' action="" th:action="@{multiFileUpload}">
		<label>files : </label> <input type="file" name="files" multiple>
		<label>name : </label> <input type="text" name="name"> <input type="submit" value="upload">
	</form>

    <div id="root">
        
    </div>
    <!-- <script src="../resources/dist/app.js"></script> -->
    <script src="../dist/app.js"></script>
</body>

</html>
블로그 이미지

미나미나미

,

# 스프링 JSP에서 HTML로 설정 변경하기 

 

 

1. web.xml의 추가 부분 ( 추가 부분을 확인하세요.)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>

	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- 추가 부분 시작 : HTML 설정 부분 -->
	<servlet>
		<servlet-name>htmlServlet</servlet-name>
		<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>htmlServlet</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>
	<!-- 추가 부분 끝 : HTML 설정 부분 -->


	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>


	<!-- 한글설정 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>
			org.springframework.web.filter.CharacterEncodingFilter
		</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 한글설정 END -->


</web-app>

 

 

2. servlet-context.html의 suffix를 html로 변경

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

   <beans:property name="suffix" value=".html" />로 변경

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://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/" />
		<!-- jsp을 html로 변경 -->
		<beans:property name="suffix" value=".html" />
		
	</beans:bean>
	
	<context:component-scan base-package="com.example.thymeleaf" />
	
	
	
</beans:beans>

 

 

3. eventList.html 파일 불러오기

 

- HTML 파일

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
    <title>Insert title here</title>
</head>

<body>
    <p>스프링에서 html 불러오기</p>
</body>

</html>

- EventController.java

package com.example.thymeleaf;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class EventController {

	@GetMapping("eventList")
	public String eventList(HttpServletRequest request, HttpServletResponse response) {
		return "eventList";
	}
	
}

4. 결과화면

 

블로그 이미지

미나미나미

,