简体   繁体   English

如何在同一台Tomcat服务器上运行基于angular2和spring mvc的rest api?

[英]How to run angular2 and spring mvc based rest api on the same tomcat server?

I am building an angular 2 app that has to use spring mvc based rest api at the backend. 我正在构建一个必须在后端使用基于spring mvc的rest api的angular 2应用程序。 I am using maven, not using spring-boot, and want to map the index.html of angular2 to the base URL of the deployed WAR file. 我正在使用maven,而不是使用spring-boot,并且想要将angular2的index.html映射到已部署的WAR文件的基本URL。 I am seeing the index.html to be present in the MyApplication named folder in webapps folder of tomcat, but somehow trying to access the site via the base URL gives no resource available. 我看到index.html存在于tomcat的webapps文件夹中的MyApplication命名文件夹中,但是以某种方式尝试通过基本URL访问该站点没有可用资源。 Could somebody help me. 有人可以帮我吗。 This is my layout. 这是我的布局。

web.xml web.xml

<display-name>MyApplication</display-name>

<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>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

mvc-dispatcher-servlet.xml mvc-dispatcher-servlet.xml

<context:annotation-config/>

<context:component-scan base-package="com.rochak.*" />

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

MyController.java in com/rochak/controller com / rochak / controller中的MyController.java

@Controller
public class MyController {

@RequestMapping(value="*")
public String getIndex(ModelMap model){
    return "index";
}
}

and angular folder lies in WEB-INF folder with it's index.html 和角度文件夹位于WEB-INF文件夹中,其索引为html

Why is index.html not being found by spring mvc? Spring MVC为什么找不到index.html?

Change your 'url-pattern' to something like '/server/'. 将您的“网址模式”更改为“ / server /”。 It would work. 会的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM