一、配置web.

\"\"\"\"
<?  version="1.0" encoding="UTF-8"?>
<web-app  ns="http:// ns.jcp.org/ /ns/javaee"
          ns:xsi="http://www.w3.org/2001/ Schema-instance"
         xsi:schemaLocation="http:// ns.jcp.org/ /ns/javaee http:// ns.jcp.org/ /ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!-- 配置请求总控器 -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.spring work.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:dispatcher-servlet. </param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
View Code

二、配置dispatcher-servlet.

<?  version="1.0" encoding="UTF-8"?>
<beans  ns="http://www.spring work.org/schema/beans"
        ns:xsi="http://www.w3.org/2001/ Schema-instance"
        ns:context="http://www.spring work.org/schema/context"
        ns:mvc="http://www.spring work.org/schema/mvc"
       xsi:schemaLocation="http://www.spring work.org/schema/beans
       http://www.spring work.org/schema/beans/spring-beans.xsd
       http://www.spring work.org/schema/context
       http://www.spring work.org/schema/context/spring-context.xsd
       http://www.spring work.org/schema/mvc
       http://www.spring work.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan  -package="edu.nf.ch03.controller"/>

    <mvc:annotation-driven/>

    <!-- 静态资源的处理 -->
    <!-- 方式一:将静态页面等资源交由给servlet容器来处理,Springmvc不参与解析-->
    <!-- 任何的servlet容器都有一个DefaultServlet来处理静态资源-->
    <!--<mvc:default-servlet-handler/>-->

    <!-- 方式二:静态资源由springmvc自己来处理-->
    <!-- mapping用于映射静态资源的url,location用于指定静态资源的本地相对路径-->
    <!-- 意思就是将mapping映射的请求url到location指定的相应目录中查找静态资源,
         location实行可以同时指定多个目录,中间用逗号隔开-->
    <mvc:resources mapping="/page/**" location="/static/,/assesst/"/>

    <bean class="org.spring work.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

三、controller类:

\"\"\"\"
package edu.nf.ch03.controller;

import org.spring work.stereotype.Controller;
import org.spring work.web.bind.annotation.GetMapping;
import org.spring work.web.servlet.ModelAndView;

/**
 * @author wangl
 * @date 2018/10/29
 */
@Controller
public class TestController {

    @GetMapping("/test")
    public ModelAndView test(){
        System.out.println("test...");
        return new ModelAndView("index");
    }
}
View Code
收藏 打印