Spring MVC 简单的hello world的实现
作者:@一头雾水@ 时间:2023-06-18 02:56:41
一、项目搭建
1、可以在新建项目的使用Spring MVC框架。或者创建一个简单的项目之后再用Add Framework Support来添加Spring MVC框架。
2、删除自动生成的lib的jar包,使用pom文件来进行管理包。目录结构如下图。
3、pom文件。加载完成之后才能进行下一步。
<?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>
<!--测试-->
<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的编辑,创建一下包名。
二、webapp的编辑
1、目录结构。
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/>
<!-- 配置注解驱动 可以将request参数与绑定到controller参数上 -->
<mvc:annotation-driven/>
<!--这句要加上,要不然可能会访问不到静态资源-->
<mvc:default-servlet-handler />
<!--静态资源映射如下-->
<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默认页面。
<%@ 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进行启动项目的配置。然后启动项目。
war模式:将web工程以war包的形式上传到服务器
war exploed模式:将web工程以当前文件夹的位置关系上传到服务器
五、测试结果
1、启动之后的默认界面index.jsp。
2、hello.jsp界面。
3、test2.jsp界面。带参数。
来源:https://blog.csdn.net/qq_36511401/article/details/103959003
标签:Spring,MVC,helloworld
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现登录邮箱的自动补全功能
2021-12-18 04:02:36
![](https://img.aspxhome.com/file/2023/7/91657_0s.jpg)
Spring Boot启动过程全面解析(三)
2023-09-13 13:16:39
java实现手写一个简单版的线程池
2022-09-29 04:59:32
![](https://img.aspxhome.com/file/2023/6/62726_0s.jpg)
SpringBoot使用Redisson实现分布式锁(秒杀系统)
2022-07-17 05:15:41
![](https://img.aspxhome.com/file/2023/7/83487_0s.jpg)
在Parallel中使用DbSet.Add()发现的一系列多线程问题和解决思路详解
2023-02-13 19:31:10
![](https://img.aspxhome.com/file/2023/2/129642_0s.png)
Android使用SoundPool播放短音效
2022-03-02 12:33:00
Android实现完整游戏循环的方法
2023-12-26 16:40:06
Java多态中动态绑定原理解析
2021-08-01 06:39:23
Apache commons fileupload文件上传实例讲解
2021-09-09 17:19:29
![](https://img.aspxhome.com/file/2023/5/97795_0s.png)
以武侠形式理解Java LinkedList源码
2021-12-27 20:34:23
解决Android Studio 3.0 butterknife:7.0.1配置的问题
2021-08-20 09:23:05
Prometheus 入门教程之SpringBoot 实现自定义指标监控
2022-08-02 12:58:54
![](https://img.aspxhome.com/file/2023/7/67487_0s.jpg)
完美解决安卓jni项目会删除其他so文件的问题
2023-11-07 16:45:47
C#借助OpenCvSharp读取摄像头并显示的实现示例
2022-12-22 05:49:51
![](https://img.aspxhome.com/file/2023/3/126043_0s.jpg)
Java设计模式之java备忘录模式详解
2023-08-22 19:31:07
![](https://img.aspxhome.com/file/2023/4/58434_0s.png)
Java基础之容器Vector详解
2023-11-25 13:10:07
C#委托delegate实例解析
2021-07-28 08:58:26
Java Swing中的工具栏(JToolBar)和分割面版(JSplitPane)组件使用案例
2021-09-07 14:45:17
![](https://img.aspxhome.com/file/2023/5/98645_0s.jpg)
C#四舍五入用法实例
2021-07-20 02:49:20
浅谈对Java双冒号::的理解
2023-09-20 02:19:21