<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Spring Boot JSP 視圖解析器示例 > 原文: [https://howtodoinjava.com/spring-boot/spring-boot-jsp-view-example/](https://howtodoinjava.com/spring-boot/spring-boot-jsp-view-example/) 學習創建和配置 **spring boot jsp 視圖解析器**,該解析器使用 **JSP 模板**文件渲染視圖層。 本示例使用**嵌入式 Tomcat 服務器**運行該應用程序。 ## 源代碼結構 該應用程序中的文件作為給定的結構放置在圖像中。 ![Spring Boot Application Structure](https://img.kancloud.cn/9a/7f/9a7f712e3218c953bd337a0e77182847_370x476.jpg) Spring Boot 應用結構 ## Maven 依賴項 – `pom.xml` 此應用程序使用以下給定的依賴項。 ```java <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.howtodoinjava</groupId> <artifactId>spring-boot-demo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-demo Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Tomcat Embed --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- To compile JSP files --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> </project> ``` ## Spring Boot 應用程序初始化器 產生可部署 war 文件的第一步是提供`SpringBootServletInitializer`子類并覆蓋其`configure()`方法。 這利用了 Spring Framework 的 Servlet 3.0 支持,并允許您在 Servlet 容器啟動應用程序時對其進行配置。 ```java package com.howtodoinjava.app.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication public class SpringBootWebApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringBootWebApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootWebApplication.class, args); } } ``` ## Spring 控制器 控制器類可以將方法映射到應用程序中的特定 URL。 在給定的應用程序中,它具有兩個視圖,即`/`和`/next`。 ```java package com.howtodoinjava.app.controller; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexController { @RequestMapping("/") public String home(Map<String, Object> model) { model.put("message", "HowToDoInJava Reader !!"); return "index"; } @RequestMapping("/next") public String next(Map<String, Object> model) { model.put("message", "You are in new page !!"); return "next"; } } ``` ## Spring Boot JSP `ViewResolver`配置 要解決 **JSP** 文件的位置,可以采用兩種方法。 #### 1)在`application.properties`中添加條目 ```java spring.mvc.view.prefix=/WEB-INF/view/ spring.mvc.view.suffix=.jsp //For detailed logging during development logging.level.org.springframework=TRACE logging.level.com=TRACE ``` #### 2)配置`InternalResourceViewResolver`服務 JSP 頁面 ```java package com.howtodoinjava.app.controller; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @Configuration @EnableWebMvc @ComponentScan public class MvcConfiguration extends WebMvcConfigurerAdapter { @Override public void configureViewResolvers(ViewResolverRegistry registry) { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/view/"); resolver.setSuffix(".jsp"); resolver.setViewClass(JstlView.class); registry.viewResolver(resolver); } } ``` ## JSP 文件 下面是這個 Spring Boot JSP 示例中使??用的兩個 JSP 文件。 `index.jsp` ```java <!DOCTYPE html> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <html lang="en"> <body> <div> <div> <h1>Spring Boot JSP Example</h1> <h2>Hello ${message}</h2> Click on this <strong><a href="next">link</a></strong> to visit another page. </div> </div> </body> </html> ``` `next.jsp` ```java <!DOCTYPE html> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <html lang="en"> <body> <div> <div> <h1>Another page</h1> <h2>Hello ${message}</h2> Click on this <strong><a href="/">link</a></strong> to visit previous page. </div> </div> </body> </html> ``` ## 示例 編寫完所有代碼并將其放置在文件夾中之后,通過執行`SpringBootWebApplication`類中的`main()`方法來運行應用程序。 #### 現在點擊 URL:`http://localhost:8080/` ![Spring Boot Application - index](https://img.kancloud.cn/19/18/19188edd98c889d745229953965fe10f_399x242.jpg) Spring Boot 應用 – index #### 點擊下一個鏈接 ![Spring Boot Application - next](https://img.kancloud.cn/e9/5f/e95f12b43869ed544ba7b332ef726831_409x232.jpg) Spring Boot 應用 – next ## Spring Boot JSP 示例源代碼 用下面的鏈接下載該應用程序的源代碼。 [下載源碼](https://howtodoinjava.com/wp-content/downloads/spring-boot-demo-jsp-example.zip) 在本教程中,我們通過示例學習了 Spring Boot JSP `ViewResolver`,以及其他可能使用的多個視圖解析器。 學習愉快! **參考:** [Spring Boot](https://projects.spring.io/spring-boot/) [Spring Boot 應用程序屬性](https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html) [Spring MVC](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-default-servlet-handler)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看