如何创建Springmvc项目

如何创建Springmvc项目

什么是Springmvc?

springmvc是spring的子集,是Spring为了解决程序员开发中与前端页面进行交互所遇到的问题的一种解决的方案。

如何创建一个最基本的Springmvc项目?

第一步: 引入依赖

<dependencies>
    <!--spring 的 核心包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!--Spring测试包-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!--springmvc的核心依赖 web-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!--servlet api-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <!--thymeleaf api-->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
        <version>3.0.12.RELEASE</version>
    </dependency>
    <!-- 日志依赖-->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>
</dependencies>

第二步骤: 配置 web.xml 文件

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <!--springmvc的入口类-->
    <servlet>
        <!--映射文件名-->
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--映射路径-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

第三步骤: 创建spring的配置文件 以 -servlet结尾
文件名为: springmvc-servlet.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--开启包扫描-->
    <context:component-scan base-package="com.atguigu.controller"></context:component-scan>

    <!--开启thymeleaf配置-->
    <!-- 配置Thymeleaf视图解析器 -->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <!--配置视图解析器的优先级-->
        <property name="order" value="1"/>
        <!--配置视图解析器的字符编码方式-->
        <property name="characterEncoding" value="UTF-8"/>
        <!--配置视图解析器的模板-->
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">

                        <!-- 视图前缀 -->
                        <property name="prefix" value="/WEB-INF/templates/"/>

                        <!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
</beans>

第四步: 编辑一个类 用于进行测试

package com.atguigu.controller;

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

import javax.servlet.annotation.WebServlet;

@Controller

public class HelloSpringMVC {
    @RequestMapping("/")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        return modelAndView;
    }
    @RequestMapping("/getHello")
    public ModelAndView getHello(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        modelAndView.addObject("id",123);
        modelAndView.addObject("username","张三");
        return modelAndView;
    }
}

最后启动项目: 查看项目是否启动成功?
配置tomcat项目

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述配置成功后显示
在这里插入图片描述
在这里插入图片描述

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用IntelliJ IDEA创建Spring MVC项目时,你需要按照以下步骤进行操作: 1. 打开IntelliJ IDEA并选择创建项目。 2. 在创建项目的向导中,选择你想要创建项目的位置,并为项目命名。 3. 在项目类型选择中,选择"Maven"作为构建工具,并勾选"Create from archetype"选项。 4. 在搜索框中输入"maven-archetype-webapp",选择该archetype作为项目模板。 5. 点击"Next"继续。 6. 在GroupId和ArtifactId中填写你的项目包名和项目名称。 7. 点击"Next"继续。 8. 在项目设置中,选择你想要使用的Java版本,并点击"Next"。 9. 在最后一步中,点击"Finish"完成项目创建创建项目后,你需要进行一些配置: 1. 在项目的根目录下创建一个名为"src/main/java"的目录,用于存放Java源代码。 2. 在"src/main/java"目录下创建你的Java包,例如"csdn.junKo"。 3. 在"src/main/resources"目录下创建一个名为"dispatcher-servlet.xml"的文件,并将引用\[1\]中的内容复制到该文件中。 4. 在"src/main/resources"目录下创建一个名为"applicationContext.xml"的文件,并将引用\[3\]中的内容复制到该文件中。 5. 在"pom.xml"文件中添加引用\[2\]中的Spring MVC依赖。 完成以上配置后,你就可以开始编写Spring MVC的控制器和视图了。同时,你可以根据需要进一步配置和定制你的Spring MVC项目。 #### 引用[.reference_title] - *1* *3* [Idea最新版创建一个SpringMVC项目的详细步骤](https://blog.csdn.net/qq_36223406/article/details/120850022)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [idea创建springmvc项目](https://blog.csdn.net/scj0725/article/details/131269193)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值