간단 한 사례 에서 Spring MVC 를 손 에 넣 고 Spring MVC 면접 문 제 를 분석 하 다.

많은 회사 들 이 Spring MVC 를 사용 하고 초급 프로그래머 들 이 면접 을 볼 때 반드시 이 방면 의 문 제 를 물 을 것 이다. 그래서 우 리 는 간단 한 사례 를 통 해 Spring MVC 를 분석 할 것 이다. 사실은 우 리 는 교육 과정 에서 이 예 를 들 어 많은 기초 프로그래머 들 이 이 걸 빨리 사용 할 수 있다.
   본 논문 의 문자 와 사례 는 자바 웹 경량급 개발 면접 튜 토리 얼 에 따라 개편 되 었 다.
1 Spring MVC 코드 설명
    절차 1. 웹 프로젝트 를 만 들 고 웹. xml 를 작성 합 니 다. Spring 을 사용 하 는 MVC 를 지정 합 니 다. 주요 코드 는 다음 과 같 습 니 다.    
1	
2	spring	org.springframework.web.servlet.DispatcherServlet
3	1
4	
5	
6		spring
7		/
8	

    servlet 과 servlet - mapping 을 정의 합 니 다. 보통 쌍 을 지어 나타 납 니 다.첫 번 째 줄 에서 네 번 째 줄 까지 의 servlet 요소 에 서 는 Spring 이라는 Servlet 을 정의 하고 두 번 째 줄 에 서 는 Spring 의 Dispatcher Servlet 를 지정 합 니 다.즉, 이 프로젝트 에 서 는 Spring 의 MVC 처리 류 를 사 용 했 으 며, 세 번 째 줄 에 서 는 load - on - startup 을 통 해 이 Servlet 를 Spring 용기 에 불 러 올 때 불 러 올 것 이 라 고 지정 한 것 이다.
    5 줄 에서 8 줄 까지 의 servlet - mapping 에 서 는 7 줄 의 url - pattern 을 통 해 모든 요청 이 Spring 이라는 Servlet 에 의 해 처 리 됩 니 다.
    보통 localhost: 8080 / 프로젝트 이름 / index. jsp (IP 주소: 포트 번호 / 프로젝트 이름 / 디 렉 터 리 이름 / JSP 파일 이름) 방식 으로 웹 프로그램 을 실행 합 니 다. 여기 url - pattern 은 / 입 니 다.즉, IP 주소: 포트 번호 / 프로젝트 이름 의 모든 URL 요청 은 Spring 이라는 Servlet 에 의 해 처 리 됩 니 다.   
    둘째, 컨트롤 러 역할 을 담당 하 는 RestController 류 를 개발 한다.
1	package com.mvc.rest;
2	import javax.servlet.http.HttpServletRequest;
3	import javax.servlet.http.HttpServletResponse;
4	import org.springframework.stereotype.Controller;
5	import org.springframework.ui.ModelMap;
6	import org.springframework.web.bind.annotation.PathVariable;
7	import org.springframework.web.bind.annotation.RequestMapping;
8	import org.springframework.web.bind.annotation.RequestMethod;
9	import org.springframework.web.servlet.ModelAndView;
10	
11	@Controller
12	public class RestController {
13	    public RestController(){   }
14	
15	    @RequestMapping(value = "/login/{userName}", method = RequestMethod.GET)   
16	    public ModelAndView myMethod(HttpServletRequest request, HttpServletResponse response,             @PathVariable("userName") String userName, ModelMap modelMap) throws Exception {  
17	        modelMap.put("loginUser", userName);
18	        return new ModelAndView("/login/hello", modelMap);
19	    }   
20	//          Get      /welcome   
21	@RequestMapping(value = "/welcome", method = RequestMethod.GET)  
22	        public String registPost() {  
23	         return "/welcome";
24	    }  
25	}

    11 줄 에 서 는 @ Controller 라 는 주 해 를 통 해 RestController 류 가 컨트롤 러 의 역할 을 맡 았 음 을 설명 합 니 다.그 중에서 16 줄 과 21 줄 에서 각각 두 개의 처리 함 수 를 정의 했다.먼저 비교적 간단 한 registPost 방법 을 살 펴 보 겠 습 니 다.
    21 줄 의 @ RequestMapping 이라는 주 해 는 Get 방법 으로 요청 을 처리 하 는 것 을 method 를 통 해 설명 합 니 다. 예 를 들 어 localhost: 8080 / SpringMVC 데모 / welcome 의 요청 이 있 으 면 이 registPost 방법 으로 처리 합 니 다.다른 my Method 방법 은 비교적 복잡 하 다.
1	@RequestMapping(value = "/login/{userName}", method = RequestMethod.GET)
2	public ModelAndView myMethod(HttpServletRequest request, HttpServletResponse response,             @PathVariable("userName") String userName, ModelMap modelMap) throws Exception {  
3	        modelMap.put("loginUser", userName);
4	        return new ModelAndView("/login/hello", modelMap);
5	}

    사용자 가 localhost: 8080 / SpringMVC 데모 / login / 로그 인 이름 을 입력 하면 이 방법 은 Get 방식 으로 처 리 됩 니 다.여기 value 의 표기 법 은 "/ log in / {userName}" 입 니 다. 즉, localhost: 8080 / SpringMVC 데모 / log in / 자바 를 입력 하면 해당 하 는 userName 값 은 자바 입 니 다.
    세 번 째 줄 에 서 는 modelpap 의 put 방법 을 통 해 userName 을 loginUser 라 는 속성 에 넣 습 니 다. modelpap 대상 은 보통 다른 페이지 로 데 이 터 를 전달 합 니 다. 네 번 째 줄 에 서 는 ModelAndView 대상 을 통 해 loginUser 인 자 를 포함 한 modelpap 대상 을 가지 고 / login / hello 페이지 로 이동 합 니 다.
    세 번 째 단계 에서 Spring MVC 를 위 한 프로필 을 작성 합 니 다. 이 프로필 의 이름 은 마음대로 지 을 수 있 습 니 다. 일반적으로 웹. xml 과 같은 디 렉 터 리 에 놓 여 있 습 니 다. 이 프로필 의 핵심 코드 는 다음 과 같 습 니 다.   
1	
2	
3	
4	
5	
6	
7	
8	
9	

    그 중에서 네 번 째 줄 의 코드 는 주해 모드 를 열 수 있 습 니 다. @ Controller 와 같은 주해 가 적 용 됩 니 다. 여섯 번 째 줄 에서 아홉 번 째 줄 의 코드 는 요청 에 접두사 와 접 두 사 를 붙 입 니 다.
    4 단계 jsp 파일 두 개 를 작성 합 니 다. hello. jsp 는 Webroot / login 디 렉 터 리 에 놓 여 있 습 니 다. hello. jsp 코드 를 먼저 보 세 요.    
1	
2	
3	
4	
5	
6	
7	hello:
8	
9	
</code></pre> 
 </div> 
 <p>      7    loginUser  。welcome.jsp       ,    7     welcome  。    </p> 
 <div class="cnblogs_Highlighter"> 
  <pre><code>1	
2	
3	<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
4	<title/>
5	
6	
7	welcome
8	
9	
</code></pre> 
 </div> 
 <p>     Spring MVC Struts         ,  Spring                   ,              ,        Spring  。</p> 
 <p>                   。</p> 
 <p>       ,  Tomcat    ,  http://localhost:8080/SpringMVCDemo/welcome      。</p> 
 <p>①      URL   ,  web.xml   ,     Spring DispatcherServlet   。</p> 
 <p>②  @Controller  ,  RestController          。            @RequestMapping。</p> 
 <p>③ /welcome  registPost    ,       return "/welcome";  ,      ,      .jsp   ,    ,    welcome.jsp   。</p> 
 <p>④  welcome.jsp  ,  welcome   。</p> 
 <p>       ,  http://localhost:8080/SpringMVCDemo/login/java      。 </p> 
 <p>           RestController       @RequestMapping,        myMethod    。</p> 
 <p>      @RequestMapping value ,  userName   java,  4  java      loginUser ,     /login/hello     。</p> 
 <p>             .jsp,   /login/hello.jsp   ,     hello:java   。</p> 
 <p>               Spring MVC   ,              。</p> 
 <p>      ,URL      DispatcherServlet  。</p> 
 <p>      ,     DispatcherServlet     ,    HandlerMapping           Controller。</p> 
 <p>         HandlerMapping            ,       。</p> 
 <p>      SimpleUrlHandlerMapping,          , URL   Controller 。</p> 
 <p>      DefaultAnnotationHandlerMapping,        ,   URL      Controller  。</p> 
 <p>      ,   URL    HandlerMapping ,HandlerMapping           Controller        。</p> 
 <p>      ,Controller     ,    ModelAndView                    。</p> 
 <h1><span style="font-size: 18pt;">2 Spring MVC       </span></h1> 
 <p>    Java        Web  ,      JD(    )        Spring     ,         Spring Web      ,           Web   ,         Spring    。</p> 
 <p>               Java     (3       ),     Spring     ,             ,                  。</p> 
 <div> 
  <table style="width: 563px;" border="1"> 
   <tbody> 
    <tr> 
     <td width="173"> <p class="a"><strong>    </strong></p> </td> 
     <td width="166"> <p class="a"><strong>    </strong></p> </td> 
     <td valign="top" width="224"> <p class="a"><strong>     </strong></p> </td> 
    </tr> 
    <tr> 
     <td width="173"> <p class="a">      1  (                ),       Spring     ,      Web     </p> </td> 
     <td width="166"> <p class="a"> </p> </td> 
     <td valign="top" width="224"> <p class="a">              (    ,                       )            7 </p> </td> 
    </tr> 
    <tr> 
     <td width="173"> <p class="a">    2 3 ,         Spring  Java Web      </p> </td> 
     <td width="166"> <p class="a"> </p> </td> 
     <td valign="top" width="224"> <p class="a">             ,Java          ,         ,           ,    “          ”   </p> </td> 
    </tr> 
    <tr> 
     <td rowspan="3" width="173"> <p class="a">    2 3 ,    Spring    ,                      ,         1    </p> </td> 
     <td width="166"> <p class="a">            Spring   ,                    ,  Spring         </p> </td> 
     <td valign="top" width="224"> <p class="a">  Spring      ,           ,       ,    “  Spring       ”,           ,                </p> </td> 
    </tr> 
    <tr> 
     <td width="166"> <p class="a">         IoC AOP   ,       Spring MVC   ,            ,           ,           。</p> </td> 
     <td valign="top" width="224"> <p class="a">  Spring     ,          :  Spring   ,            </p> <p class="a">                      ,          ,                </p> </td> 
    </tr> 
    <tr> 
     <td width="166"> <p class="a">                       ,          ,      xxx   URL         ,   xxx,                ,   xxx         。                  ,      Spring  </p> </td> 
     <td valign="top" width="224"> <p class="a">             ,            </p> <p class="a">                  。              ,             ,            ,        </p> <p class="a">           ,                   </p> </td> 
    </tr> 
    <tr> 
     <td width="173"> <p class="a"> </p> </td> 
     <td width="166"> <p class="a">            ,             ,    </p> </td> 
     <td valign="top" width="224"> <p class="a"> </p> </td> 
    </tr> 
    <tr> 
     <td width="173"> <p class="a"> </p> </td> 
     <td width="166"> <p class="a">           ,       Spring   </p> </td> 
     <td valign="top" width="224"> <p class="a">   ,              ,               </p> </td> 
    </tr> 
    <tr> 
     <td rowspan="2" width="173"> <p class="a"> </p> </td> 
     <td width="166"> <p class="a"> </p> </td> 
     <td valign="top" width="224"> <p class="a"> </p> </td> 
    </tr> 
    <tr> 
     <td width="166"> <p class="a"> </p> </td> 
     <td valign="top" width="224"> <p class="a"> </p> </td> 
    </tr> 
   </tbody> 
  </table> 
 </div> 
 <p>                             ,            。              ,                 ,           1           2     。</p> 
 <p>              ,       “         ” ,     Spring     ,              ,       ,       ,          xxx  。</p> 
 <p>    </p> 
 <p>    </p> 
</div>
                            </div>
                        </div>

좋은 웹페이지 즐겨찾기