美文网首页
注解方式环境搭建

注解方式环境搭建

作者: Yanl__ | 来源:发表于2019-12-12 10:42 被阅读0次

    注解方式

    1. 导入jar包


    2. 配置前端控制器

    // web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <!--    配置前端控制器-->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--        修改配置文件路径和名称-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            <!--        设置自启动,启动tomcat时,自动加载-->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
            <!--        拦截模式设置为 /  表示除了jsp 都拦截-->
        </servlet-mapping>
    </web-app>
    
    1. 配置SpringMVC的配置文件
    1. 上面需要插入新的命名空间,mvc
    2. 要使用注解,必须进行注解扫描
      由SpringMVC进行扫描,放在SpringMVC容器中,不能放在Spring
    3. 注解驱动
      相当于配了HandlerMapping 和 HandlerAdapter
    4. 声明静态资源,让Spring从指定位置去找
      由于拦截模式设置的 / ,会拦截静态资源,所以需要放行静态资源,声明位置,去指定地方找
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
            <!--   1. 上面需要插入新的命名空间,mvc    -->
    
        <!--   2. 要使用注解,必须进行注解扫描-->
        <context:component-scan base-package="com.steer.controller "></context:component-scan>
        <!--   3.  注解驱动-->
        <!--    相当于配了HandlerMapping 和  HandlerAdapter-->
        <mvc:annotation-driven></mvc:annotation-driven>
        <!--    web.xml中设置拦截模式为 / ,除类jsp以外都拦截 -->
        <!--    4.  声明静态资源,让Spring从指定位置去找-->
        <mvc:resources mapping="/js/" location="/js/**"></mvc:resources>
    </beans>
    

    <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>

    • mapping="/js/**" 表示js下的所有子文件
    • mapping="/js/**" 表示js下的所有子文件及子文件夹下的所有文件
      当请求(浏览器地址格式)符合这种格式的,就从location中去找
    1. 编写控制器类
    // 添加Controller注解,就相当于把类交给容器管理
    @Controller
    public class DemoController {
        // 请求映射,浏览器发送跳转demo请求,就执行该方法
        @RequestMapping("demo")
        public String demo(){
            System.out.println("执行demo");
            // 相对路径
            // requestmapping的路径
    //        return "demo.jsp";
            // 全路径
            // 跳转界面
            return "/demo.jsp";
        }
    
        @RequestMapping("demo2")
        public String demo2(){
            System.out.println("demo2");
            return "/demo2.jsp";
        }
    }
    

    相关文章

      网友评论

          本文标题:注解方式环境搭建

          本文链接:https://www.haomeiwen.com/subject/oepzgctx.html