如何创建Springmvc项目

如何创建Springmvc项目

什么是Springmvc?

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

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

第一步: 引入依赖

<dependencies>
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>${spring.version}version>
    dependency>
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-testartifactId>
        <version>${spring.version}version>
    dependency>
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>${spring.version}version>
    dependency>
    
    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>javax.servlet-apiartifactId>
        <version>3.1.0version>
        <scope>providedscope>
    dependency>
    
    <dependency>
        <groupId>org.thymeleafgroupId>
        <artifactId>thymeleaf-spring5artifactId>
        <version>3.0.12.RELEASEversion>
    dependency>
    
    <dependency>
        <groupId>ch.qos.logbackgroupId>
        <artifactId>logback-classicartifactId>
        <version>1.2.3version>
    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 Applicationdisplay-name>
    
    <servlet>
        
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

第三步骤: 创建spring的配置文件 以 -servlet结尾
文件名为: springmvc-servlet.xml


<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>

    
    
    <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项目

如何创建Springmvc项目_第1张图片
如何创建Springmvc项目_第2张图片

如何创建Springmvc项目_第3张图片配置成功后显示
如何创建Springmvc项目_第4张图片
如何创建Springmvc项目_第5张图片

你可能感兴趣的:(spring,spring)