创建Spring和SpringMVC的框架程序
1、 创建maven项目
创建项目并补齐目录结构
2、 pom.xml文件添加依赖和插件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.daw</groupId>
<artifactId>springmvc</artifactId>
<version>1.0-SNAPSHOT</version>
<!--web项目-->
<packaging>war</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--spring-webmvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<!--springmvc底层还是servlet,所以必须添加serlvet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided </scope><!--插件运行的时候没有范围插件启动会失败-->
</dependency>
</dependencies>
<build>
<plugins>
<!-- 编码和编译和JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
<!--tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
3、 创建Spring和SpringMVC的配置文件
我们一般将除了 Controller 之外的所有 Bean 注册到 Spring 容器中,而将 Controller 注册到 SpringMVC 容器中。所以我们在 resources 目录下添加 applicationContext.xml 作为 spring 的配置, 添加springmvc.xml作为springmvc的配置文件。
3.1 、创建Spring配置文件applicationContext.xml文件
<?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">
<!--spring的配置文件:除了控制器之外的bean对象都在这里扫描 -->
<context:component-scan base-package="com.daw.dao,com.daw.service"></context:component-scan>
</beans>
3.2 、创建SpringMVC的配置文件springmvc.xml
<?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">
<!--springmvc的配置文件:控制器的bean对象都在这里扫描 -->
<context:component-scan base-package="com.daw.controller"></context:component-scan>
</beans>
4 、在web.xml中进行Spring和SpringMVC配置
<?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">
<!--spring配置 -->
<context-param>
<!--contextConfigLocation:表示用于加载 Bean的配置文件-->
<param-name>contextConfigLocation</param-name>
<!--指定spring配置文件的位置
这个配置文件也有一些默认规则,它的配置文件名默认就叫 applicationContext.xml ,
如果将这个配置文件放在 WEB-INF 目录下,那么这里就可以不用 指定配置文件位置,
只需要指定监听器就可以。
这段配置是 Spring 集成 Web 环境的通用配置;一般用于加载除控制器层的 Bean(如
dao、service 等),以便于与其他任何Web框架集成。
-->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--SpringMVC的配置-->
<!--
前端控制器:所有的请求都会经过此控制器,然后通过此控制器分发到各个分控制器.
前端控制器本质上还是一个Servlet,因为SpringMVC底层就是使用Servlet编写的
-->
<!--springmvc的前端/核心/中央控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 创建前端控制器的时候读取springmvc配置文件启动ioc容器 -->
<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>
<!-- 配置拦截路径url,所有以.do结尾的请求都会被前端控制器拦截处理 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--
SpringMVC的配置解析:
1、servlet-class:前端控制器的完全限定名,在spring-webmvc-5.2.5.RELEASE.jar包中的
org.springframework.web.servlet下
2、load-on-startup:标记是否在Web服务器(这里是Tomcat)启动时会创建这个 Servlet 实
例,即是否在 Web 服务器启动时调用执行该 Servlet 的 init()方
法,而不是在真正访问时才创建。 要求取值是整数。
值大于0:表示容器在启动时就加载并初始化这个 servlet,数值越小,该 Servlet的优先级就
越高,其被创建的也就越早
值小于0或者省略:表示该 Servlet 在真正被使用时才会去创建。
值相同:容器会自己选择创建顺序
3、url-pattern:可以写为 / ,可以写为*.do 、*.action、*.mvc等形式,此处先写*.do,以后
介绍不同写法的区别。
4、init-param:表示了springmvc配置文件的名称和位置。如果没有配置,默认在项目的WEB-INF
目录下找名称为 Servlet 名称-servlet.xml 的配置文件。
如果没有配置,启用默认的规则:即如果配置文件放在 webapp/WEB-INF/ 目录下,并且配置文
件的名字等于 DispatcherServlet 的名字+ -servlet(即这里的配置文件路径是 webapp/WEB-INF/dispatcherServlet-servlet.xml),如果是这样的话,可以不用添加 init-param 参数,即不
用手动配置 springmvc 的配置文件,框架会自动加载。
而一般情况下,配置文件是放在类路径下,即 resources 目录下。所以,在注册前端控制器时,
还需要设置查找 SpringMVC 配置文件路径。
其中contextConfigLocation属性:来自DispatcherServlet的父类FrameworkServlet,
该类中的contextConfigLocation属性用来配置springmvc的路径和名称。
-->
</web-app>
5、 创建控制器
package com.daw.controller;
import com.daw.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* ClassName: TeamController
*
* @author wanglina
* @version 1.0
*/
@Controller
public class TeamController {
@Autowired
private TeamService teamService;
@RequestMapping("hello.do")
public ModelAndView hello(){
System.out.println("TeamController----add---");
teamService.add();
ModelAndView mv=new ModelAndView();
mv.addObject("teamName","湖人");//相当于request.setAttrubuite("teanName","湖人");
mv.setViewName("index");//未来经过springmvc的视图解析器处理,转换成物理资源路径,相当于request.getRequestDispatcher("index.jsp").forward();
//经过InternalResourceViewResolver对象的处理之后加上前后缀就变为了 /jsp/index.jsp
return mv;
}
}
package com.daw.service;
import org.springframework.stereotype.Service;
/**
* ClassName: TeamService
*
* @author wanglina
* @version 1.0
*/
@Service
public class TeamService {
public void add(){
System.out.println("TeamService---- add-----");
}
}
6 、配置视图解析器
在springmvc.xml配置文件中添加视图解析器的配置
<!--视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:annotation-driven/>
<!--annotation-driven是一种简写形式,也可以手动配置替代这种简写形式,简写形式可以让初学者快速
应用默认配置方案。
该注解会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter
两个bean,是springMVC为@Controller分发用户请求所必须的,解决了@Controller注解使用的前提配
置。
同时它还提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支
持,读写XML的支持(JAXB,读写JSON的支持(Jackson)。我们处理响应ajax请求时,就使用到了对json
的支持(配置之后,在加入了jackson的core和mapper包之后,不写配置文件也能自动转换成json)。
-->
7 、编写index.jsp页面
webapp文件夹下面创建文件夹jsp,然后jsp文件夹中添加index.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
</head>
<body>
<h1>index---------------${teamName}</h1>
</body>
</html>
8、测试
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YyUjJma1-1629122148432)(C:/Users/Mr-DUAN/AppData/Roaming/Typora/typora-user-images/image-20210816215424067.png)]
将刚刚的地址复制到浏览器之后追加控制中add方法的访问路径hello.do,看到如下页面表示运行成功!
tentType=“text/html;charset=UTF-8” language=“java” %>
index---------------${teamName}
## 8、测试
![在这里插入图片描述](https://img-blog.csdnimg.cn/95491319c5e8480497187b47c5f78222.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L01yREFX,size_16,color_FFFFFF,t_70)
将刚刚的地址复制到浏览器之后追加控制中add方法的访问路径hello.do,看到如下页面表示运行成功!
![在这里插入图片描述](https://img-blog.csdnimg.cn/91b018d712784d2da232abbbaddb51ca.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L01yREFX,size_16,color_FFFFFF,t_70)