SpringMVC源码(一):源码运行环境搭建
一、创建smvc子模块
子模块选择gradle
二、实体类代码
2.1、User实体
1 public class User { 2 3 private String name; 4 5 public User() { 6 } 7 8 public User(String name) { 9 this.name = name; 10 } 11 12 public String getName() { 13 return name; 14 } 15 16 public void setName(String name) { 17 this.name = name; 18 } 19 20 }
2.2、Controller控制器
1 import com.snails.bean.User; 2 import org.springframework.stereotype.Controller; 3 import org.springframework.ui.Model; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 @Controller 9 public class HelloController { 10 11 @RequestMapping("/userlist") 12 public String hello(Model model){ 13 System.out.println("snials"); 14 List<User> userList = new ArrayList<>(); 15 User user1 = new User("张三"); 16 User user2 = new User("李四"); 17 userList.add(user1); 18 userList.add(user2); 19 model.addAttribute("users",userList); 20 return "userlist"; 21 } 22 }
三、配置文件
3.1、添加Spring配置文件spring-config.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 7 <property name="prefix" value="/WEB-INF/jsp/" /> 8 <property name="suffix" value=".jsp" /> 9 </bean> 10 <bean id = "user" class="com.snails.bean.User"></bean> 11 </beans>
3.2、添加SpringMVC配置文件applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 https://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 https://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 13 <context:component-scan base-package="com.snails"></context:component-scan> 14 <mvc:default-servlet-handler/> 15 <mvc:annotation-driven></mvc:annotation-driven> 16 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 17 <property name="suffix" value=".jsp"></property> 18 <property name="prefix" value="/WEB-INF/jsp/"></property> 19 </bean> 20 </beans>
3.3、添加web.xml文件
新增webapp文件夹,在webapp文件夹下新建WEB-INF,在WEB-INF文件夹添加web.xml文件,wweb.xml文件详情如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 5 version="4.0"> 6 <!--Spring配置文件--> 7 <context-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>classpath:spring-config.xml</param-value> 10 </context-param> 11 12 <servlet> 13 <servlet-name>mvc-test</servlet-name> 14 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 15 <!--SpringMVC配置文件--> 16 <init-param> 17 <param-name>contextConfigLocation</param-name> 18 <param-value>classpath:applicationContext.xml</param-value> 19 </init-param> 20 <load-on-startup>1</load-on-startup> 21 <async-supported>true</async-supported> 22 </servlet> 23 24 <servlet-mapping> 25 <servlet-name>mvc-test</servlet-name> 26 <url-pattern>/</url-pattern> 27 </servlet-mapping> 28 <listener> 29 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 30 </listener> 31 </web-app>
四、项目依赖
build.gradle详情如下
1 plugins { 2 id 'java' 3 id 'war' 4 } 5 6 group 'org.springframework' 7 version '5.2.9.BUILD-SNAPSHOT' 8 9 sourceCompatibility = 1.8 10 11 repositories { 12 mavenCentral() 13 } 14 15 dependencies { 16 compile(project(":spring-context")) 17 compile(project(":spring-context-support")) // freemarker需要,不加可能报错 18 compile(project(":spring-webmvc")) 19 compile(project(":spring-beans")) 20 compile(project(":spring-core")) 21 compile(project(":spring-jdbc")) 22 compile(project(":spring-orm")) 23 compile(project(":spring-tx")) 24 compile(project(":spring-web")) 25 compile(project(":spring-context-indexer")) 26 compile(project(":spring-expression")) 27 compile(project(":spring-instrument")) 28 compile(project(":spring-jcl")) 29 compile(project(":spring-jms")) 30 compile(project(":spring-messaging")) 31 compile(project(":spring-oxm")) 32 compile(project(":spring-test")) 33 compile(project(":spring-webflux")) 34 compile(project(":spring-websocket")) 35 testImplementation 'javax.servlet:javax.servlet-api:3.1.0' 36 // compile 'javax.servlet.jsp.jstl:jstl:1.2' 37 compile 'org.apache.taglibs:taglibs-standard-spec:1.2.5' 38 compile 'org.apache.taglibs:taglibs-standard-impl:1.2.5' 39 testCompile group: 'junit', name: 'junit', version: '4.12' 40 compile 'commons-fileupload:commons-fileupload:1.4' 41 compile 'com.fasterxml.jackson.core:jackson-databind:2.12.3' 42 compile 'com.fasterxml.jackson.core:jackson-annotations:2.12.3' 43 compile 'com.fasterxml.jackson.core:jackson-core:2.12.3' 44 }
五、Spring内部实现类依赖
spring.handlers详情如下
1 http\://www.springframework.org/schema/mvc=org.springframework.web.servlet.config.MvcNamespaceHandler 2 http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler 3 http\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler 4 http\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler 5 http\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler 6 http\://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler
六、jsp页面
userlist.jsp详情如下1 <%-- 2 Created by IntelliJ IDEA. 3 User: p'c 4 Date: 2021/1/31 5 Time: 0:50 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>Title</title> 12 </head> 13 <body> 14 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 15 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 16 <h2>This is SpringMVC demo page</h2> 17 <c:forEach items="${users}" var="user"> 18 <c:out value="${user.name}"/><br/> 19 </c:forEach> 20 </body> 21 </html>
七、项目结构
八、调试问题
SpringMVC的请求只有第一次有效,之后就不起作用了
在实际的调试过程中,跳到下一个断点采用的是ALT+F9,IDEA界面如下,会导致SpringMVC后续请求到达不了断点。
解决方案:
使用快捷键F9,跳到下一个断点可解决此问题。