【Java】在 Spring Boot 2 中致敬 JSP

Spring Boot" title="Spring Boot">Spring Boot 2 中致敬 JSP

haozi发布于 今天 14:42

一起来看看我们需要些什么

项目结构

咱们可以从 Spring Initializer 获取项目框架。

项目依赖

<u>pom.xml</u>

<?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>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.1.6.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>com.eprogrammerz.examples.spring</groupId>

<artifactId>spring-boot-jsp</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>spring-boot-jsp</name>

<description>Example Spring Boot with JSP view</description>

<properties>

<java.version>1.8</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</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>

<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>

配置

  1. 启动类配置

SpringBootServletInitializer 按传统的 WAR包 部署方式来运行 SpringBootJspApplication
<u>SpringBootJspApplication.java</u>

package com.eprogrammerz.examples.spring.springbootjsp;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication

public class SpringBootJspApplication extends SpringBootServletInitializer {

@Override

protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

return application.sources(SpringBootJspApplication.class);

}

public static void main(String[] args) {

SpringApplication.run(SpringBootJspApplication.class, args);

}

}

  1. Resources

<u>application.properties</u>

spring.mvc.view.prefix: /WEB-INF/jsp/

spring.mvc.view.suffix: .jsp

Controller and View Template

  1. 写一个简单映射方法的Controller

package com.eprogrammerz.examples.spring.springbootjsp.controllers;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestParam;

@Controller

public class HelloController {

@GetMapping({"/", "/hello"})

public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {

model.addAttribute("name", name);

return "hello";

}

}

  1. 将下面内容保存成 JSP 文件,放在src/main/webapp/WEB-INF/jsp/目录

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Hello ${name}!</title>

</head>

<body>

<h2 class="hello-title">Hello ${name}!</h2>

</body>

</html>

使用Maven运行

在项目根路径下使用命令行运行程序。

mvn clean spring-boot:run

访问 localhost:8080 测试你的程序效果。

至此,使用 Spring Boot 2 展示 JSP 页面基础配置就完成,希望对大家有一定帮助。

提前㊗️大家新年新气象,2021年技术更上一个新台阶!

javaspringboot

阅读 35发布于 今天 14:42

本作品系原创,采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议

avatar

haozi

有些事不是因为看到了希望才去坚持

1 声望

0 粉丝

0 条评论

得票时间

avatar

haozi

有些事不是因为看到了希望才去坚持

1 声望

0 粉丝

宣传栏

一起来看看我们需要些什么

项目结构

咱们可以从 Spring Initializer 获取项目框架。

项目依赖

<u>pom.xml</u>

<?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>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.1.6.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>com.eprogrammerz.examples.spring</groupId>

<artifactId>spring-boot-jsp</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>spring-boot-jsp</name>

<description>Example Spring Boot with JSP view</description>

<properties>

<java.version>1.8</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</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>

<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>

配置

  1. 启动类配置

SpringBootServletInitializer 按传统的 WAR包 部署方式来运行 SpringBootJspApplication
<u>SpringBootJspApplication.java</u>

package com.eprogrammerz.examples.spring.springbootjsp;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication

public class SpringBootJspApplication extends SpringBootServletInitializer {

@Override

protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

return application.sources(SpringBootJspApplication.class);

}

public static void main(String[] args) {

SpringApplication.run(SpringBootJspApplication.class, args);

}

}

  1. Resources

<u>application.properties</u>

spring.mvc.view.prefix: /WEB-INF/jsp/

spring.mvc.view.suffix: .jsp

Controller and View Template

  1. 写一个简单映射方法的Controller

package com.eprogrammerz.examples.spring.springbootjsp.controllers;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestParam;

@Controller

public class HelloController {

@GetMapping({"/", "/hello"})

public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {

model.addAttribute("name", name);

return "hello";

}

}

  1. 将下面内容保存成 JSP 文件,放在src/main/webapp/WEB-INF/jsp/目录

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Hello ${name}!</title>

</head>

<body>

<h2 class="hello-title">Hello ${name}!</h2>

</body>

</html>

使用Maven运行

在项目根路径下使用命令行运行程序。

mvn clean spring-boot:run

访问 localhost:8080 测试你的程序效果。

至此,使用 Spring Boot 2 展示 JSP 页面基础配置就完成,希望对大家有一定帮助。

提前㊗️大家新年新气象,2021年技术更上一个新台阶!

以上是 【Java】在 Spring Boot 2 中致敬 JSP 的全部内容, 来源链接: www.h5w3.com/114468.html

回到顶部