springmvc-hello

目录

一、 引入依赖

二、配置前端控制器( dispatcherServlet )

三、创建主配置文件 ( applicationContext.xml )

四、编写控制器类 ( HelloController)

五、创建响应页面 (hello.jsp)

六、JSP页面热更新

七、可能遇到的问题


本章实现的功能:

搭建 SpringMvc 开发环境,发送 http://localhost:8080/springmvc01/hello.do?name=zs 请求,响应 JSP 页面。效果如下图:

springmvc-hello_第1张图片

一、 引入依赖

在 pom.xml 文件中

    
    
      org.springframework
      spring-webmvc
      5.2.12.RELEASE
    

二、配置前端控制器( dispatcherServlet )

在 web.xml 文件中

    
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath*:applicationContext.xml
        
        1
    

    
        dispatcherServlet
        /
    

这个配置做了2件事:

  • 在容器运行时加载 DispatcherServlet 这个类                   -->通过 1 实现
  • 告诉Spring容器,我们自定义的主配置文件在哪              -->通过classpath*:applicationContext.xml

三、创建主配置文件 ( applicationContext.xml )

在 resources 文件夹下,创建 applicationContext.xml




    
    

    
    
        
        
    

四、编写控制器类 ( HelloController)

package com.lcy.controller;

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

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(String name, ModelMap modelMap){
        modelMap.put("name", name);
        return "hello";
    }

}

五、创建响应页面 (hello.jsp)

springmvc-hello_第2张图片

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


    hello



hello -> ${name}

六、JSP页面热更新

springmvc-hello_第3张图片

七、可能遇到的问题

springmvc-hello_第4张图片

解决方法:

    
      org.springframework
      spring-tx
      5.2.12.RELEASE
    

 

 

 

你可能感兴趣的:(springmvc环境搭建,SpringMVC之Hello)