一、先修知識
- Java servlet 知識
- Jsp 知識
- Tomcat 伺服器配置
二、建立一個動態web專案
- 開啟IDEA,選擇新建一個 Maven 專案
- 根據提示,一路next下去
三、配置SpringMVC專案
- 配置Spring配置檔案
新增好依賴包
<?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>LearnSpringMVC</groupId>
<artifactId>LearnSpringMVCDemoFirst</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>LearnSpringMVCDemoFirst 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>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--配置SpringMVC包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>LearnSpringMVCDemoFirst</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>
複製程式碼
- 配置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>
<servlet>
<!--這個servlet-name很重要-->
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
複製程式碼
由於SpringMVC所有的請求都是由一個Servlet處理的,所以在web.xml中應該將所有的請求都分派給一個Servlet,即DispatcherServlet
所以web.xml檔案中servlet相關的標籤是這樣子配的。
這裡還有一個小坑,我用Maven建立的web專案中是預設JSP1.2的,這將導致el表示式無法使用,所以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">
複製程式碼
以此宣告使用的是JSP2.0版本。 如果這個地方不改的話,還可以在JSP檔案中加上這行程式碼
<%@ page isELIgnored="false" %>
複製程式碼
3.配置Spring配置檔案 spring配置檔案的名稱必須是web.xml檔案中的servlet-name加上-servlet字尾,比如 springmvc-servlet.xml。這樣spring框架才能找到它。
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置好自動掃描包和註解-->
<context:component-scan base-package="com.learn.springMVCDemo"/>
<context:annotation-config/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
複製程式碼
配置中有個bean為InternalResourceViewResolver
這個bean用來配置檢視的路徑和訪問路徑
- 建立控制器類
package com.learn.springMVCDemo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/demo")
public String hello(Model model){
model.addAttribute("greeting", "Hello Spring MVC");
return "hello";
}
}
複製程式碼
這個控制器類很簡單
model.addAttribute("greeting", "Hello Spring MVC");給檢視傳遞一個名為greeting的值,並賦值為Hello Spring MVC。
return "hello"; 表示顯示的是名為hello.jsp的頁面。
- 建立JSP檔案
這個沒啥好解釋的
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--<%@ page isELIgnored="false" %>--%>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</html>
複製程式碼
執行結果: