From 4c8d1e965b00bfb91d7643535ad29e2f25a3a98c Mon Sep 17 00:00:00 2001 From: liosha84 <138026690+liosha84@users.noreply.github.com> Date: Sat, 14 Jun 2025 23:25:50 +0300 Subject: [PATCH] add angular material project as embedded in spring --- .gitignore | 1 + build.gradle | 28 +++++++++++++++ jambotron-ui/package-lock.json | 17 +++++++++ jambotron-ui/package.json | 1 + jambotron-ui/src/main.ts | 2 +- .../jambotronGroup/jambotron/MvcConfig.java | 17 +++++++++ .../jambotron/NotFoundHandler.java | 35 +++++++++++++++++++ src/main/resources/application.properties | 9 +++++ 8 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/jambotronGroup/jambotron/MvcConfig.java create mode 100644 src/main/java/com/jambotronGroup/jambotron/NotFoundHandler.java diff --git a/.gitignore b/.gitignore index ca8790f..e5f7cfe 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,4 @@ out/ /gradle/wrapper/gradle-wrapper.properties /.gitattributes /gradle/ +/src/main/resources/public/ diff --git a/build.gradle b/build.gradle index 092dfb4..9e02775 100644 --- a/build.gradle +++ b/build.gradle @@ -32,6 +32,34 @@ dependencies { implementation 'org.postgresql:postgresql:42.6.0' } +// copyResouces( type:Copy + +//tasks copyResouces( type:Copy){ +// +// def buildDir = "${project.buildDir}" +// +// from "{$buildDir}/jambotron-ui/dist/jambotron-ui/browser" // Source directory +// into "{$buildDir}/build/resources" // Target directory +// include '**/*' +//} + +apply plugin: 'java' + +tasks.register('copyResources', Copy) { + from 'jambotron-ui/dist/jambotron-ui/browser' + into 'src/main/resources/public' +} +processResources.dependsOn(copyResources) +compileJava.dependsOn(copyResources) + +sourceSets { + main { + resources { + srcDir 'jambotron-ui/dist/jambotron-ui/browser' + } + } +} + tasks.named('test') { useJUnitPlatform() } diff --git a/jambotron-ui/package-lock.json b/jambotron-ui/package-lock.json index be4fb3d..23950ff 100644 --- a/jambotron-ui/package-lock.json +++ b/jambotron-ui/package-lock.json @@ -8,6 +8,7 @@ "name": "jambotron-ui", "version": "0.0.0", "dependencies": { + "@angular/animations": "^20.0.3", "@angular/cdk": "^20.0.3", "@angular/common": "^20.0.0", "@angular/compiler": "^20.0.0", @@ -117,6 +118,22 @@ "yarn": ">= 1.13.0" } }, + "node_modules/@angular/animations": { + "version": "20.0.3", + "resolved": "https://registry.npmjs.org/@angular/animations/-/animations-20.0.3.tgz", + "integrity": "sha512-R6yv2RmrH49nW1ybgoOMw5pWzqaRYo8Kn3VtvrUDBty4TXjwc0addaw/t89n0smO3/lmBB4vnlRScePAEQZ/3w==", + "license": "MIT", + "dependencies": { + "tslib": "^2.3.0" + }, + "engines": { + "node": "^20.19.0 || ^22.12.0 || >=24.0.0" + }, + "peerDependencies": { + "@angular/common": "20.0.3", + "@angular/core": "20.0.3" + } + }, "node_modules/@angular/build": { "version": "20.0.2", "resolved": "https://registry.npmjs.org/@angular/build/-/build-20.0.2.tgz", diff --git a/jambotron-ui/package.json b/jambotron-ui/package.json index b98181b..806b70e 100644 --- a/jambotron-ui/package.json +++ b/jambotron-ui/package.json @@ -11,6 +11,7 @@ }, "private": true, "dependencies": { + "@angular/animations": "^20.0.3", "@angular/cdk": "^20.0.3", "@angular/common": "^20.0.0", "@angular/compiler": "^20.0.0", diff --git a/jambotron-ui/src/main.ts b/jambotron-ui/src/main.ts index 2411b88..99dd616 100644 --- a/jambotron-ui/src/main.ts +++ b/jambotron-ui/src/main.ts @@ -2,7 +2,7 @@ import { bootstrapApplication } from '@angular/platform-browser'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import {AppModule} from './app/app.module'; - +//import('@angular/animations/browser') platformBrowserDynamic().bootstrapModule(AppModule) diff --git a/src/main/java/com/jambotronGroup/jambotron/MvcConfig.java b/src/main/java/com/jambotronGroup/jambotron/MvcConfig.java new file mode 100644 index 0000000..4fdecc9 --- /dev/null +++ b/src/main/java/com/jambotronGroup/jambotron/MvcConfig.java @@ -0,0 +1,17 @@ +package com.jambotronGroup.jambotron; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class MvcConfig implements WebMvcConfigurer { + @Value("${spring.resources.static-locations}") + String resourceLocations; + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/public/**").addResourceLocations(resourceLocations); + } +} diff --git a/src/main/java/com/jambotronGroup/jambotron/NotFoundHandler.java b/src/main/java/com/jambotronGroup/jambotron/NotFoundHandler.java new file mode 100644 index 0000000..170f62c --- /dev/null +++ b/src/main/java/com/jambotronGroup/jambotron/NotFoundHandler.java @@ -0,0 +1,35 @@ +package com.jambotronGroup.jambotron; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.util.ResourceUtils; +import org.springframework.util.StreamUtils; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.servlet.NoHandlerFoundException; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.charset.Charset; + +@ControllerAdvice +public class NotFoundHandler { + @Value("${spa.default-file}") + String defaultFile; + + @ExceptionHandler(NoHandlerFoundException.class) + public ResponseEntity renderDefaultPage() { + try { + File indexFile = ResourceUtils.getFile(defaultFile); + FileInputStream inputStream = new FileInputStream(indexFile); + String body = StreamUtils.copyToString(inputStream, Charset.defaultCharset()); + return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(body); + } catch (IOException e) { + e.printStackTrace(); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("There was an error completing the action."); + } + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 1063fa9..f09f37d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -15,3 +15,12 @@ app.jwtSecret= ======================spring=back==================== app.jwtExpirationMs= 30000 app.jwtCookieName=springangularts app.origin=http://localhost:4200 + +spring.mvc.throw-exception-if-no-handler-found=true + +# Disable the default mappings +spring.resources.add-mappings=false + +spring.resources.static-locations=classpath:/public +spring.resources.cache.period=31557600s # 1 year if you want +spa.default-file=classpath:/public/browser/index.html