首页 > 解决方案 > 从 Angular 7 调用遗留代码 jsp/servlet 代码返回 302 错误

问题描述

我正在使用 jsp 和 servelts 将我们的遗留代码更新到 angular 7。但是,遗留代码的某些部分我仍然需要维护。因此,我必须对具有以下 Web 服务的 Web 服务进行一些调用。

@RequestMapping(value = "/mainpage", method = { RequestMethod.GET, RequestMethod.POST })
    public ModelAndView portalMainPage(HttpServletRequest request, HttpServletResponse response) throws Exception { 
/* Code here to do a few things */
return new ModelAndView(".company.portal.mainpage");
}

我尝试从角度调用它,但正如您所见,它返回一个模型和视图,并有自己的用于渲染的 jsp 页面。

我如何从角度调用它并让它呈现 jsp 页面?

我试图调用它,但我收到一条 HTTP 302 错误消息,它重定向到 index.html 页面,该页面是 angular 的欢迎页面。它无法呈现 jsp 页面。

我称之为直接来自浏览器的方式:https://company/portal/mainpage.do。完成此操作后,我检查了开发人员工具中的网络设置。它显示 302 消息,然后将其重定向到 index.html。

标签: angularspringspring-rest

解决方案


所以我能够解决这个问题。问题是 xml 文件中没有视图解析器。

所以我在 xml 文件中添加了以下代码。添加后,问题解决了

<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass">
        <value>org.springframework.web.servlet.view.tiles2.TilesView</value>
    </property>
</bean>
<bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/config/spring-tiles.xml</value>
            <!-- <value>/WEB-INF/config/cwf/tiles-defs-cwf.xml</value> -->
        </list>
    </property>
</bean>

推荐阅读