一 SpringMvc概述&入门

 

SpringMVC:

类似Struts2的MVC框架,属于SpringFrameWork的后续产品.

与Struts2的区别:

 Struts2设置成单例,并发访问,拿到的数据不匹配。

SpringMvc入门

  1. 引入jar包
  2. 创建config源目录,在目录下新建springmvc.xml核心配置文件(只需要配置一个Controller扫描就可以了,让Spring对页面控制层进行管理)
  3. 在web.xml中基于Servlet配置前端控制器
  4. 创建hello.jsp
  5. 创建Controller,访问url

 

引入jar包

创建config源目录,在目录下新建springmvc.xml核心配置文件(只需要配置一个Controller扫描就可以了,让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:p="http://www.springframework.org/schema/p"
    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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- 配置controller扫描包 -->
    <context:component-scan base-package="com.springmvc01.controller"></context:component-scan>
    
    
</beans>

在web.xml中基于Servlet配置前端控制器DispatcherServlet,在初始化参数中加入SpringMVC核心配置文件(classpath:从Resource目录下查找)

  <!-- 配置核心控制器,SpringMVC入口是Servlet -->
  <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>
  </servlet>
  <servlet-mapping>
     <servlet-name>springmvc</servlet-name>
     <url-pattern>*.action</url-pattern>
  </servlet-mapping>

创建hello.jsp

 

创建Controller,访问url

猜你喜欢

转载自www.cnblogs.com/ltfxy/p/10415958.html