日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

idea 編寫(xiě)springmvc項(xiàng)目并部署到Tomcat

作者:hayhead 更新時(shí)間: 2022-07-16 編程語(yǔ)言

MVC概念

model(模型):數(shù)據(jù)模型,提供要展示的數(shù)據(jù),現(xiàn)在一般包括數(shù)據(jù)Dao和行為Service;
view(視圖):負(fù)責(zé)進(jìn)行模型的展示,一般就是用于用戶(hù)交互;
Controller(控制器):調(diào)度model和view,接受請(qǐng)求,委托給模型處理,返回?cái)?shù)據(jù),由視圖展示;

各層作用:
控制器:獲得表單數(shù)據(jù)、調(diào)用業(yè)務(wù)邏輯、轉(zhuǎn)向指定頁(yè)面;
模型層:業(yè)務(wù)邏輯、保存數(shù)據(jù)狀態(tài);
視圖:顯示頁(yè)面;

SpringMVC概念

SpringMVC是Spring全家桶的一部分,是基于java實(shí)現(xiàn)mvc的輕量級(jí)web框架;
Spring的web框架?chē)@DispatcherServlet設(shè)計(jì),它可以將請(qǐng)求分發(fā)到不同的處理器,本質(zhì)還是Servlet

spring mvc工作原理在這里插入圖片描述

maven項(xiàng)目配置

新建一個(gè)Maven工程!導(dǎo)入pom依賴(lài)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>tjw</groupId>
    <artifactId>mvc</artifactId>
    <version>1.0-SNAPSHOT</version>


    <!-- 設(shè)置jdk版本   -->
    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>

    <packaging>war</packaging>


    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <!--   依賴(lài)范圍   -->
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>

        <!-- 必須添加 war 打包插件        -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!-- 指定web.xml的路徑  -->
                    <webXml>web\WEB-INF\web.xml</webXml>
                    <!-- 指定jsp、js、css路徑 -->
                    <warSourceDirectory>web</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>

    </build>


</project>

創(chuàng)建web文件夾

在這里插入圖片描述
web包結(jié)構(gòu)
在這里插入圖片描述

web.xml配置

web.xml 配置DispatcherServlet

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

        <!-- 配置springMVC需要加載的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--  啟動(dòng)時(shí)加載      -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <!-- 默認(rèn)匹配所有的請(qǐng)求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring-mvc.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:aop="http://www.springframework.org/schema/aop"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <context:component-scan base-package="com.mvc">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- mvc 注解配置 -->
    <mvc:annotation-driven/>

    <!--    全部靜態(tài)資源放行-->
    <mvc:default-servlet-handler/>

    <!-- 配置視圖解析器   -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <context:annotation-config/>
</beans>

編寫(xiě)controller

package com.mvc.controller;

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

@Controller
public class HelloController {


    @RequestMapping("/home")
    public String hello(){
        return "index";
    }
}

編寫(xiě)jsp頁(yè)面


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>hello mvc</h1>
</body>
</html>

Tomcat配置

1、添加Tomcat server

在這里插入圖片描述

2、添加 war 并指定項(xiàng)目路徑

在這里插入圖片描述

3、運(yùn)行Tomcat
在這里插入圖片描述
訪(fǎng)問(wèn):http://localhost:8080/home

原文鏈接:https://blog.csdn.net/m0_46267375/article/details/125810595

欄目分類(lèi)
最近更新