作为一个懒癌患者,做程序也几年了,没啥长进。在尚硅谷看到spring mvc的视频讲解非常好,希望自己可以按照视频写一套文字的教程。也希望能坚持点什么,同时能帮助别人更好的学习spring mvc这项技术。
工具
- IntelliJ IDEA
- apache-tomcat-9.0.12-windows-x64
入门之hello world
项目创建
- 创建工程
- 输入公司名或组织名
- 选择使用的maven
- 点击next输入项目保存的地址后,maven项目创建成功。
- 创建java包及资源文件包
将java包
好的,现在一个完整的maven web项目已经完成,下面可以开始spring mvc的开发了。
hello world 开发
- 添加Spring MVC依赖添加Spring MVC依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
- 配置web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<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="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置DispatcherServlet的一个初始化参数,配置SpringMVC配置文件的位置和名称-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<!--servlet加载的时候创建-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!--配置自动扫描的包-->
<context:component-scan base-package="com.erricsson.panda.controller.*"></context:component-scan>
<!-- 开启注解 -->
<mvc:annotation-driven />
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- controller代码
package com.xcl.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author chuanlei.xu
* @version 1.0
* @date 2019/2/20 11:02
*/
@Controller
public class HelloWorld {
/*
* 使用@RequestMapping来映射请求的url
*
*/
@RequestMapping("/helloworld")
public String helloworld(){
System.out.print("hello world");
return "success";
}
}
RequestMapping
除了可以修饰方法外,还可以修饰类。比如在HelloWorld类上添加修饰@RequestMapping(“/request”),则url变为/request/helloworld
- index.jsp
<html>
<body>
<a href="helloworld">hello world</a>
</body>
</html>
- success.jsp
<html>
<body>
success
</body>
</html>
-
运行环境配置
-
运行
至此hello world 项目完成。 -
github 地址
https://github.com/chuanleixu/SpringMVC_Tutorials