简体   繁体   中英

how to get Spring MVC in intellij to run HTML

I have a basic spring MVC project (the basic mold they create) and am trying to get it to run a html instead of a JSP (JSP works perfect)

my controller

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

my mvc dispatcher service

<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/" />

I've tried numerous things, such as mapping to /WEB-INF/pages/** , removing the InternalResourceViewResolver completely (W/ the previous mappings,) leaving the suffix empty, with html, etc but to no avail. I've looked at the other questions similar to this but no luck. Also read about static folder for the htmls, but was confused... What am I doing wrong?

the file structure for webpages webapp

--WEB-INF ----pages ------hello.jsp, index.html

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

Mapping value stands for URL mapping. So give here for example "/pages/**"

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

Now you can access to static files like html page by localhost:8080/app/pages/index.html for example.

In Java Controller instead of return "index"; there should be return "redirect:/pages/index.html";

Read this example to get all detailed informations. Hope i could help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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