SpringMVC(一)初识springMVC(MVC的作用,MVC配置详解,静态资源的访问)

首先温习一下我们前面所学过的几个框架:

mybatis :

我们学习的目的是为了减少java代码和sql语句之间的耦合度,可以是用户更加的专注于sql语句而不在于java代码本身。

spring:

解决了业务层和其他层的耦合度。

springMVC:

解决了java代码和servlet之间的耦合程度。

没有springMVC时,出现的问题:

学到了现在我们发现,当我们使用servlet时,局限性非常之高,因为是Tomcat创建servlet,通过init中创建service,并且service方法是将所有的请求汇集在一起,然后通过请求路径中的method来判断什么方法,然后if ..else..来进入我们想要进入的方法才可以,学习springMVC就能够解决我们这些出现的问题,来使后台的代码更加的简化。

 

SpringMVC是如何来进行“干涉”我们的代码的?

我们学过servlet都清楚,可以配置一个BaseServlet来进行不同servlet的访问,这个BaseServlet相当于一个引导者,它把所有的访问的请求都接受,然后一一指向他们该去的方法中,springMVC也是这样处理的,只不过更加的完善BaseServlet

代码部分:

web-inf文件:

<?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>dispatcherServlet</servlet-name>
<!--        前端控制器,前端控制器需要找springmvc的配置,默认实在web-inf下面找servletname-servlet.xml 文件-->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--        因为我们没有将文件放在web-inf下,所以需要告诉mvc文件的位置和名字,必须要写classpath-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:*.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
<!--        所有的非jsp请求都会通过这个控制器-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

springmvc.xml文件配置:

这里还添加对静态资源的访问配置!!!!!

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.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">
<!-- 因为我们要在controller里添加注解 @controller所以需要注解扫描-->
    <context:component-scan base-package="com.bjsxt.controller"></context:component-scan>
<!--    因为要用到@RequestMapping映射访问路径,这个又是mvc提供的,所以要加注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>
<!--文件中会有静态资源,但是前台控制器除了jsp的页面都会放行
    这个时候我们要特别的告诉mvc,访问路径是/img/**这个文件下的所有文件全部不接受
    location就是本地资源路径-->
    <mvc:resources mapping="/img/**" location="/img/"></mvc:resources>
</beans>

controller层

package com.bjsxt.controller;

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

//创建对象
@Controller
public class MyController {
//    访问路径
    @RequestMapping("/abc")
    public String show(){
        System.out.println("a");
//        转发的方式进行页面的跳转,如果是重定向 url的地址会发生变化
        return "/success.jsp";
    }
}

这样我们就可以通过/abc来访问我们的success.jsp页面啦!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值