springboot中的视图解析器 23

2020-04-09  本文已影响0人  张力的程序园

本节将讲述springboot中的视图解析。

1、环境约束

2、前提约束

2.1、方式一

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    ......
     <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/webapp</directory>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
        </resources>
     </build>
server.port=8089
spring.mvc.view.prefix=/WEB-INF/pages/
spring.mvc.view.suffix=.jsp
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class PageController {
    @RequestMapping("/toIndex")
    public ModelAndView toIndex()
    {
        ModelAndView modelAndView = new ModelAndView("index");
        return modelAndView;
    }
}

2.2、方式二

server.port=8089
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/pages/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读