Spring MVC 簡(jiǎn)單的hello world的實(shí)現(xiàn)
一、項(xiàng)目搭建
1、可以在新建項(xiàng)目的使用Spring MVC框架?;蛘邉?chuàng)建一個(gè)簡(jiǎn)單的項(xiàng)目之后再用Add Framework Support來(lái)添加Spring MVC框架。

2、刪除自動(dòng)生成的lib的jar包,使用pom文件來(lái)進(jìn)行管理包。目錄結(jié)構(gòu)如下圖。

3、pom文件。加載完成之后才能進(jìn)行下一步。
<?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>com.zxj</groupId>
<artifactId>zxj-spring-mvc</artifactId>
<version>1.0-SNAPSHOT</version>
<name>zxj-spring-mvc</name>
<url>http://www.example.com</url>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>4.3.18.RELEASE</spring.version>
</properties>
<dependencies>
<!--測(cè)試-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring mvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
</plugins>
</build>
</project>
4、Project Structure的編輯,創(chuàng)建一下包名。

二、webapp的編輯
1、目錄結(jié)構(gòu)。

2、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"> <!--welcome pages--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3、applicationContent.xml。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.xiaojie.spring.mvc"/> </beans>
4、dispatcher-servlet.xml。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.xiaojie.spring.mvc"/> <context:annotation-config/> <!-- 配置注解驅(qū)動(dòng) 可以將request參數(shù)與綁定到controller參數(shù)上 --> <mvc:annotation-driven/> <!--這句要加上,要不然可能會(huì)訪問(wèn)不到靜態(tài)資源--> <mvc:default-servlet-handler /> <!--靜態(tài)資源映射如下--> <mvc:resources mapping="/css/**" location="/statics/css/"/> <mvc:resources mapping="/js/**" location="/statics/js/"/> <mvc:resources mapping="/image/**" location="/statics/images/"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
5、index.jsp默認(rèn)頁(yè)面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>hello world</title> </head> <body> welcome zhuoxiaojie spring mvc </body> </html>
6、hello.jsp。
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>test</title> </head> <body> hello world </body> </html>
7、test2.jsp。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>test</title>
</head>
<body>
key1: ${key1} , key2: ${key2}
</body>
</html>
三、Controller層
package com.xiaojie.spring.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("/hello")
public ModelAndView test1(Model model) {
return new ModelAndView("hello");
}
@GetMapping("/test2")
public ModelAndView test2(Model model) {
model.addAttribute("key1", "卓小杰");
model.addAttribute("key2", "你真帥");
return new ModelAndView("test2");
}
}
四、Tomcat的配置
1、下載Tomcat8。自己去百度教程下載。

2、用Tomcat進(jìn)行啟動(dòng)項(xiàng)目的配置。然后啟動(dòng)項(xiàng)目。
- war模式:將web工程以war包的形式上傳到服務(wù)器
- war exploed模式:將web工程以當(dāng)前文件夾的位置關(guān)系上傳到服務(wù)器


五、測(cè)試結(jié)果
1、啟動(dòng)之后的默認(rèn)界面index.jsp。

2、hello.jsp界面。

3、test2.jsp界面。帶參數(shù)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合spring-retry實(shí)現(xiàn)接口請(qǐng)求重試機(jī)制及注意事項(xiàng)
今天通過(guò)本文給大家介紹我們應(yīng)該如何使用SpringBoot來(lái)整合spring-retry組件實(shí)現(xiàn)重試機(jī)制及注意事項(xiàng),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-08-08
mybatis 為什么千萬(wàn)不要使用 where 1=1
這篇文章主要介紹了mybatis 為什么千萬(wàn)不要使用 where 1=1,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Redis實(shí)現(xiàn)商品秒殺功能頁(yè)面流程
這篇文章主要介紹了Redis實(shí)現(xiàn)商品秒殺功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09
從源碼角度簡(jiǎn)單看StringBuilder和StringBuffer的異同(全面解析)
下面小編就為大家分享一篇從源碼角度簡(jiǎn)單看StringBuilder和StringBuffer的異同(全面解析),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
SpringBoot監(jiān)控Tomcat活動(dòng)線程數(shù)來(lái)判斷是否完成請(qǐng)求處理方式
這篇文章主要介紹了SpringBoot監(jiān)控Tomcat活動(dòng)線程數(shù)來(lái)判斷是否完成請(qǐng)求處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
SpringBoot3整合SpringDoc實(shí)現(xiàn)在線接口文檔的詳細(xì)過(guò)程
這篇文章主要介紹了SpringBoot3整合SpringDoc實(shí)現(xiàn)在線接口文檔的詳細(xì)過(guò)程,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06

