【问题标题】:Get rid of first View Controller with Spring 4 and AngularJS使用 Spring 4 和 AngularJS 摆脱第一个 View Controller
【发布时间】:2016-11-23 14:34:03
【问题描述】:

我使用RestController 通过AngularJS 获取数据,但我仍然需要常规 控制器来加载index.html

@Controller
public final class LayoutController {
    @RequestMapping(value = "/")
    public String getIndexPage() {
        return "/resources/index";
    }
}

所有其他控制器都是RestControllers。如果索引文件有jsp 扩展名,我不需要LayoutController,但是当它是html 时需要它。 这是我的dispatcher 配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="order" value="1"></property>
        <property name="prefix">
            <value>/</value>
        </property>
        <property name="suffix">
            <value>.html</value>
        </property>
    </bean>

</beans>

这是applicationContext的一部分:

<mvc:annotation-driven/>
<tx:annotation-driven />

<mvc:resources mapping="/resources/**" location="WEB-INF/resources/" />

当我想使用index.html 而不是index.jsp 时,有没有办法摆脱LayoutController?提前感谢您的帮助。 基于公认答案的解决方案:

<mvc:view-controller path="/" view-name="/resources/index.html"/>

完全没有LayoutControllerInternalResourceViewResolver

【问题讨论】:

    标签: java angularjs spring spring-mvc spring-4


    【解决方案1】:

    你可以使用view-controller

    &lt;mvc:view-controller path="/" view-name="index"/&gt;

    我假设您已经有一个指向 jsp 位置的 InternalResourceViewResolver 。如果不是,您还必须将其包含在应用程序上下文中。

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

    如果不想包含 InternalResourceViewResolver 也可以直接转发到 html。

    <mvc:view-controller path="/" view-name="forward:/WEB-INF/resources/index.html"/>
    

    希望,这就是你要找的。​​p>

    【讨论】:

    • 谢谢 :) 通过直接指向 HTML 文件,我摆脱了对 LayoutController 和 InternalResourceViewResolver 的需求,但对我来说,最终解决方案看起来与你的有点不同:&lt;mvc:view-controller path="/" view-name="/resources/index.html"/&gt; - Intellij 向我展示了“无法解析符号'/resources/index.html'”,但是......它可以工作。
    猜你喜欢
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多