spring整合表现层框架springMVC

springMVC本身是属于spring的衍生框架,所以整合起来很简单,只要包jar依赖进来,然后增加springmvc的配置即可,前提是已经了解并合理使用spring容器

1、导入maven依赖:

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>4.3.8.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>4.3.8.RELEASEversion>
        dependency>

2、配置web.xml
springMVC是一个基于servlet实现的框架,所以web就是一段servlet的配置
而在web项目中,我们需要吧创建spring容器的操作交给tomcat,所以需要配置自动spring容器的监听器,同时制定xml文件的位置



<web-app>
    <display-name>Archetype Created Web Applicationdisplay-name>
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring-core.xmlparam-value>
    context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <servlet>
        <servlet-name>actionservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-mvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>actionservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>


web-app>

配置spring-mvc.xml


<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:util="http://www.springframework.org/schema/util"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 


  
  
  <context:component-scan base-package="com.xingxue.controller"/>      
   <mvc:annotation-driven/>

        
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"/>
        <property name="suffix" value=".jsp">property> 
    bean>     
beans>

配置spring-core.xml


<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cache="http://www.springframework.org/schema/cache" 
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
     
    <context:component-scan base-package="com.xingxue" >
     context:component-scan>



beans>

编写cotroller, @RequestMapping: 表示请求隐射地址

package com.xingxue.controller;

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

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login.do")
    public String login() {
        System.out.println("11111111111");
        return "OK";
    }

    @RequestMapping("/register.do")
    public String register() {
        return "OK";
    }

    public String add() {

        return "LIST";
    }

}

编写页面请求路径

<html>
<body>
<h2>Hello World!h2>

<a href="<%=request.getContextPath()%>/user/login.do">spring MVCa>
body>
html>

发布运行即可!

springMVC运行原理:
spring整合表现层框架springMVC_第1张图片

1、springMVC是基于servlet来进行实现的。
2、请求的流程,我们的http请求直接交到我们的DispatcherServlet里面,他通过读取配置文件,解析我们的请求路径找到对应controller来处理这个功能。当controller处理完成这个功能之后吧结果反馈给我们的disatcherServlet。dispacherServlet通过使用视图解析器组装数据,跳转到对应页面。
3、springmvc是单利模式,效率高, 参数传力利用的参数的方式,线程安全

你可能感兴趣的:(springMVC)