翻新第一个私活 CMS 项目

本贴最后更新于 2519 天前,其中的信息可能已经时移世易

1. 项目背景

项目地址: https://github.com/wangyuheng/Haro-CMS

最近因为换电脑整理资料的过程中,发现了自己之前写过的一些项目,现在准备拿出来升级一下,构建自己的技术体系。看着三个月前的代码,已经似曾相识了,那三年前的呢?
这个项目是用来做企业展示的简易 CMS 系统,后台管理员可以编辑展示内容,发布新闻;访问用户可以查看企业信息,并反馈建议。这个系统当时应该熬了几个通宵,因为 5000 大洋实在太吸引人。。而且还是三个人分。。。人的时间好像是越往后越值钱,现在看可能觉得不值,但还是很感谢熬夜编码的自己。
项目当时考虑了很多:分布式,前后端分离,甚至是 saas 化,希望根据用户反馈,或者再接到类似的项目,可以进一步完善,但是并没有什么后续。所以项目历史就是一个基于 web.xml 配置的 spring 单机应用。

1.1 依赖项目

  1. spring-boot
  2. h2
  3. gson

目前将其升级为 spring-boot 应用,并且为了开发演示方便,开发环境使用了 H2 数据库。一直觉得,项目不需要修改任何配置就能跑起来很重要。

2. 项目 code

企业信息展示,分为 3 个 module

  1. admin 管理人员编辑展示内容
  2. view 展示企业信息及新闻
  3. core 业务数据层

haro_designpng

新版 spring boot 已不推荐使用 jsp,带来很多不便,如: 不能直接运行 java, 需要使用 maven spring boot 插件运行。

mvn spring-boot:run

admin 和 view 只负责业务渲染与鉴权,业务操作放在 core 中,方便后期进行前后端分离。
生产环境数据库使用 mysql,为了方便演示,开发环境使用 H2 内嵌数据库。

admin

haro_adminpng

view

haro_viewpng

3 问题

之前的项目基于 spring 开发,采用 web.xml 配置。spring-boot 通过约定大于配置,极大的简化了这部分工作,但是有时候又会因为不熟悉带来迷茫。明明我没有这个配置,怎么就生效了。。而且 jsp 又不推荐使用,所以虽然大部分是在删代码,单还是遇到了很多问题。

3.1 设置视图解析器 InternalResourceViewResolver

原项目在 mvc-dispatcher-servlet.xml 文件中配置

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean>

spring-boot 提供了默认配置 prefix = "spring.mvc",详情可以参看 org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties 类, 并在 org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration 类中自动注入。

@Configuration @ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class }) @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10) @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class }) public class WebMvcAutoConfiguration { ... @Bean @ConditionalOnMissingBean public InternalResourceViewResolver defaultViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix(this.mvcProperties.getView().getPrefix()); resolver.setSuffix(this.mvcProperties.getView().getSuffix()); return resolver; } ... }

所以只需在 application.properties 中添加配置项

spring.mvc.view.prefix=/WEB-INF/pages/ spring.mvc.view.suffix=.jsp

3.2 静态资源请求

未实现前后端分离的项目,css、js 等静态资源仍保存在项目中。spring-boot 建议保存在 resource/static 目录下,并通过 spring.resources.static-locations 属性设置目录位置,并且需要制定 mapping 规则。

spring.mvc.static-path-pattern=/static/** spring.resources.static-locations=classpath:/static/

3.3 spring-boot 运行 war 项目

需要添加 tomcat-embed 依赖,并且设置 servlet 初始化。如果使用 jsp 标签,一般也需要添加 jstl 依赖。

<dependencies> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Provided --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
@SpringBootApplication @ComponentScan(basePackages={"wang.crick.business.haro"}) public class ViewAPP extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(ViewAPP.class); } public static void main(String[] args) { SpringApplication.run(ViewAPP.class, args); } }

需要通过 spring-boot maven 插件运行。

mvn spring-boot:run

3.4 自定义请求后缀匹配

旧项目中为了提高 seo 通过 servlet-mapping 指定请求路径为.html

<servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>

spring-boot 在 org.springframework.boot.autoconfigure.web.ServerProperties 提供了 servlet 相关配置,可以通过 application.properties 配置

server.servlet.path=*.html

但是目前希望忽略后缀,即请求 index 和 index.html 都可以路由到对应的页面,此时可以通过设置 pathmatch 属性实现

/**
* Whether to use suffix pattern match (".*") when matching patterns to requests. * If enabled a method mapped to "/users" also matches to "/users.*". */
spring.mvc.pathmatch.use-suffix-pattern=true

3.5 使用内存数据库 H2

为了让程序可以“无痛”运行,在代码中使用了基于内存的 H2 数据库,生产环境可以考虑 mysql 等关系型数据库,只需修改对应的配置即可。

需要添加 H2 依赖,引入驱动。

<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency>

在配置文件 application.properties 中直接配置路径、驱动、以及账户信息

spring.datasource.url=jdbc:h2:mem:~/.h2/haro spring.datasource.schema=classpath:db/schema.sql spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=root spring.datasource.password=123456

db/schema.sql 放置在 resource 目录下,保存了 DDL 以及开发环境所需数据。DDL 支持情况为标准 sql 语句,部分工具导出的脚本需要简单修改。在程序启动过程中,会初始化执行,如果有异常,会在控制台中有详细提示。

  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3194 引用 • 8214 回帖
  • CMS
    10 引用 • 42 回帖
  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    945 引用 • 1460 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...