I am configuring spring mvc project to use angular 2 without spring boot.
my directory structure is :
Project
|
+--src
|
+--resources
| |
| +--css
| |
| +--js
| |
| +--angular
| |
| +--app/
| |
| +--node_modules/
| |
| +--package.json,systemjs.config.js,tsconfig.json
|
|
+--WEB-INF
|
+--pages
|
+--index.jsp
I have included the follwing lines in index.jsp
<!-- 1. Load libraries for angular 2 setup -->
<!-- Polyfill(s) for older browsers -->
<script src="${pageContext.request.contextPath}/resources/angular/node_modules/core-js/client/shim.min.js"></script>
<script src="${pageContext.request.contextPath}/resources/angular/node_modules/zone.js/dist/zone.js"></script>
<script src="${pageContext.request.contextPath}/resources/angular/node_modules/reflect-metadata/Reflect.js"></script>
<script src="${pageContext.request.contextPath}/resources/angular/node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="${pageContext.request.contextPath}/resources/angular/systemjs.config.js"></script>
<script>
System.import('${pageContext.request.contextPath}/resources/angular/app').catch(function(err){ console.error(err); });
</script>
I have followed the angular 2 quick start
https://angular.io/guide/quickstart
All the files contain the same code mentioned in the above link. The only thing i have changed is:
Copied app directory, node_modules directory and configuration files to resources directory and modified index.jsp to load it from there.
It is throwing the following exception:
GET http://localhost:8085/Phoenix/resources/angular/app/ 404 (Not Found) in browser console. Please suggest anything to fix this issue.
Follow below steps to achieve our main goal, to run SPRINGMVC + Angular2 on a single server.
Create normal Dynamic web project.
Add all dependancy required for spring or user maven pom.xml
Open CMD, navigate to angular2 application. Hit command npm install and then ng build or use ng build --prod for production.This command will create a "dist" folder, copy all files including all folders.
Paste those files and folders into WebContent directory.
Last thing, you need to change basehref="./" in index.html. Now you are ready to run server or you can deploy war file and serve it with tomcat or other servers.
Checkout this thread, I have mentioned file structure and its working fine.
I know there are many drawbacks to use these way for running application on a single server, but if it must required you can follow this.
If you change anything at angular side, you need to copy paste dist folder all time and then you can deploy it!
I think you are missing the Ressourcehandler configuration and also the Controller on Java site - Here are some examples for using Spring MVC and Angular
Related
I have a Spring Boot Web project that has a Spring Boot JPA project as a dependency like so:
spring_boot_web
|__.../application.yml
|
|__spring_boot_jpa
| |__.../data.properties
| |__.../data-test.properties
| |__.../data-dev.properties
| \__pom.xml
|
|__pom.xml
Web project uses the default application.yml file and jpa uses a properties file as yml are not supported by #PropertySource annotation.
I can run them alone flawlessly but when I try to include the jpa inside web there are problems creating the beans related to db. Is there any way to have those project running their own config files?
I just got it working. As the JPA project does not stand on its own (only for testing purposes) I just left some properties for the profiles I want that project to run standalone and then the main properties file under the web project. The structure is as follows:
spring_boot_web
|__.../application.yml
|
|__spring_boot_jpa
| |__.../application-test.yaml
| |__.../application-dev.yaml
| \__pom.xml
|
|__pom.xml
And then I made sure that there was only one #SpringBootApplication in between both projects altogether.
Also keep in mind that the main #SpringBootApplication has to be in a package in common for both projects:
spring_boot_web
|__foo.bar.core
| \__SpringWebApplication.java <- main #SpringBootApplication
|
|__foo.bar.core.web
| \__...
|
\__spring_boot_jpa
\__foo.bar.core.services
\__...
I have a gradle multiple projects. The structure is like below:
|--MyProject
| |--ejb-project
| | --build.gradle
| |--spring-project
| --com.mine.demo
| --Test.java
| | --build.gradle
| |--build.gradle
| |--settings.gradle
in the settings.gradle
include: ':ejb-project'
include: ':spring-project'
I defined a Test.java in the spring-project.
Now, if I wanna use Test.java in the EJB projects, How can I use it? Are there configurations to be configured?
Questions can be simplfy as this: Can I use a class from spring project in a ejb project within a gradle(or maven) multiple projects? If can, How can I use it?
I've never done this before, so barely know how to integrate spring in ejb. HELP!
In light of a recent problem I had, I would like to make sure it does not happen again. Kind of like a regression test for my build system.
I need a way to scan an ear (or other jar style package) to make sure a class is only once in it.
Example:
- test.ear
| - lib (folder)
| | - api.jar (zipped file)
| | - packageName
| | - ClassName.class
| - connector.rar (zipped file)
| - api.jar (zipped file)
| - packageName
| - ClassName.class
| - ejbs.jar
The pom.xml of ejbs.jar has a dependency on the api that brings the api.jar to the lib folder. The pom.xml of the connector.rar also has a dependency on the api that brings the api.jar to the connector.rar file, so while the reference to the same dependency, it still causes the above result.
As I have a maven build I'm looking for a way that integrates well there (if possible).
There's this enforcer rule you can use. Have a look at http://www.mojohaus.org/extra-enforcer-rules/banDuplicateClasses.html
I'm experimenting with Gradle's war plugin. At this point project is still using Ant. Its a standard webapp layout:
| - project
| ---- src/main/java
| ---- src/main/resource
| ---- src/main/webapp
| ---- src/main/webapp/WEB-INF/classes
In src/main/webapp/WEB-INF/classes there's a properties file containing key jawr.debug.on. For development purposes, it is usually set to true. During release phase this property is changed to false using Ant's propertyfile task.
I'm unable to find similar way of performing this in Gradle. I did find snippet below, but its not changing said file inside the resulting war:
ant.propertyfile(
file: "jawr.properties") {
entry( key: "jawr.debug.on", value: "false")
}
What would be the proper way to achieve this?
One way would be to filter it during war creation
war {
rootSpec.eachFile {
if (it.name == 'UserMessages.properties') {
filter { line ->
line.replace('#build.label#', "${buildLabel}-${stage}")
}
}
}
}
Let's assume that we have Spring Boot based web application using JSP templates. It can be even as simple as in the following example (from official Spring Projects repository)
Project structure:
|-src/main/java/
| |-sample.tomcat.jsp
| |-SampleTomcatJspApplication.java
| |-WelcomeController.java
|-src/main/resources/
| |-application.properties
|-src/test/java/
| |-...
|-src/main/webapp/
| |-WEB-INF
| |-jsp
| |-welcome.jsp
|-pom.xml
Properties file contains view prefix /WEB-INF/jsp/ and suffix .jsp and when requesting / we see properly rendered content of welcome.jsp.
WelcomeController.java
application.properties
Changes
Now let's make the following changes
Duplicate WelcomeController.java as WelcomeController2.java and change a bit request mapping, model attributes and returned view name, e.g.:
#RequestMapping("/2")
public String welcome2(Map<String, Object> model) {
model.put("message", "Hi from Welcome2");
return "welcome2";
}
Duplicate welcome.jsp as welcome2.jsp so that src/main/webapp will be like this:
|-src/main/java/
| |-sample.tomcat.jsp
| |-SampleTomcatJspApplication.java
| |-WelcomeController.java
| |-WelcomeController2.java
...
|-src/main/webapp/
| |-WEB-INF
| |-jsp
| |-welcome.jsp
| |-welcome2.jsp
Then when requesting /2 we can see properly rendered content of welcome2.jsp.
The question
What is the way of splitting such project into two maven projects, so that both WelcomeController2.java and welcome2.jsp could be moved to other project (maven dependency) and still be successfully resolved when /2 URL is requested?
Note that with Spring Boot web-fragment.xml (that could be placed in META-INF directory of dependency) is ignored.
Unfortunately, I don't know of an easy way to do this but one approach I've used is to create a Maven artifact just like normal for the main project, in your case probably a WAR artifact. This project will need to have a dependency upon your second project. Then your second project would consist of two components:
A standard Maven JAR artifact containing the compiled class files.
A Maven assembly ZIP consisting of the JSP files that need to be included in the WAR archive as well. This will be generated from the second project during the package phase, but will need to be included as a separate dependency on the main project using a zip classifier.
When the first project is built, you'll need to unpack the assembly dependency as part of the packaging process for the WAR archive. If you want this to work in an IDE, you'll probably need to unpack it in a fairly early phase, such as process-resources or generate-sources.