【问题标题】:how to get Spring MVC in intellij to run HTML如何让 Intellij 中的 Spring MVC 运行 HTML
【发布时间】:2015-09-16 18:15:38
【问题描述】:

我有一个基本的 spring MVC 项目(他们创建的基本模具),并试图让它运行 html 而不是 JSP(JSP 工作完美)

我的控制器

@Controller
@RequestMapping("/")
public class HelloController {
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        return "index";
    }
}    

我的 mvc 调度服务

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="com.springapp.mvc"/>

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


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

我尝试了很多事情,例如映射到 /WEB-INF/pages/** ,完全删除 InternalResourceViewResolver(W/ 以前的映射),将后缀留空,使用 html 等,但无济于事。我看过与此类似的其他问题,但没有运气。还阅读了关于 htmls 的静态文件夹,但很困惑......我做错了什么?

网页的文件结构 网络应用

--WEB-INF ----页数 ------hello.jsp, index.html

【问题讨论】:

    标签: java html spring spring-mvc intellij-idea


    【解决方案1】:
    <mvc:resources mapping="/WEB-INF/pages/" location="/WEB-INF/pages/" />
    

    映射值代表 URL 映射。所以在这里举个例子“/pages/**”

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

    现在您可以通过localhost:8080/app/pages/index.html 访问静态文件,例如 html 页面。

    在 Java 控制器中而不是 return "index"; 应该有 return "redirect:/pages/index.html";

    阅读this 示例以获取所有详细信息。希望我能帮上忙。

    【讨论】:

      猜你喜欢
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2017-01-30
      • 2015-12-20
      • 2016-10-25
      相关资源
      最近更新 更多