使用注解方式搭建SpringMVC环境

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

开发工具与关键技术:java,搭建SpringMVC环境

撰写时间:2020-05-28

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

第一步:导包(有些是多的,不影响效果)

第二步:全局配置文件

包括有两个配置文件

第一个是(web.xml)

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"

      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_3_0.xsd">

  <servlet>

         <servlet-name>jqk</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>

         <!-- 自启动 -->

         <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

         <servlet-name>jqk</servlet-name>

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

  </servlet-mapping>

</web-app>

 

第二个是(springmvc.xml当然这个名字随意,只是标准一点)

springMVCservlet.xml配置文件默认存放在/WEB-INF/文件下的

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:context="http://www.springframework.org/schema/context"

      xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="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

         http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   <!-- 扫描注解 ,base-package:扫描的包-->

   <context:component-scan base-package="com.demo.controller"></context:component-scan>

   <!-- 注解驱动 -->

   <!-- org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping -->

   <!-- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter -->

   <!-- mvc相当于配置了上面两个 -->

   <mvc:annotation-driven></mvc:annotation-driven>

   <!-- 静态资源 。例如js、css文件-->

   <!-- 为什么要配置这个,因为web配置文件中拦截了除了jsp外所有文件,所以js与css文件是不能拦截的。 -->

  <!-- 路径:location:到什么文件夹上找到可以找到当前配置的类型文件-->

  <!-- 文件类型名称:mapping :其中"/abc"是浏览器请求的路径类型(例如:localhost:8080/springmvc/abc/jQuery.js)"/**"通配符,

  就是匹配其中所有的文件:其中的一个通配符就代表其第一个级的子文件,双通配符代表其中子文件以及子文件中的子文件,

  通俗表达就是:包括其中所有的子文件(所有层子文件)-->

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

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

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

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

   </beans>

Controller类

package com.demo.controller;



import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;



//通过@Controller注解交给容器管理,不用通过实现或者继承

@Controller

public class DemoController {

      //@RequestMapping映射路径

      @RequestMapping("demo1")

           public String demo(){

           System.out.println("执行demo");

                 return "/main.jsp";

           }

      @RequestMapping("demo2")

      public String demo1(){

           System.out.println("执行demo");

           return "/index.jsp";

      }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值