目录
基本概念
很多页面再用themleaf,但也有些网站使用的JSP页面,这里在spring boot中配置jsp前端支持有如下逻辑。
添加Maven依赖,配置application.properties,增加jsp文件。
代码与实例
porn.xml如下:
这里要注意文件结构:
这里要看下application.properties:
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
spring.http.multipart.max-request-size=10Mb
spring.http.multipart.max-file-size=20Mb
这里要注意:
views后的 "/" 这个不能少,不然找不到;
error.jsp文件如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>error</title>
</head>
<body>
<h1>error咯!!!!!</h1>
</body>
</html>
contoller如下:
程序运行截图如下:
添加一个参数试试:
先在porn.xml添加一个依赖:
jsp要注解添加:
程序运行截图如下:
请求如下:
下面给出源码:
Test.java
package com.jsp.server.controller;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("base")
public class Test {
@GetMapping("/Test")
public String test(){
return "Hello World!";
}
@GetMapping("/error")
public String ret404(){
return "error";
}
@GetMapping("/para1")
public String retPara1(String name, ModelMap modelMap){
if(StringUtils.isBlank(name)) {
name = "hehe哒";
}
modelMap.put("name", name);
return "para1";
}
}
MainApplication.java
package com.jsp.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class MainApplication extends SpringBootServletInitializer {
public static void main(String[] args){
SpringApplication.run(MainApplication.class, args);
}
}
application.properties
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
spring.http.multipart.max-request-size=10Mb
spring.http.multipart.max-file-size=20Mb
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>error</title>
</head>
<body>
<h1>error咯!!!!!</h1>
</body>
</html>
para1.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>para1</title>
</head>
<body>
<h1>para1咯!!!!!</h1>
<br>
<h1>${name}</h1>
</body>
</html>