Spring MVC 是 Spring 提供的一个基于 MVC 设计模式的轻量级 Web 开发框架。
JSTL(Java server pages standarded tag library,即JSP标准标签库)是由JCP(Java community Proces)所制定的标准规范,它主要提供给Java Web开发人员一个标准通用的标签库。
Apache Tiles是一个JSP页面布局框架。Tiles框架提供了一种模板机制,可以为某一类页面定义一个通用的模板,该模板定义了页面的整体布局。
Springmvc: https://docs.spring.io/spring-framework/docs/current/reference/html/web.html
JSTL: https://tomcat.apache.org/taglibs/standard/
Tiles: https://tiles.apache.org/
1. 开发环境
系统:Windows 10 Home
开发工具:IntelliJ IDEA 2020.1.4 (Community Edition)
数据库: MariaDB 10.4.21 (本文内容没有用到数据库)
Maven 3.8.1 配置:
Maven安装目录: C:\Applications\java\apache-maven-3.8.1
本地Maven仓库: C:\Applications\java\maven-repository
Java version "1.8.0_121"
Java, Maven 和 IDEA 的安装配置过程,见 IDEA创建Maven Quickstart项目
2. 在 IDEA上创建 Maven Webapp 项目
New Project -> Project Type: Maven -> Project SDK: 1.8 -> Select maven-archtype-webapp: Next
Name: SpringmvcTiles
GroupId: com.example
ArtifactId: SpringmvcTiles
-> Next
Maven home Directory: C:\Applications\java\apache-maven-3.8.1
User settings file: C:\Applications\java\apache-maven-3.8.1\conf\settings.xml
Local repository: C:\Applications\java\maven-repository
-> Finish
生成的项目目录结构,参考 IDEA创建Maven Webapp项目
3. 使用 jetty-maven-plugin, 将 jetty 内嵌运行
IDEA创建Maven Webapp项目 使用 tomcat7-maven-plugin,本文尝试使用 jetty-maven-plugin。
1) 修改 pom.xml:
<project ... >
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.7.v20160115</version>
<configuration>
<httpConnector>
<port>9090</port>
<host>localhost</host>
</httpConnector>
<scanIntervalSeconds>1</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>
</build>
</project>
*注: path 项目访问路径 本例:localhost:9090, 如果配置的aa,则访问路径为localhost:9090/aa;uriEncoding 非必需项。
2) 运行
Run -> Edit configurations -> Click "+" -> Select "Maven"
Command line: clean jetty:run
Name: SpringmvcTiles [clean,jetty:run]
Before Launch:
Click "+", select "Launch Web Browser"
Browser: default
Url: http://localhost:9090
-> OK
Run -> Run "SpringmvcTiles [clean,jetty:run]"
3) 打包 War 运行
Run -> Edit configurations -> Click "+" -> Select "Maven"
Command line: clean jetty:run-war
Name: SpringmvcTiles [clean,jetty:run-war]
Before Launch:
Click "+", select "Launch Web Browser"
Browser: default
Url: http://localhost:9090
-> OK
Run -> Run "SpringmvcTiles [clean,jetty:run-war]"
4. 导入 spring-webmvc, servlet, jstl, tiles
访问 http://www.mvnrepository.com/,查询 ...
修改 pom.xml
<project ... >
...
<dependencies>
...
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-jsp -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>3.0.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
...
</dependencies>
...
</project>
在IDE中项目列表 -> 点击鼠标右键 -> Maven -> Reload Project
5. 支持 SpringMVC
1) 修改 src/main/webapp/WEB-INF/web.xml
<!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>
...
<!-- Spring mvc 适配器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
...
</web-app>
2) 添加 src/main/resources/springmvc-beans.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:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
default-init-method="init"
default-destroy-method="destroy">
<!-- Scan package -->
<context:component-scan base-package="com.example"/>
<mvc:annotation-driven />
<!-- MVC viewResolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
6. 支持静态资源 (html/js/css/images)
1) 修改 src/main/resources/springmvc-beans.xml
<beans ...>
...
<!-- html,css,js,images -->
<mvc:resources location="/static/" mapping="/static/**" />
<mvc:default-servlet-handler />
...
</beans>
新建目录 src/main/webapp/static
2) 添加 src/main/webapp/static/test.html
<!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=UTF-8">
<title>Static HTML</title>
</head>
<body>
<h3>Static HTML Page - Test</h3>
<p> </p>
</body>
</html>
http://localhost:9090/static/test.html
3) 添加 jQuery
从 https://jquery.com/ 下载 JQuery 包,添加到:
src/main/webapp/static/js/jquery-1.12.2.min.js
* jQuery版本根据项目需要,这里用1.12.2, CSS和图片也是放到static目录下
7. 支持 Tiles
1) 修改 src/main/resources/springmvc-beans.xml
<beans ...>
...
<!-- tiles tag -->
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>classpath:tiles-definitions.xml</value>
</list>
</property>
</bean>
...
</beans>
3) 添加 src/main/resources/tiles-definitions.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="header" template="/WEB-INF/jsp/layout/header.jsp"></definition>
<definition name="top_navbar" template="/WEB-INF/jsp/layout/top_navbar.jsp"></definition>
<definition name="top_menu" template="/WEB-INF/jsp/layout/top_menu.jsp"></definition>
<definition name="footer" template="/WEB-INF/jsp/layout/footer.jsp"></definition>
</tiles-definitions>
4) 页面布局
(1) 添加 src/main/webapp/WEB-INF/jsp/layout/header.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<!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=UTF-8">
<title>Springmvc Tiles</title>
<script language="javascript" src="${request.getContextPath()}/static/js/jquery-1.12.2.min.js"></script>
</head>
<body id="app-layout">
<p style="background-color: gray; padding: 5px; color: white;">Header</p>
(2) 添加 src/main/webapp/WEB-INF/jsp/layout/top_navbar.jsp
<p style="background-color: blue; padding: 5px; color: white;">Top Navbar</p>
(3) 添加 src/main/webapp/WEB-INF/jsp/layout/top_menu.jsp
<p style="background-color: red; padding: 5px; color: white;">Top Menu</p>
(4) 添加 src/main/webapp/WEB-INF/jsp/layout/footer.jsp
<p style="background-color: gray; padding: 5px; color: white;">Footer</p>
</body>
</html>
8. View & Controller
1) 添加 src/main/webapp/WEB-INF/jsp/home.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertDefinition name="header" />
<tiles:insertDefinition name="top_navbar" />
<tiles:insertDefinition name="top_menu" />
<h3>Home Page</h3>
<p>URL: ${request.getContextPath()} </p>
<c:if test="${not empty message}">
<p>${message}</p>
</c:if>
<p> </p>
<script type="text/javascript">
$(document).ready(function() {
console.log("Home Page");
});
</script>
<tiles:insertDefinition name="footer" />
2) 添加 src/main/java/com/example/controller/IndexController.java
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
mport org.springframework.ui.ModelMap;
@Controller
@RequestMapping("/")
public class IndexController {
@RequestMapping(method = RequestMethod.GET)
public String defaultIndex(ModelMap modelMap) {
modelMap.addAttribute("message", "Springmvc Tiles Demo");
return "home";
}
}
9. 运行
跳转到第 3 步,运行。
Tomcat 环境下,注解里的 “/” 和 src/main/webapp/index.jsp 会只认 index.jsp,可以删除 index.jsp, 或在 WEB-INF/web.xml 添加:
<welcome-file-list>
<welcome-file>springMVC</welcome-file>
</welcome-file-list>