简体   繁体   English

Spring MVC 4 与 HTML 页面而不是 JSP

[英]Spring MVC 4 with HTML pages instead of JSP

I am using Spring MVC 4 to build a web app, when I run the application the default home page is index.jsp, I want to change it to index.html. I am using Spring MVC 4 to build a web app, when I run the application the default home page is index.jsp, I want to change it to index.html.

I have tried a lot of solutions, and no one is worked for me.我尝试了很多解决方案,但没有一个适合我。

web.xml: web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>TEST</display-name>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>SpringDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>es.test.test</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

MvcConfiguration.java MvcConfiguration.java

@Configuration
@ComponentScan(basePackages = "com.test.test")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/**").addResourceLocations("/assets/");
    }
}

IndexController.java索引控制器.java

@RestController
public class IndexController {

    @GetMapping(value = "/")
    public ModelAndView test(HttpServletResponse response) throws IOException {
        return new ModelAndView("index");
    }

}

I have changed the suffix to.html and it is not working, also the from web.xml and still have the same problem.我已将后缀更改为 .html 并且它不起作用,web.xml 仍然存在同样的问题。

Add in your web.xml follow code添加您的 web.xml 跟随代码

<servlet> 
     <servlet-name>htmlServlet</servlet-name> 
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> 
        <load-on-startup>2</load-on-startup> 
 </servlet> 
 <servlet-mapping> 
        <servlet-name>htmlServlet</servlet-name> 
        <url-pattern>*.html</url-pattern>
 </servlet-mapping>

and modify并修改

resolver.setSuffix(".jsp");

to

to  resolver.setSuffix(".html");

in your MvcConfiguration.java在你的 MvcConfiguration.java

if not working如果不工作

modify controller like this像这样修改 controller

@GetMapping(value = "/")
public String test(HttpServletResponse response) throws IOException {
    return "index";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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