一.spring MVC的特点:
- 轻量级,简单易学
- 高效,基于请求响应的MVC架构
- 与spring的兼容性好,无缝结合
- 约定优于配置
- 功能强大:RESTful,数据验证,格式化,本地化,主题等;
Spring的web框架围绕DispatcherServet
DispatcherServet相当于一个中心调度器,它简化了我们原来的配置,必须要配置多个servlet,现在就可以只配置一个DispatcherServet,然后由它去协调控制我们的这几个servlet,由他来统一请求和控制转发。
本质上DispatcherServet也是一个servlet,只是写他的人写了更多的功能,让他凌驾其它层以上,用于控制请求转发。
二.认识spring MVC
环境搭建:由于springMVC简化了我们的配置,所以这里我们只用配置一个DispatcherServet由它来控制转发所以web.xml就显得很简单了
<?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="3.1" metadata-complete="true"> <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-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
我们的web.xml就简单的配置了一个DispatcherServet,然后的配置都在spring来配置了
springmvc-servlet.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:context="http://www.springframework.org/schema/context" 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"> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/page/"/> <!--后缀--> <property name="suffix" value=".jsp"/> </bean> <bean id="/hello" class="top.lostyou.controller.HellowController"/> </beans>
这里的spring的配置,简单的看也不难,因为前三个<bean>都是springmvc的固定配置,不需要过多的深究,我们自己要的配置主要在下面;
每当我们需要访问一个页面的时候,就需要在spring中注册一个<bean>就可以了,简化了注册方式
控制层接口:
package top.lostyou.controller; import org.springframework.stereotype.Controller; import org.springframework.web.servlet.ModelAndView; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class HellowController implements org.springframework.web.servlet.mvc.Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mv = new ModelAndView(); mv.addObject("msg","HelloSpringMvc!"); mv.setViewName("hello"); return mv; } }
jsp内容:/hello
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>hello</title> </head> <body> ${msg} </body> </html>
可能需要的依赖:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.24</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <!--EL表达式,前端--> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
标签:DispatcherServet,web,spring,MVC,import,servlet,javax,初始 From: https://www.cnblogs.com/5ran2yl/p/17162237.html