Spring MVC入门案例!!!

 1.先加入架包(我这里使用了tomcat插件,你也可以直接按照原本的方式使用tomcat)

war

    
        8
        8
        UTF-8
    
    
        
            org.springframework
            spring-context
            5.1.8.RELEASE
        
        
            org.springframework
            spring-web
            5.1.8.RELEASE
        
        
            org.springframework
            spring-webmvc
            5.1.8.RELEASE
        

        
            javax.servlet
            servlet-api
            2.5
            provided
        

        
            javax.servlet.jsp
            jsp-api
            2.0
            provided
        
    
    
        
            
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
                
                    
                    8080
                    
                    /
                
            
        
    

 2.将创建的maven项目添加web工程(不会的看这里。。。)

idea如何建立web项目???-CSDN博客

 3.web.xml




    
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springmvc.xml
        
        2
    
    
        dispatcherServlet
        /
    

4.springmvc.xml




    
    

    
    
        
        
    
    
    

5.创建页面

Spring MVC入门案例!!!_第1张图片

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/1/8
  Time: 21:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

  
    $Title$
  
  
  hello
  

success.jsp

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/1/8
  Time: 21:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


        ${msg}


6.HelloController:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 

Project: SpringMVC - HelloController

*

Powered by scl On 2024-01-08 21:41:26

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ @Controller public class HelloController { @RequestMapping("/hello") public ModelAndView show(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("msg","欢迎你"); modelAndView.setViewName("success"); return modelAndView; } }

你可能感兴趣的:(spring,mvc,java,mybatis,intellij-idea,tomcat)