手把手教你如何使用Java语言开发门户网站,后续将不断迭代更新,针对当前主流门户网站建设应该考虑的问题常见的解决方法及实践技巧
本章节目标
- 使用Eclipse工具,基于Maven搭建由Spring Boot +Spring MVC +Thymleaf 构建Web门户基础框架
- 采用yml方式进行配置
搭建基本环境
目前Spring 官方提供了web的方式进行快速构建,可访问地址:https://start.spring.io/ 地址,那这里为了手把手教大家,就自己动手、丰衣足食了,有兴趣的小伙伴可以自己动动小手指,自己DIY
使用Eclipse 创建maven项目
项目名称:spring-boot-infrastructure-projects
打开eclipse , File > New > Maven Project
New Maven project
选择Next >, 选择Artifact Id : maven-archetype-quickstart
选择Next >, 输入项目信息
选择Finish完成
创建源文件:
src/main/resource
src/test/resource
Eclipse > 文件 > 新建 > 新建 Source Folder
src/mian/resources
至此,一个简单的Maven项目构建完成,截图如下
Maven Project Build Finished
配置Spring Boot + Spring MVC + Thymleaf Hello World 环境
pom.xml文件
<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.lab</groupId>
<artifactId>spring-boot-infrastructure-projects</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-infrastructure-projects</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--
| Spring Boot
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--
| thymeleaf
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
</dependencies>
<!--
| Spring IO Platform 依赖管理器,将Spring各个组件之间的版本兼容性交给Spring来统一维护管理,避免存在兼容性问题。
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Cairo-RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>spring-boot-infrastructure-projects</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置文件: application.yml
server:
port: 8080
servlet:
context-path: /
spring:
http:
encoding:
charset: UTF-8
force: true
enabled: true
messages:
encoding: UTF-8
thymeleaf:
server:
port: 8080
servlet:
context-path: /
spring:
http:
encoding:
charset: UTF-8
force: true
enabled: true
messages:
encoding: UTF-8
thymeleaf:
# Prefix that gets prepended to view names when building a URL.
prefix: classpath:/templates/
# Suffix that gets appended to view names when building a URL.
suffix: .html
# Template encoding.
encoding: UTF-8
# Enable template caching.
cache: false
# Enable MVC Thymeleaf view resolution.
enabled: true
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.
mode: HTML
# Check that the templates location exists.
check-template-location: true
servlet:
# Content-Type value.
content-type: text/html
配置Spring MVC, WebMVCConfig .java
package com.lab.spring.boot.infrastructure.projects.config.web;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
*
* Spring MVC WebConfig
*
* @author Martin
* @since 1.0.0
*/
@Configurable
public class WebMVCConfig implements WebMvcConfigurer {
}
首页Hello World 页面配置PortalController.java
package com.lab.spring.boot.infrastructure.projects.component.portal.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
*
* Portal Controller
*
* @author Martin
* @since 1.0.0
*/
@Controller
public class PortalController {
@RequestMapping(value="/",method= RequestMethod.GET)
public String index() {
return "index";
}
}
最终项目目录结构如下:
Finished
启动运行: Application.java 右键启动运行
启动成功 端口:8080
打开浏览器地址,输入地址访问:
http://localhost:8080/
好了,一个简单的Spring Boot + Spring MVC + Thymleaf 基础框架就搭建完成了,后续将在此基础之上做其他组件的扩展。
源代码已经更新到Github上
项目地址:spring-boot-infrastructure-projects
Git检出地址: https://github.com/DataZhenTech/spring-boot-infrastructure-projects.git
作者:
Martin
描述:
全观数据栈,焦距行业、科技、技术、区域性大数据。
-- 看完这张图,就到此结束吧! --
网友评论