Springboot 返回 jsp

一、配置pom.xml

 1         <!--配置jsp jstl的支持-->  
 2         <dependency>  
 3             <groupId>javax.servlet</groupId>  
 4             <artifactId>jstl</artifactId>  
 5         </dependency>   
 6   
 7         <!--對jsp的支持-->  
 8         <dependency>  
 9             <groupId>org.apache.tomcat.embed</groupId>  
10             <artifactId>tomcat-embed-jasper</artifactId>  
11         </dependency>

二、配置application.properties(在src/main下建立webapp文件)

# 配置jsp文件的位置,默認位置爲:src/main/webapp spring.mvc.view.prefix:/WEB-INF/jsp/  #指向jsp文件位置:src/main/webapp/WEB-INF/jsp # 配置jsp文件的後綴 spring.mvc.view.suffix:.jsp

三、編寫jsp(文件放到src/main/webapp/WEB-INF/jsp目錄下)

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 
 2   pageEncoding="UTF-8"%>
 3 <html>
 4 <head><title>登陸頁面</title></head>
 5 <body>
 6 <form action="login.do" method="post">
 7    <table>
 8    <tr><td>學&nbsp;&nbsp;號:</td>
 9        <td><input type="text" name="num"/></td>
10    </tr>
11    <tr><td>密&nbsp;&nbsp;碼:</td>
12         <td><input type="password" name="password"/></td>
13    </tr>
14    <tr><td><input type="submit" value="登陸"/></td>
15    </tr>
16    </table>
17 </form>
18 </body>
19 </html>

四、編寫控制類

 1 package com.example.controller;  2 
 3 import org.springframework.stereotype.Controller;  4 import org.springframework.web.bind.annotation.RequestMapping;  5 
 6 
 7 //這個註解不能使用RestController,否則會返回模板類型的頁面
 8 @Controller  9 public class LoginController { 10     @RequestMapping("/login") 11     public String index() { 12 
13         return "login"; 14  } 15 }

五、編寫啓動類,重寫SpringBootServletInitializer方法

 1 package com.example.controller;  2 
 3 import org.springframework.boot.SpringApplication;  4 import org.springframework.boot.autoconfigure.SpringBootApplication;  5 import org.springframework.boot.builder.SpringApplicationBuilder;  6 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;  7 @SpringBootApplication  8 //返回jsp頁面必須繼承SpringBootServletInitializer類重寫裏面的方法
 9 public class SpringbootMybatisApplication extends SpringBootServletInitializer { 10 
11     public static void main(String[] args) { 12         SpringApplication.run(SpringbootMybatisApplication.class, args); 13  } 14 
15  @Override 16     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 17         return application.sources(SpringbootMybatisApplication.class); 18  } 19 }
相關文章
相關標籤/搜索