【问题标题】:How to load html views in spring?如何在春季加载html视图?
【发布时间】:2017-01-26 23:06:04
【问题描述】:

我当前用于解析 html 视图的工作代码是这样的

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

但我需要返回视图,例如

return "/static/html/index";

我怎样才能做到这样?

return "index";

【问题讨论】:

    标签: spring spring-mvc spring-boot


    【解决方案1】:

    如果你使用的是spring Boot,它会自动添加位于以下任一目录中的静态web资源:

    /META-INF/resources/
    /resources/
    /static/
    /public/
    

    在使用 RESTful Web 服务的情况下,如果您将资源放入公共文件夹中,这可能是一个好方法,这就是您的控制器应该的样子。

    @Controller
    class Controller{   
    
        @RequestMapping("/")
        public String index() {
             return "index.html";
        }
    
    }
    

    【讨论】:

    • 谢谢@Priyamal。我不知道这个。
    【解决方案2】:

    将前缀改为/static/html/

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

    因此,当您以"index" 返回时,它将更改为/static/html/index.html。

    【讨论】:

    • 感谢我的努力。这解决了我的问题。拯救了我的一天..
    猜你喜欢
    • 1970-01-01
    • 2015-03-12
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 2017-04-28
    相关资源
    最近更新 更多