Spring Boot系列02-Spring Boot + JSP 整合进行网页开发

IDE创建Maven项目

MVC展示层模板Spring Boot官方不推荐使用JSP,
但是像我这有已经用惯了JSP,再换FreeMarker/Velocity学习成本有点大。
还用JSP,有两块需要注意的地方。

需要创建Maven web项目,在IDE中创建maven项目时,ArtifactId选maven-archtype-webapp

需要在src/main/resources目录下的核心配置文件application.properties中追加如下配置
application.properties

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

最简单pom依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency> 
    </dependencies>

JSP页面开发

新建源文件夹src/main/webapp
在src/main/webapp下创建WEB-INF/views文件夹
tt.jsp内容如下

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Full Layout - jQuery EasyUI Demo</title>
</head>
<body>
    <input type="button" value="点我" onclick="f()"/>
    <input type="text" style="height:100px;width:90%" id="input"/>
</body>
</html>

Java后端代码

driver.TesterMan.java

package driver;

import java.util.Map;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@EnableAutoConfiguration
@Controller

public class TesterMan{
    public static void main(String[] args) throws Exception {
        SpringApplication.run(TesterMan.class, args);
        System.out.println("spring boot running ...");
    }  

    @RequestMapping("/tt")
    public String index(Map<String,Object> ){
        return "/tt";
    }
}

测试

以java application运行程序
在浏览器访问localhost:8080/tt
看到一个按钮控件、一个文本框控件,说明Spring Boot + Jsp整合成功

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值