com.shengsiyuan.boot.controller->类->JspController.java
JspController.java的内容如下
package com.shengsiyuan.boot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.management.RuntimeErrorException;
import java.util.Date;
@Controller
public class JspController {
@GetMapping("/jsp")
public String jsp(Map<String, Object>model){
model.put("date", new Date());
model.put("message", "hello world");
return "result";
}
@RequestMapping("/jspError")
public String jspError(Map<String, Object> model){
throw new RuntimeErrorException("jspError")
}
}
WebApp也有一些规则,创建
src->main->webapp->WEB-INF->jsp->(两个文件result.jsp和error.jsp)
webapp->WEB-INF->jsp-error.jsp内容
<html>
<body>
error occured: ${status}, ${error}
</body>
</html>
webapp->WEB-INF->result.jsp内容
<html>
<body>
Date: ${date} <br>
Message: ${message}
</body>
</body>
</html>
com.shengsiyuan->controller->JspController.java
package com.shengsiyuan.boot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.management.RuntimeErrorException;
import java.util.Date;
import java.util.Map;
@Controller
public class JspController {
@GetMapping("/jsp")
public String jsp(Map<String, Object> model){
model.put("date", new Date());
model.put("message", "hello world");
return "result";
}
@RequestMapping("/jspError")
public String jspError(Map<String, Object> model){
throw new RuntimeException("Error condition!!!");
}
}
resources->application.yml文件内容
spring:
application:
name: mytest
mandatory-file-encoding: GBK
http:
encoding:
enabled: ture
charset: GBK
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
server:
Port: 9090
myConfig:
myObject:
myName: zhangsan
myAge: 20
logging:
level:
root: debug
file:
path: logs\mylog
build.gradle中文件内容
buildscript {
ext{
springBootVersion='2.2.5.RELEASE'
}
repositories {
maven { url 'https://plugins.gradle.org/m2/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
mavenCentral()
}
dependencies {
classpath(
"org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}",
"io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE"
)
}
}
apply {
plugin("java")
plugin("maven")
plugin("idea")
plugin("org.springframework.boot")
plugin("io.spring.dependency-management")
}
group 'com.shengsiyuan'
version '1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
maven { url 'https://plugins.gradle.org/m2/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
mavenCentral()
}
dependencies {
compile(
"org.springframework.boot:spring-boot-starter-web",
"org.springframework.boot:spring-boot-loader",
"javax.servlet:jstl",
"org.apache.tomcat.embed:tomcat-embed-jasper"
)
}
14 SpringBoot 整合JSP
最新推荐文章于 2024-07-18 21:33:50 发布