springmvc-hello world


创建 maven工程 

  在pom.xml引入依赖:




org.springframework
spring-webmvc
5.3.1



ch.qos.logback
logback-classic
1.2.3



javax.servlet
javax.servlet-api
3.1.0
provided



org.thymeleaf
thymeleaf-spring5
3.0.12.RELEASE

然后创建webapp目录  在main目录下 ,添加web-info 和web.xml文件

在web.xml文件中配置springMVC



springMVC
org.springframework.web.servlet.DispatcherServlet



contextConfigLocation

classpath:springMVC.xml


1


springMVC

/

创建请求控制器

@Controller
public class HelloController {

}

配置sprinMVC.xml

<?xml version="1.0" encoding="UTF-8"?>
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 https://www.springframework.org/schema/context/spring-context.xsd">


























测试:

// @RequestMapping注解:处理请求和控制器方法之间的映射关系
// @RequestMapping注解的value属性可以通过请求地址匹配请求,/表示的当前工程的上下文路径
// localhost:8080/springMVC/
@RequestMapping("/")
public String index() {
//设置视图名称
return "index";
}

html文件如下:web-info/temapltes/index.html





首页


首页


HelloWorld


截图如下: