Spring 셋팅 설정
Spring 셋팅 설정
프로젝트 구조
순서
dynamic web project 로 프로젝트 시작
-> 프로젝트에 우클릭 configure -> convert maven project (기존 자바 EE프로젝트를 maven형태의 프로젝트로 변환)
프로젝트에 우클릭 -> javaEE tools -> generate deploy 클릭(web.xml생성)
pom.xml에 받을 정보들을 기재해준다(해당 정보에 기재한대로 프로젝트 내에서 자동으로 필요한 데이터들을다운로드를 시작한다)
4.0.0 sample01 sample01 0.0.1-SNAPSHOT war src maven-compiler-plugin 3.8.0 1.8 1.8 maven-war-plugin 3.2.3 WebContent org.springframework spring-webmvc 5.2.0.RELEASE log4j log4j 1.2.17 org.slf4j slf4j-simple 1.7.25
WebContent WEB-INF에 있는 web.xml 값 설정하기
(xml주소지정및 한글설정)
sample01 index.html index.htm index.jsp default.html default.htm default.jsp dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/spring/servlet-context.xml 1 dispatcherServlet / encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true encodingFilter /*
WEB-INF에 spring 폴더 생성및 spring 폴더 내부에
servlet-context.xml 생성 (prefix와 suffix를 정해주면, dispatcher가 알아서 view를 찾을 수 있게 된다
Java Resource폴더 우클릭 -> TestController생성(servlet-context에서 지정한 java의 공통 패키지 이름과 패키지명을 맞춰주면된다 해당 프로젝트에서는 패키지명을 bit.com.a로 지정하였다)
TestController
package bit.com.a; import java.text.DateFormat; import java.util.Date; import java.util.Locale; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { @RequestMapping(value = "hello", method = {RequestMethod.GET, RequestMethod.POST}) public String hello(Model model, Locale locale) { System.out.println("TestController hello"); Date date = new Date(); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); String formattedDate = dateFormat.format(date); // 짐싸! model.addAttribute("serverTime", formattedDate); //ModelAndView 로 return 해주지 않아도 된다 String 형식으로 주로 사용 return "hello"; } }
RequestMapping은 WebServlet("/") 와 비슷한 역할을 하며 GET, POST방식으로 지정해서 받을수 있다.
@RequestMapping("hello")를 사용하면 그냥 바로 get방식의 hello로 넘어간다
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Insert title here hello
a태그를 클릭하면 hello가지정되어 RequestMapping을 타고 간다.
WEB-INF안에 views를 폴더명으로 하는 hello.jsp생성
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Insert title here hello
log4j(System.out이 무겁기때문에 대신 값을 나타내주는 라이브러리)
JavaResource에 log4j.xml 생성한후 해당 값들을 넣어준다
Logger을 활용한 print출력
package bit.com.a; import java.text.DateFormat; import java.util.Date; import java.util.Locale; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { //가벼움 private static Logger logger = LoggerFactory.getLogger(TestController.class); @RequestMapping("hello") public String hello(Model model, Locale locale) { // System.out.println("TestController hello"); logger.info("TestController hello " + new Date()); Date date = new Date(); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); String formattedDate = dateFormat.format(date); // 짐싸! model.addAttribute("serverTime", formattedDate); //ModelAndView 로 return 해주지 않아도 된다 String 형식으로 주로 사용 return "hello"; } }
출력 결과
from http://loy124.tistory.com/188 by ccl(A) rewrite - 2020-03-06 14:21:02
댓글
댓글 쓰기