본문 바로가기

Spring

"아무것도 입력해주지 않았는데 서버를 실행하면 Hello World가 출력된다???"

Spring 실행 시 Hello World! 어떻게 나오는 걸까?


Web.xml에 등록 된 소스코드를 실행해주면 실행된다 !! 





url-pattern 중에 / 가 들어가면 appServlet을 실행해주세요 라는 코드인데 모든 url에 동작한다고 보면 된다


appServlet이 실행되면 Dispatcher가 실행, 이는 Front Controller이고 쉽게 주차 배차원이라고 생각하자


servlet-context.xml에서 정보 값을 받는다 



리스너를 달아주면 생명주기가 실행된다 



Dispatcher가 servlet- context.xml 과 root-context.xml을 읽어서 Spring을 실행한다 



servlet- context.xml 과 root-context.xml은 어디에 있을까?? 



Spring - appServlet - servlet-context.xml이 있고

Spring - root-context.xml이 있다


어떤 내용이 있을까??


servlet-context.xml: 자바클래스 객체를 등록하고 관리한다


컨트롤 등록하기 비교 Struts vs Spring

<Struts>


<Struts>

class My extends Action{

execute(){} //오버라이딩

}

—> struts-config.xml

<action path="/m" type="My">


<Spring>

@Controller

class My{

@Mapping("/m")

void gildong(){

return "caprisun";

}

}


※URL 요청

Client                Server

hello.html         hello.html 찾아서 준다

hello.jsp         Servlet을 통해 out.print 변환 된 html을 준다


서비스할 파일들(이미지 css js) servlet-context.xml에서 location="/resources/"



bean 태그 안 property는 set!!!!! 


/WEB-INF/views/ + caprisun +.jsp

/WEB-INF/views/caprisun.jsp

뷰 생성하는 역할!!


component Object 정도로 보면 된다 Object를 scan하겠다

없다면 !!!

<bean class="a.b.c.My"></bean> 이렇게 써줘야 컨트롤러로 등록된다 !!!

역할: 등록 안 해도 골뱅이를 다 가져올게! 즉 , 컨트롤러 클래스들을 등록하는 역할!!


root-context.xml

역할: DB관련 설정, Model (대표 DAO) 설정


servlet-context.xml에서

<context:component-scan base-package="com.encore.myapp" />

지우면 hello 안 나온다


※com.encore.myapp 안에 HomeController.java 만들고 Spring container 마크 붙이기~ 

How?

servlet-context.xml에 등록하기

<beans:bean class="com.encore.myapp.HomeController"></beans:bean>


서버 재시작하기 !! 


<MyController.java>

@Controller 

public class MyController{


}


servlet-context.xml에 등록하기

<beans:bean class="com.encore.myapp.HomeController"></beans:bean>

<beans:bean class="com.encore.myapp.MyController"></beans:bean>

==> 패키지가 동일하네 ????

서버 재시작하기 !!

<context:component-scan base-package="com.encore.myapp" /> 등록해주기!!



@RequestMapping(value = "/", method = RequestMethod.GET) / 요청이 들어왔을 때 !!

※Struts 에서 <action path="/">

model은 영역객체의 requset와 같다 

home아 어디로 가니??




Hello World! 를 올리브색으로 바꾸고 한글 깨짐 현상 고치기 !!

올리브색 주기 - style

한글깨짐현상 - contentTpye과 pageEncoding 넣기