1. 简单介绍
本篇博客主要使用虚拟机作为开发主机,maven在虚拟机创建项目,vscode远程开发。
相关信息:
- 操作系统:CentOS Linux release 8.4.2105
- jdk版本:java version "17"
- maven版本:3.8.2
- srpingmvc版本:
2. maven命令行创建项目
在虚拟机创建目录:mkdir -p /root/liwl_dev/java_dev
进入到目录java_dev,执行以下命令:
mvn archetype:generate -DgroupId=com.liwl.dev -DartifactId=LiwlSpringMVCDev -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
这样就在java_dev目录下,创建了LiwlSpringMVCDev
项目目录,创建结束后,目录结构如下
3. vscode远程打开项目
使用vscode远程插件打开虚拟机项目,其结构图如下
4. 开始开发
4.1 引入springmvc开发jar包
主要是两个包:spring-web和spring-webmvc
在项目管理文件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>com.liwl.dev</groupId>
<artifactId>LiwlSpringMVCDev</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>LiwlSpringVMCDev Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<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>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.14</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.14</version>
</dependency>
</dependencies>
<build>
<finalName>demo</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
4.2 创建控制器目录
在虚拟机命令行创建,如下:
或者使用vscode也可以创建
vscode项目结构图:
创建存放控制器的源码目录,并创建一个控制器类HelloController.java
,内容如下:
package liwl.springmvc.dev.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
//表示请url请求到hello,生成视图对象mv,mv对象要处理的jsp文件为hello.jsp
@RequestMapping(value="/hello")
public ModelAndView helloWorld( @RequestParam(value = "name", required = false, defaultValue = "spring mvc") String name)
{
ModelAndView mv = new ModelAndView("hello");
mv.addObject("name", name);
return mv;
}
}
4.3 创建视图文件
视图文件是最后要返回到浏览器的页面
在webapp/WEB-INF下创建views目录,然后在views目录下,创建hello.jsp文件,内容如下
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>message:${name}</h1>
</body>
</html>
4.4 创建前端控制器
web.xml只是Tomcat启动应用时的配置文件,在web.xml能够配置我们的前端控制器
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Archetype Created Web Application</display-name>
<!--第一步:spring接管所有请求 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value> //这里指定了web.xml要使用的前端控制器配置文件spring-mvc.xml
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern> //这里所有的请求都要发送到前端控制器
</servlet-mapping>
</web-app>
然后在resources
目录下,创建前端控制器配置文件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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> -->
<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"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置扫描的包 -->
<context:component-scan base-package="liwl.springmvc.dev.controller"></context:component-scan>
<!-- 视图解析器,将Controller类返回的视图名加上配置的前后缀进行展示 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
5. 编译运行
在目录/root/liwl_dev/java_dev/LiwlSpringMVCDev
下,执行mvn clean package
编译
拷贝结果到Tomcat的webapp目录:
cp /root/liwl_dev/java_dev/LiwlSpringMVCDev/target/LiwlSpringMVCDev.war /opt/apache-tomcat-8.5.70/webapps/
浏览器访问:
加个请求参数