创建一个SpringMVC程序 , 处理请求, 页面跳转 以及 共享数据

Spring版本 : 5.x

工具 : STS3

第一步 : 创建一个JavaWeb项目  ,  拷贝SpringMVC依赖的jar包 项目结构图如下:

创建一个SpringMVC程序 , 处理请求, 页面跳转 以及 共享数据_第1张图片

 

第二步:把项目部署到Tomcat服务器,使用Eclipse的Tomcat插件部署,支持热部署

创建一个SpringMVC程序 , 处理请求, 页面跳转 以及 共享数据_第2张图片

第三步:在web.xml 中配置前端控制器,这是SpringMVC的入口


		springDispatcherServlet
		org.springframework.web.servlet.DispatcherServlet
		
			
			contextConfigLocation
			classpath:mvc.xml
		
		1
	

	
		springDispatcherServlet
		/
	

第四步:在项目根路径下创建一个source folder(该文件夹下的文件会被编译到classpath的根路径), 创建mvc.xml文件

	
	
	
	
	
	
	
	
		
		
	

第五步:创建一个HelloController类,并且创建一个hello方法,该方法处理请求localhost/hello,跳转到/WEB-INF/views/hello.jsp , 并共享数据

package com.ren.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

	@RequestMapping("/hello")
	public String helloPage(Model model) {
		// 设置共享数据
		model.addAttribute("msg", "你好,SpringMVC");
		// 设置要跳转的物理视图名称
		return "hello";
	}
}

第六步:在WEB-INF/views/下创建一个hello.jsp页面,使用EL表达式取出共享数据

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


${msg}

第七步,启动服务器没有报错,访问 localhost/hello ,看到页面上是否能出现 , 你好SpringMVC , 出现即说明成功

创建一个SpringMVC程序 , 处理请求, 页面跳转 以及 共享数据_第3张图片

创建一个SpringMVC程序 , 处理请求, 页面跳转 以及 共享数据_第4张图片

你可能感兴趣的:(Java,服务器)