1.如何进入系统:
首先,spring mvc需要知道的是spring mvc是如何进入系统来帮助我们工作的,我通常回去查阅官方的教程文档,文档中这样说道:
Spring MVC, like many other web frameworks, is designed around the front controller pattern where a central Servlet
, theDispatcherServlet
, provides a shared algorithm for request processing while actual work is performed by configurable, delegate components. This model is flexible and supports diverse workflows.
The DispatcherServlet
, as any Servlet
, needs to be declared and mapped according to the Servlet specification using Java configuration or in web.xml
. In turn the DispatcherServlet
uses Spring configuration to discover the delegate components it needs for request mapping, view resolution, exception handling
spring mvc 通过dispatcher servlet来拦截客户端发起的请求,所以我们首先要配置它的一些信息:
2.在web.xml中配置相应的信息
这是初始状态的web.xml:
接下来我们这样配置一下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>studyprocess</display-name>
<!-- 声明dispatcher servlet -->
<servlet>
<!--dispatcher servlet的别名 -->
<servlet-name>dispatcher</servlet-name>
<!-- 对应的类 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数,告诉系统响应的配置文件在哪里 -->
<init-param>
<param-name>ContextConfigLocation</param-name>
<param-value>/WEB-INF/spring/dispatcher-servlet.xml</param-value>
</init-param>
<!-- 告诉系统在服务器开启时就加载这个类 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!-- 名字 -->
<servlet-name>dispatcher</servlet-name>
<!-- 只要是应用程序名+/的请求它都会拦截 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
这里给大家再说明一个非常容易出错的地方,在这一段配置文件的代码中:
<servlet-mapping>
<!-- 名字 -->
<servlet-name>dispatcher</servlet-name>
<!-- 只要是应用程序名+/的请求它都会拦截 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<url-pattern>/</url-pattern>这一行中也隐含了很多的学问,一不小心就会入坑,接下来进行一定的说明:
其中/和/*的区别:
< url-pattern > / </ url-pattern > 不会匹配到*.jsp,即:*.jsp不会进入spring的 DispatcherServlet类 。
< url-pattern > /* </ url-pattern > 会匹配*.jsp,会出现返回jsp视图时再次进入spring的DispatcherServlet 类,导致找不到对应的controller所以报404错。
总之,关于web.xml的url映射的小知识:
< url-pattern>/</url-pattern> 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url
< url-pattern>/*</url-pattern> 会匹配所有url:路径型的和后缀型的url(包括/login,*.jsp,*.js和*.html等)
3.在dispatcher-servlet.xml中配置相应的信息
注意这个配置文件是自己新建的,名字要和web.xml中写的路径的名字一致,不然无法找到,我那里写了*是通配符,那样写的话名字就可以不一致了,但是我个人不建议这样做。
首先现在配置文件的namespace中增加以下的模块
然后添加信息:
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">
<!-- 告诉系统发起的请求映射到这个包下的某一个类中的某一个方法去处理,具体是哪一个,系统会自动的检索 -->
<context:component-scan base-package="com.brsc.studyprocess.controller"></context:component-scan>
</beans>
地址填的是控制器所在的包名
4.创建一个controller来进行实验:
package com.brsc.studyprocess.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//该注解告诉spring mvc这是一个控制器
@Controller
public class HomeController {
//该注解告诉spring mvc 请求是应用程序名+/+home的请求由这个方法来处理
@RequestMapping("/home")
//说明这个方法返回的信息就是网页所需要显示的信息
@ResponseBody
public String gohome() {
return "home fdsgfds fsgfdsg";
}
}
启动服务器
实验成功!那如果我想跳转到某一个jsp页面怎么做呢?
5.跳转到某一个页面的做法:
首先我们如果要返回一个页面,那么就要映射到相应的jsp文件。
我们首先在dispatcher-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">
<!-- 告诉系统发起的请求映射到这个包下的某一个类中的某一个方法去处理,具体是哪一个,系统会自动的检索 -->
<context:component-scan base-package="com.brsc.studyprocess.controller"></context:component-scan>
<!-- spring mvc提供了一个内部资源解析器,id和class都是用来映射到这个工具的标识 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀填你的JSP文件的路径 -->
<property name="prefix" value="/WEB-INF/views/"></property>
<!-- 后缀填前缀路径下你要访问的资源的文件后缀名 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
然后在前缀的文件目录下,新建一个jsp文件,我的叫home.jsp,然后在控制器中修改一下的代码:
package com.brsc.studyprocess.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//该注解告诉spring mvc这是一个控制器
@Controller
public class HomeController {
//该注解告诉spring mvc 请求是应用程序名+/+home的请求由这个方法来处理
@RequestMapping("/home")
public String gohome() {
System.out.println("dsfdsfdsfsdfsfsdfsd");
return "home";//jsp文件的名字
}
}
home.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hello spring mvc!
</body>
</html>
重启服务器: