SpringMVC basic configuration

SpringMVC uses steps:
            - Add jar package
            - Configure DispatcherServlet in web.xml
            - Add Spring MVC configuration file
            - Write a processor that handles requests and identify it as a processor
            - Write a view

Specific steps for using SpringMVC:
1. Create a dynamic web project in eclipse;
1. Import the jar package (springMVC related);
            –commons-logging-1.1.3.jar –spring
            -aop-4.0.0.RELEASE.jar
            –spring -beans-4.0.0.RELEASE.jar –spring
            -context-4.0.0.RELEASE.jar –spring
            -core-4.0.0.RELEASE.jar –spring
            -expression-4.0.0.RELEASE.jar –spring
            -web -4.0.0.RELEASE.jar –spring
            -webmvc-4.0.0.RELEASE.jar
2. Configure the core controller (DispatcherServlet); (configured in the web-xml file) (replaces the servlet explained earlier with springMVC)
(Alt+/selects the penultimate dispatcherservlet)

 
<!-- The front controller (directly facing the front end, (official website: www.fhadmin.org) page requests can be sent here) --> 
    <!-- The front controller of this Spring Web application, responsible for handling all application requests --> 
    <!-- After importing the jar package. Configure the start class (general control class) of springMVC in the package to the server, let the server know that there is springMVC, so that it can be used in the future --> 
    <!-- Register the front-end controller with the server --> 
    <!-- Everything is configured here (web.xml) are all singletons --> 
    < servlet > 
        < servlet-name > springDispatcherServlet </ servlet-name > 
        <!-- DispatcherServlet refers to the SpringMVC front-end configurator (refers to the SpringMVC front-end configuration above), the full class name is used by the server, and the servlet object is created --> 
        <!--> 
        < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class > 
        <!-- The initialization parameter contextConfigLocation is used to specify the path and name of the SpringMVC configuration file --> 
        <!-- Set springMVC itself The location and name of the configuration file (official website: www.fhadmin.org) --> 
        < init-param > 
            < param-name > contextConfigLocation </ param-name > 
            < param-value > classpath:springmvc.xml </ param- value > 
            <!-- classpath refers to the src file, here means to put the springMVC configuration file in the springmvc.xml file under the src file --> 
        </init-param> 
        <!-- The number 0 or a number greater than 0 means that the servlet object is created when the server starts, not when it is accessed for the first time
            A number less than 0 means: wear a servlet object at the first access,
            In the case of a number greater than 0, the smaller the number, the higher the priority!
         -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling --> 
    <!-- Use servlet-mapping to specify the path to request mapping --> 
    < servlet-mapping > 
        < servlet-name > springDispatcherServlet </ servlet-name > 
        <! --Use url - pattern to specify the intercepted request path --> 
        < url-pattern > / </ url-pattern > <!-- REST requires simplicity, and * is not recommended here. action (may report an error) --> 
    </ servlet-mapping >
 
 
1 <init-param>
2     <param-name>contextConfigLocation</param-name>
3     <param-value>classpath:springmvc.xml</param-value>
4     <!-- classpath指的是src文件,此处表示将springMVC(官网:www.fhadmin.org)配置文件放在src文件下的springmvc.xml文件中 -->
5 </init-param>
6 此处若未配置,即当我们不用init-param指定springMVC配置文件的路径好位置的时候,SpringMVC默认会去
7             /WEB-INF/<servlet-name>-servlet.xml
8          (在<servlet-name>对应名称后加-servlet,此时将springmvc.xml中的内容改写到<servlet-name>-servlet.xml中)
 

3、创建springMVC配置文件:
    1)、文件名对应web.xml初始化处的文件名;
    2)、使用Spring Bean Configuration File 创建的,不是XML!
选中beans、context、mvc三个选项,点击Next,Finish,则文件创建完成。

4、配置springMVC配置文件:(文件左上角有S标识表示类已加入容器)
    1)、在springmvc.xml中配置SpringMVC容器要扫描的包:(com.neuedu.controller为包名)
            <context:component-scan base-package="com.neuedu.controller"></context:component-scan>
    2)、在com.neuedu.controller包下添加(控制器类)类如AController,注意加入@Controller注解。
    只要在类窗口前添加@Controller注解,则包下的类便加入了springMVC容器,在容器中生成了一个对象(默认的)(springmvc.xml视为一个容器,相当于一个Map)
    其中Map的键为类的首字母小写(如aController),值为类的一个对象

5、在类中(如AController)添加方法使用springMVC
    1)、在类的方法上使用@RequestMapping注解,在括号里指定请求url的请求路径!

 
 1  /*
 2          * 我们通过@RequestMapping做映射请求
 3          * @return(官网:www.fhadmin.org)
 4          */
 5          //括号内路径默认对应value值(即value="/sayHello")
 6          //()内为多个属性时,value不可省略
 7         @RequestMapping("/sayHello")
 8         public String sayHellolo() {
 9             return "/WEB-INF/view/success.jsp";//(官网:www.fhadmin.org)通过返回值转发到success.jsp页面
10         }
 

        请求路径名不必和方法名一致,但需和页面中的请求路径名一致。
    2)、创建index.jsp 文件(创建页面):  

 <a href="${pageContext.request.contextPath}/sayHello">测试Say Hello!</a>

 **若想将springmvc.xml单独放一个文件内,可点击选择source Folder创建文件,其相当于动态web项目中的src。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326212566&siteId=291194637