Reputation: 3636
I am developing a simple Spring MVC(v4.1.2) and Angular4 app.
Basically this app does CRUD operations by making http requests from angular client.
Following combination works perfectly fine: Angular app ran using "ng serv" Spring MVC war deployed in a application server.
Now, I am trying to combine both the client and server into a single project. With this I should be able to generate a single war file containg both client and server side code.
For this,
dist
of angular app into ..src/main/webapp/
.
overridden following methods of org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
@Configuration
@EnableWebMvc
public class ServletContextConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorParameter(false).favorPathExtension(true).ignoreAcceptHeader(false)
.defaultContentType(MediaType.TEXT_HTML).mediaType("xml", MediaType.APPLICATION_XML)
.mediaType("json", MediaType.APPLICATION_JSON).mediaType("html", MediaType.TEXT_HTML);
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/dist/");
}
}
Changed web.xml
as following:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
After deploying and running the app, I get following errors in the web console:
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>StoreClientApp</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<script type="text/javascript" src="inline.bundle.js"></script><script
type="text/javascript" src="polyfills.bundle.js"></script><script
type="text/javascript" src="styles.bundle.js"></script><script
type="text/javascript" src="vendor.bundle.js"></script><script
type="text/javascript" src="main.bundle.js"></script></body>
</html>
It is evident from the console logs that Spring is triggering http requests for the static resources in index.html
. Is this expected behaviour? What should be the changes so that Spring considers static resources as actually static and fetches them from relative path?
Thanks.
Upvotes: 1
Views: 395
Reputation: 703
http://127.0.0.1:7001/inline.bundle.js It hasn't contextPath, so you need correct it.
http://127.0.0.1:7001/store-server/inline.bundle.js
1.static definition
<base href="/store-server">
2.dynamic definition
If you use pure HTML, you can get contextPath by the following.
<script type="text/javascript">
var contextPath = "/"+window.location.pathname.substring(0, window.location.pathname.indexOf("/",2));
document.head.innerHTML = document.head.innerHTML + "<base href='" + contextPath + "' />";
</script>
Upvotes: 2
Reputation: 4356
In your index.html Try to add a forward slash to each one of the URLs of the script file being used . Like this
<script type="text/javascript" src="/inline.bundle.js"></script><script
type="text/javascript" src="/polyfills.bundle.js"></script><script
type="text/javascript" src="/styles.bundle.js"></script><script
type="text/javascript" src="/vendor.bundle.js"></script><script
type="text/javascript" src="/main.bundle.js"></script></body>
Upvotes: 0
Reputation: 6133
Your context path is /store-server
and the requests to your static resources are to the root. You can do one of the following to fix it
<base href="/">
to <base href="/store-server">
Upvotes: 1