<?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 http://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

              classpath:/common.xml

             </param-value>

//한개 이상의 설정 파일을 사용하거나 이름이 [이름]-servlet.xml형식이 아닌 파일을 사용해야 한다면 contextConfigLocation 초기화 파라미터로 설정 파일 목록을 지정하면된다.

//각 설정 파일의 경로는 웹 어플리케이션 루트디렉터리를 기준으로 하며 

file: 이나 classpath: 접두어를 이용해서 로컬 파일이나 클래스패스에 위치한 파일을 사용할 수 있다.

</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>


<servlet-mapping>

<servlet-name>appServlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>


</web-app>


  //@Configuration 클래스를 이용해서 설정 정보를 작성했다면, 다음과같이 해야된다.



블로그 이미지

왕왕왕왕

,


 구성요소

설명 

DispatcherServlet 

클라이언트의 요청을 전달 받는다. 

HandlerMapping 

클라이언트의 요청 URL을 어떤 컨트롤러가 처리할지 결정한다. 

HandlerAdapter 

DispatcherServlet의 처리 요청을 변환해서 컨트롤러에게 전달하고

컨트롤러의 응답 결과를 DispatcherServlet이 요구하는 형식으로 변환한다. 

Controller

클라이언트의 요청을 처리한뒤 결과를 리턴한다. 

ModelAndView 

컨트롤러가 처리한 결과 정보 및 뷰 선택에 필요한 정보를 담는다. 

ViewResolver 

컨트롤러의 처리 결과를 보여줄 뷰를 결정한다. 

View 

컨트롤러의 처리 결과 화면을 생성한다. 


블로그 이미지

왕왕왕왕

,

<filter>

<filter-name>encodingFilter</filter-name>

<filter-class>

             org.springframework.web.filter.CharacterEncodingFileter

             </filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encofingFileter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>



스프링은 요청 파라미터의 캐릭터 인코딩을 지정할 수 있는 서블릿 필터 CharacterEncodingfilter를 제공하고 있으므로, 요청 파라미터의 캐릭터 인코딩 처리를 위해 별도의 코드를 작성할 필요가 없다.

블로그 이미지

왕왕왕왕

,

<?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 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/" />

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

        //리소스폴더를 추가한경우 맵핑 해줘야 된다.


<!-- 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.king.spring" />

</beans:beans>



컨트롤러에서 반환된 문자열과 prefix + 문자열 + suffix 조합으로 연결


블로그 이미지

왕왕왕왕

,


@Controller

public class HomeController {


private static final Logger logger =          LoggerFactory.getLogger(HomeController.class);


/**

* Simply selects the home view to render by returning its name.

*/

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

public String home(Model model) {


model.addAttribute("serverTime", formattedDate);


return "home";

}


}



프로젝트에서 src/main/java/ 하위에 HomeController.java가 있다.


@RequestMapping 에 value값으로 URL뒤에 /home으로 접근하면 home메소드가 작동한다.


전송 방식은 GET방식으로 전송한다.


Attribute추가하고, home라는 문자열을 반환한다.


리턴값은 실제 뷰페이지명을 입력한다. ex)home.jsp

블로그 이미지

왕왕왕왕

,


프로젝트 생성하면 이런 구조를 갖는데, WEB-INF디렉토리 하위에 위치한 home.jsp는 

XML에 매핑에 영향을 받아서 매핑된 주소로 접근해야된다.


index.jsp는 WEB-INF디렉토리 상위에 위치하기 때문에, 현재 경로로 바로 접근가능하다.


index.jsp를 실행하면 home.jsp로 바로 넘어갈 수 있도록 구성했다.



index.jsp



<html>

<head>

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

<title>Insert title here</title>

</head>

<body onload="javascript:location.href='/king/view'">

//패키지명/매핑문자열을 써주면 첫페이지 거치면서 바로 매핑된페이지를 불러온다.

</body>

</html>

'JAVA > Spring 4.0' 카테고리의 다른 글

Spring4.0 스프링 MVC의 주요 구성요소  (0) 2015.09.16
Spring4.0 web.xml 캐릭터 인코딩 필터 설정  (0) 2015.09.16
Spring4.0 servlet-context.xml  (0) 2015.09.16
Spring4.0 HomeController.java  (0) 2015.09.16
Spring4.0 시작하기  (0) 2015.09.10
블로그 이미지

왕왕왕왕

,

JDK 1.7이상

톰캣7 이상

UTF-8


블로그 이미지

왕왕왕왕

,