Spring MVC入门案例!!!

这篇具有很好参考价值的文章主要介绍了Spring MVC入门案例!!!。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

<packaging>war</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 配置Tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!--端口号-->
                    <port>8080</port>
                    <!--项目名-->
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

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

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

 3.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

4.springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--配置创建spring要扫描的包-->
    <context:component-scan base-package="com.by.controller"></context:component-scan>

    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!--开启springmvc注解配置-->
    <mvc:annotation-driven></mvc:annotation-driven>

</beans>

5.创建页面

Spring MVC入门案例!!!,spring,mvc,java,mybatis,intellij-idea,tomcat

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" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <a href="/hello">hello</a>
  </body>
</html>

success.jsp文章来源地址https://www.toymoban.com/news/detail-792303.html

<%--
  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" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
        ${msg}
</body>
</html>

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;

/**
 * <p>Project: SpringMVC - HelloController</p>
 * <p>Powered by scl On 2024-01-08 21:41:26</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @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入门案例!!!的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SSM框架的学习与应用(Spring + Spring MVC + MyBatis)-Java EE企业级应用开发学习记录(第二天)Mybatis的深入学习

    上一篇我们的项目搭建好了,也写了简答的Junit测试类进行测试,可以正确映射到数据库中。 那么这篇文章来深入学习一下以下几个点: 了解 MyBatis的核心对象SqlSessionFactoryBuilder 以及它的作用 掌握MyBatis核心配置文件以及元素的使用 。 掌握MyBatis映射文件及其元素的使用 。

    2024年02月11日
    浏览(53)
  • (第六天)初识Spring框架-SSM框架的学习与应用(Spring + Spring MVC + MyBatis)-Java EE企业级应用开发学习记录

    ​ 昨天我们已经把Mybatis框架的基本知识全部学完,内容有Mybatis是一个半自动化的持久层ORM框架,深入学习编写动态SQL,Mybatis的关联映射,一对一、一对多、多对多、Mybatis的缓存机制,一二级缓存的开启和设置,缓存命中率、如何使用idea链接数据库自动生成pojo类等。我们学

    2024年02月10日
    浏览(52)
  • Spring和Spring MVC和MyBatis面试题

    面试题1:请简述Spring、Spring MVC和MyBatis在整合开发中的作用? 答案: Spring :是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。它提供了强大的依赖注入功能,简化了企业级应用的开发。在整合开发中,Spring负责处理业务逻辑、事务管理、安全控制等。 Spring M

    2024年04月16日
    浏览(35)
  • SSM框架的学习与应用(Spring + Spring MVC + MyBatis)-Java EE企业级应用开发学习记录(第三天)动态SQL

    昨天我们深入学习了 Mybatis的核心对象SqlSessionFactoryBuilder , 掌握MyBatis核心配置文件以及元素的使用 ,也掌握MyBatis映射文件及其元素的使用。那么今天我们需要掌握的是更加复杂的查询操作。 学会编写MyBatis中动态SQL 学会MyBatis的条件查询操作 学会MyBatis的更新操作 学会MyBati

    2024年02月11日
    浏览(41)
  • (第十一天)初识SpringMVC SSM框架的学习与应用(Spring + Spring MVC + MyBatis)-Java EE企业级应用开发学习记录

    今天我们要来学习一下SSM框架的最后一个框架SpringMVC 一、初认SpringMVC 基本概念: ​ Spring MVC(Model-View-Controller)是一个用于构建Java Web应用程序的开源框架,它提供了一种基于MVC架构的方式来开发Web应用 。 ​ SpringMVC是Spring Framework的一部分,它是一种基于模型-视图-控制器(

    2024年02月07日
    浏览(55)
  • 案例14 Spring MVC文件上传案例

    基于Spring MVC实现文件上传: 使用commons-fileupload实现上传文件到本地目录。 实现上传文件到阿里云OSS和从阿里云OSS下载文件到本地。 选择Maven快速构建web项目,项目名称为case14-springmvc03。 ​ src.main.resources目录下创建spring-mvc.xml。 在src.main.java.com.wfit.upload目录下创建UploadContr

    2024年02月13日
    浏览(30)
  • 案例13 Spring MVC参数传递案例

    基于Spring MVC实现HttpServletRequest、基本数据类型、Java Bean、数组、List、Map、JSON方式的参数传递。 选择Maven快速构建web项目,项目名称为case13-springmvc02。 ​ src.main路径下,执行new – Directory操作,选择java、resources后,执行回车键。 ​ 在web.xml中配置DispatcherServlet。 src.main.java.

    2024年02月13日
    浏览(36)
  • SSM框架整合:掌握Spring+Spring MVC+MyBatis的完美结合!

    (1) 创建工程 创建一个Maven的web工程 pom.xml添加SSM需要的依赖jar包 编写Web项目的入口配置类,实现 AbstractAnnotationConfigDispatcherServletInitializer 重写以下方法。 getRootConfigClasses() :返回Spring的配置类-需要 SpringConfig 配置类。 getServletConfigClasses() :返回SpringMVC的配置类-需要 SpringMvc

    2024年01月17日
    浏览(45)
  • Spring、Spring-MVC、Mybatis、Mybatis-generator整合核心配置文件记录

    Spring、Spring-MVC、Mybatis、Mybatis-generator整合核心配置xml文件记录 spring-mybatis.xml

    2024年01月22日
    浏览(45)
  • Spring + Spring MVC + MyBatis+Bootstrap+Mysql酒店管理系统源码

    本系统实现了酒店管理系统源码,管理端实现了管理员登录、会员信息管理、客房信息类型管理、客房信息管理、客房信息添加 、预定信息管理、入住信息管理、入住信息添加 JDK版本:1.8 Mysql:5.7 账号:admin 密码:123456 点击以下链接获取源码。 IDEA+Spring + Spring MVC + MyBatis+Boots

    2024年02月12日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包