前言
雖然現在基本上是 springboot 的天下了,但是傳統的 spring4 在廣大的軟件企業中仍然占據很大比例。一上手能用,但是要讓我從無到有搭一個spring4的開發環境出來,可能會磕磕碰碰,耽誤不少時間,所以這里從無到有搭一個 spring4的開發環境出來,一步步完善,也復習下 spring 的基本用法,溫故知新。
創建maven項目,引入spring4的一些核心包和常用擴展包。
我用的是spring4最新版本 4.3.24.RELEASE
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.hz</groupId>
<artifactId>admin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>admin Maven Webapp</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>4.3.24.RELEASE</spring.version>
<junit.version>4.11</junit.version>
<jackson.version>2.9.8</jackson.version>
</properties>
<dependencies>
<!-- junit 測試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!--spring 常用模塊依賴 start-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring 常用模塊依賴 end -->
<!-- jackson json轉換配置,配置-->
<!-- 這個配置對應 RequestParams, RequestBody, ResponseBody 等注解,原始請求參數其實都在 HttpServletRequest 對象里,
但實際上我們用spring的時候可以直接在controller方法參數里得到請求數據,String,Integer, Map, List 和自定義bean
這個轉換就要用到這個包, 不引入這個包,使用RequestBody注解就會報錯-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- spring MVC 依賴 start-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring MVC 依賴 end-->
</dependencies>
<build>
<finalName>admin</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>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</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>
<!-- jetty 插件-->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.18.v20190429</version>
<configuration>
<httpConnector>
<port>8082</port> <!-- 監聽的http 端口,默認8080 -->
</httpConnector>
<scanIntervalSeconds>2</scanIntervalSeconds>
<webApp>
<contextPath>/</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
配置web.xml
web.xml文件是web項目的啟動文件,要想在web項目中使用spring,需要在這里配置spring
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- spring配置文件位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring.xml</param-value> <!-- 這個文件主要配置spring beans,包括數據源,事務控制
</context-param>
<!-- 啟動web時加載spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 添加springMVC支持,單獨springMVC時也需要在web.xml文件中配置 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/spring-mvc.xml</param-value> <!-- spring-mvc.xml 主要配置mvc方面,包括controller注解掃描,視圖解析器, controller參數綁定(這里就要用到pom里的jackson json依賴) -->
</init-param>
<!-- 啟動web時就加載springmvc的servlet,即啟動時就加載springmvc的配置文件 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- 攔截所有請求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list><!--指定歡迎頁面-->
<welcome-file>login.html</welcome-file>
</welcome-file-list>
<error-page> <!--當系統出現404錯誤,跳轉到頁面nopage.html-->
<error-code>404</error-code>
<location>/nopage.html</location>
</error-page>
<error-page> <!--當系統出現java.lang.NullPointerException,跳轉到頁面error.html-->
<exception-type>java.lang.NullPointerException</exception-type>
<location>/error.html</location>
</error-page>
<session-config><!--會話超時配置,單位分鍾-->
<session-timeout>360</session-timeout>
</session-config>
</web-app>
配置mvc
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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
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-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- spring掃描的包 -->
<context:component-scan base-package="com.hz.*.controller"/>
<!-- DispatcherServlet不處理靜態資源,交給服務器默認的servlet處理 -->
<mvc:default-servlet-handler />
<!-- 啟用annotation -->
<mvc:annotation-driven />
<!--json轉換器配置, @ResponseBody,RequestBody 將請求參數轉換成bean需要這個類 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 視圖渲染器 ,如果搭建REST Web應用,可以不配置這個,具體REST-Web應用配置,后面再講-->
<!-- 視圖xxx 將對應訪問/WEB-INF/page/xxx.jsp文件 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前綴 -->
<property name="prefix" value="/WEB-INF/page/" />
<!-- 后綴 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
項目結構
測試Controller
package com.hz.common.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller()
@RequestMapping("test")
public class TestController {
@RequestMapping("/info")
@ResponseBody
public Object test(Integer val){
// contentType=application/x-www-form-urlencoded
System.out.println(val == null ? "null" : val.toString());
HashMap<String, Object> map = new HashMap<>();
map.put("prop", "value");
map.put("prop1", "value1");
return map;
}
}
postman測試