๐2022-08-12๐
Spring MVC ๋ง๋ค๊ธฐ ์ํ... web application
์ผ๋ถ์ผ ๋ฟ์ด๋น..
war jar์ ํด๋์ค๋ง ๋๋ ค๋ฐ์ผ๋ฉด ๋๋๋ฐ war๋ html, ์ด๋ฏธ์ง ์ด๋ฐ๊ฑธ ๋ฃ์ด์ผํ๊ธฐ๋๋ฌธ์ ์๋๋ต!
dependency maven ๋๊ณ ์ค๊ธฐ!!
https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api/4.0.1
https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api
https://mvnrepository.com/artifact/javax.servlet/jstl/1.2
https://mvnrepository.com/artifact/org.springframework/spring-webmvc/5.3.22
FirstSpring/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gyuone</groupId>
<artifactId>FirstSpring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.22</version>
</dependency>
</dependencies>
</project>
๋ณด์ ๋๋ฌธ์ WEB-INF์ ๋ง๋ค์๋ค.
์๋๋ webapp ์์ ๋ฃ์๋๋ฐ ๋ณด์์์ ์ด์ ๋ก WEB-INF ์์ ๋ฃ์ด์ผํ๋ค.
ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ์์ฒญ๋ฐ๊ณ ์ฃผ๋ ์ปจํธ๋กค
์ ๋นํ ๋ชจ๋ธ ์ชฝ์ ํธ์ถํจ
๋น์ฆ๋์ค ๋ก์ง์ด ๋ค์ด์๋ ๋ชจ๋ธ
์ฌ์ฉ์์๊ฒ ๋ฟ๋ ค์ฃผ๋๊ฒ ๋ทฐ
๊ฒฐ๊ณผ๊ฐ์ด ๋์ค๋ฉด ์ปจํธ๋กค๋ฌ์์๋
๋ทฐ์์ jsp์ ๋์ง์ด๋ค๊ฐ ๊ฒฐ๊ณผ๊ฐ ๊ตฌํ ๊ฐ๋ค์ jsp์ ๋ค ๊ฐ์ ธ์ client๊ฐ ๋์ ธ์ค.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>FirstSpring</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.sprinframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.gyuone.config.MvcConfig
com.gyuone.config.ControllerConfig
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
ํฐ์บฃํ์ผ ๊ตฌ๋ํ๋ผ load on startup
๊ทธ๋๋ก ํฐ์บฃ ์ํ๋ก ์ ๋ฌ Dispatcher Servelt ์ด ๋ฐ๋ ๊ฒ Dispatcher servelt์ด ์ด๋ค ์์ฒญ์ธ์ง ํ๋จํ๋ค. ์ปจํธ๋กค๋ฌ๋ก ๋ณด๋ธ๋ค.
๋ชจ๋ธ์ ์๋ง๋ค์์ง๋ง, ์ปจํธ๋กค๋ฌ ๋ฉ์๋ ์์์ ๋ชจ๋ธ๋ก ์ ๊ทผ ๋น์ฆ๋์ค ๋ก์ง์ด ๋ค์ด๊ฐ. ๋ชจ๋ธ์ด ๋ฐ์ดํฐ๋ฒ ์ด์ค๊น์ง ์ ๊ทผํ๋ค. ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ค์ ์ค๋ค.
127.0.0.1:8080/FirstSpring/hello?name=kim
์ ๊ทผ์ด ์๋๋ค. view ํ์ผ ๋ง๋ค์ด์ ํ๋ ์ด์
NGINX๋ฅผ ์ค์นํ๊ณ ํฐ์บฃ์ผ๋ก ์ง์ด ๋ฃ์ด ๋ฒ๋ฆฌ๋ ..
<< ๋ฐฐํฌํ๋ ๋ฐฉ๋ฒ >>
์ดํด๋ฆฝ์ค ์๋ฒ ์ฃฝ์ด๊ณ ์์ํ ๊ฒ ์ถฉ๋์ฃผ์ !
spring์ผ๋ก ์ด๋ฆ ๋ฐ๊พธ๊ธฐ
E:\apache-tomcat-9.0.65\bin startup.bat ์คํ
spring์ผ๋ก ํ์ผ์ด ์์ฑ๋๋ค.
com.gyuone
config
ControllerConfig.java
package com.gyuone.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.gyuone.controller.HelloController;
@Configuration // Spring์ฉ ์ค์ ํด๋์ค
public class ControllerConfig {
@Bean
public HelloController helloController() {
return new HelloController();
}
}
MvcConfig.java
package com.gyuone.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer{
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
// TODO Auto-generated method stub
configurer.enable();
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
// TODO Auto-generated method stub
registry.jsp("/WEB-INF/view/", ".jsp"); // .jsp๋ฅผ ์์์ ๋ถ์ฌ๋ผ.
}
}
controller
HelloController.java
package com.gyuone.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller // ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋ฐ๋ controller
public class HelloController {
@GetMapping("/hello") // get๋ฐฉ์์ผ๋ก ๋ค์ด์์ ๋ hello ๋ฉ์๋ ์คํ
public String hello(Model model, @RequestParam(value="name", required = false) String name) { // RequestParam ํด๋ผ์ด์ธํธ์์ name์ด๋ผ๋ ํญ๋ชฉ์ผ๋ก ๊ฐ์ ์ ๋ฌํ๋ค.
model.addAttribute("greeting","์๋
ํ์ธ์, " + name); // greeting ํค๊ฐ. ํด๋ผ์ด์ธํธ ๋ชจ๋ธํ์ผ๋ก ์ ๋ฌ.
return "hello"; // string ์ผ๋ก ๋ฆฌํด ์ ์ฒด: view name ์ด๋ฆ๋ง ๋์ ธ์ฃผ๋ ๊ฒ hello.jsp๋ฅผ ๋ง๋ค ๊ฒ์ด๋ค.
// ๋น์ฆ๋์ค ๋ชจ๋ธ์ด ์๋ ๊ตฌ์กฐ
}
}
WEB-INF
view
hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello Page</title>
</head>
<body>
์ธ์ฌ๋ง : ${greeting}
</body>
</html>
FirstSpring/hello?name=kim์ ์ ๋ ฅํ๋ฉด..
'IT > WEB' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[39์ผ์ฐจ] ๊ฒ์ํ ๋ง๋ค๊ธฐ (2) (0) | 2022.08.16 |
---|---|
[38์ผ์ฐจ] ๊ฒ์ํ ๋ง๋ค๊ธฐ (0) | 2022.08.12 |
[38์ผ์ฐจ] AOP/ aop concert project (0) | 2022.08.12 |
[37์ผ์ฐจ] AutoDi (0) | 2022.08.11 |
[37์ผ์ฐจ] dISample project (0) | 2022.08.11 |
๋๊ธ