SpringMVC入门案例

SpringMVC入门案例,第1张

SpringMVC入门案例

案例概况

本案例将实现简单的登录功能,登录之后进入欢迎页面,效果如下:

目录结构

一,导入相关依赖
pom.xml




  4.0.0

  org.example
  spring_mvc_demo
  1.0-SNAPSHOT
  war

  spring_mvc_demo Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.13.2
      test
    
    
      org.slf4j
      slf4j-log4j12
      1.7.21
    
    
    
      javax.servlet
      javax.servlet-api
      3.1.0
    
    
      javax.servlet.jsp
      jsp-api
      2.2
    
    
      javax.servlet
      jstl
      1.2
    
    
    
      org.springframework
      spring-web
      5.2.3.RELEASE
    
    
      org.springframework
      spring-webmvc
      5.2.3.RELEASE
    
    
      org.springframework
      spring-context
      5.2.3.RELEASE
    
    
      org.springframework
      spring-test
      5.2.3.RELEASE
    
    
      org.springframework
      spring-jdbc
      5.2.3.RELEASE
    
    
      com.github.stefanbirkner
      system-rules
      1.16.1
      test
    
    
      org.aspectj
      aspectjweaver
      1.8.9
    
    
    
      org.apache.commons
      commons-lang3
      3.4
    
    
      commons-fileupload
      commons-fileupload
      1.3.1
    
  

  
    spring_mvc_demo
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  



二,创建和修改配置文件
springmvc-servlet.xml



    
    
    
    
        
        
    

web.xml



    
        login
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springmvc-servlet.xml
        
        
        1
    
    
        login
        /
    


三,编写类和jsp页面
index.jsp

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


    Index


请点击登录


login.jsp

<%--
  Created by IntelliJ IDEA.
  User: 28458
  Date: 2021/11/24
  Time: 14:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Login


请登录




success.jsp

<%--
  Created by IntelliJ IDEA.
  User: 28458
  Date: 2021/11/24
  Time: 14:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Success


${success}
欢迎${username}登录系统!



LoginController.java

package com.example.controller;

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

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {
    @RequestMapping(value = "/index")
    public String toLogin() {
        return "login";
    }

    @RequestMapping(value = "/login")
    public String login(Model model, HttpServletRequest request) {
        model.addAttribute("username", request.getParameter("username"));
        model.addAttribute("success", "登录成功");
        return "success";
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5582249.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存