spring-mvc是基于servlet实现的,故必须配置web.xml
一.web.xml的配置
1.配置ContextLoadListener
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
当使用编码方式进行配置文件信息传入spring:
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
但是web环境下,用ContextLoadListener对context-param的方式监听的方式注册。ContextLoaderListener
实现了接口ServletContextListener,固启动servlet容器的时候会默认执行它实现的方法。主要就是将配置文件中的bean信息注入spring,创建WebApplicationContext类型实例
2.DispatcherServlet
DispatcherServlet实现了servlet接口(这里指的是java servlet api)
Java Servlet API 是Servlet容器(tomcat)和Servlet之间的接口,它定义了一个Servlet的各种方法,还定义了Servlet容器传给Servlet的对象类,其中最重要的就是ServletRequest和ServletResponse。所以我们在编写Servlet时,需要实现Servlet接口,按照其规定进行操作。
(Servlet = Service + Applet),
servlet:一个java编写的程序,基于http协议的,在服务端运行(如Tomcat),是按照servlet规范编写的一个java类。主要是处理客户端的请求并生成响应返回客户端,servlet的生命周期是由servlet的容器来控制的,它可以分为3个阶段:初始化,运行和销毁。
初始化阶段:servlet容器加载servlet类,把servlet类的.class文件中的数据读到内存中。servlet容器创建一个ServletConfig对象。该对象包含了servlet的初始化配置信息。servlet容器创建一个servle对象。servlet容器调用servlet对象的init方法进行初始化。
运行阶段:当servlet容器接受到一个请求时,servlet容器会针对这个请求创建servletRequest和servletResponse对象,然后调用service方法,并将这两个参数传递给service方法。service方法通过servletRequest对象获得请求的信息,并处理该信息。再通过servletResponse对象生成这个请求的响应结果。然后销毁servletRequest和servletResponse对象。
销毁阶段:当web应用终止servlet容器会先调用servlet对象的destroy方法,然后在销毁servlet对象,同时销毁关联对象,并释放资源等。
Servlet与JSP有什么区别?
1.jsp编译后就是servlet
2.servlet工作于controller层,而jsp位于视图层
3.servlet擅长逻辑编写,而jsp更擅长页面的展示。
web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="schedule-console" version="3.0">
<!-- 配置web.xml,使其具有springmvc特性,主要配置两处,一个是ContextLoaderListener,一个是DispatcherServlet -->
<!-- spring mvc 配置 -->
<!-- 1.配置DispatcherServlet表示,该工程将采用springmvc的方式。 -->
<servlet>
<servlet-name>webmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 启动项目的时候要加载的配置文件 若不指定则将默认为加载/WEB-INF/<servlet-name>-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 2.spring配置 :配置ContextLoaderListener表示,该工程要以spring的方式启动.启动时会默认在/WEB-INF目录下查找applicationContext.xml
作为spring容器的配置文件,该文件里可以初始化一些bean-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!-- 字符集过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
然后是对应applicationContext的配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!-- 启用spring mvc注解 -->
<context:annotation-config />
<bean id="student" class="com.spring.model.Student">
<property name="name" value="123"/>
</bean>
</beans>
以及spring-mvc.servlet的配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 基本包扫描 -->
<context:component-scan base-package="com.spring" />
<!-- 注册HandlerMapper、HandlerAdapter两个映射类,负责将请求映射到类和方法中 -->
<mvc:annotation-driven />
<!-- 访问静态资源,如js, css文件等 -->
<mvc:default-servlet-handler />
<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
ok配置文件都好了,那么就是代码段了
自定义一个LoginController
package com.spring.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LoginController {
@RequestMapping("/index")
public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView("index");
return mav;
}
}
并在web-inf的view文件夹下新建一个index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
ok最后就是讲项目运行在tomcat上了:
若原先没有tomcat创建一个tomcat
可以自己下载 然后填上对应的路径
tips:url中80端口是web服务默认的端口号,所以就不需要显式写这个端口号了