Spring Boot Web开发一之JSP
springboot内部对jsp的支持并不是特别理想,而springboot官方推荐的视图是Thymeleaf,很多书籍和博客也是使用Thymeleaf做web开发,但是对于java开发人员来说还是大多数人员喜欢使用jsp,接下来我们来讲解下springboot是如何支持jsp的。
本章目标
整合springboot web项目支持jsp作为视图输出。
1、新建web项目
通过idea新建web项目,具体请参考 初识springboot之快速搭建!
2、配置pom.xml
配置pom.xml,下载项目所需依赖包,配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.crazyang</groupId>
<artifactId>secondjspdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SecondJSPDemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--JSP Maven-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--Servlet Maven-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!--JSTL Maven-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、修改配置文件
修改application.properties文件,如下所示,springboot自动读取配置文件,让springmvc支持视图的跳转目录指向为/main/webapp/WEB-INF/jsp
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
4、新建jsp文件
在/src/main/下新建/webapp/WEB-INF/jsp文件夹,用于存放jsp文件,新建index.jsp和success.jsp两个jsp文件,一个用于模拟登陆页面,一个用于模拟登陆成功跳转页面。代码如下:
index.jsp
<%--
Created by IntelliJ IDEA.
User: crazyang
Date: 2018-6-6
Time: 14:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
登陆界面,请输入用户名和密码登陆
<form action="/login">
<input type="text" name="name"/>
<input type="password" name="password"/>
<button type="submit">提交</button>
</form>
</body>
</html>
success.jsp
<%--
Created by IntelliJ IDEA.
User: crazyang
Date: 2018-6-6
Time: 14:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
登陆成功,欢迎来到spring boot.
</body>
</html>
5、新建IndexContoller,启动项目
新建IndexController,主要用于模拟首页和获取登陆请求,完成页面跳转,代码如下:
package com.crazyang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
/**
* Created with IntelliJ IDEA.
*
* @Author crazyang
* @Desciption:
* @Date 2018-6-6 14:02
*/
@Controller
public class IndexController {
@RequestMapping("/index")
public String index(){
return "index";
}
@RequestMapping("/login")
public String login(HttpServletRequest request){
String name = request.getParameter("name");
String password = request.getParameter("password");
System.out.println("name = "+name+" , password = "+ password);
return "success";
}
}
6、启动成功,操作效果图
6.1项目首页展示
在浏览器窗口输入http://localhost:8080/index,进入如下页面:
6.2打印提交信息
在输入框中分别输入 crazyang 和123456,点击提交,后台控制台打印出提交信息,如下图所示:
6.3页面跳转
提交成功后,页面跳转,如下图所示:
7、总结
本章主要是讲解了springboot集成jsp作为渲染视图,结合springmvc来转发请求。