我创建了一个简单的控制器类处理程序方法来运行jsp类型的文件。
我创造了一个TestController.java测试控制器文件有我的处理程序方法
package com.practice.myapp;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@RequestMapping("/")
@ResponseBody
public String handler(HttpServletRequest request) {
return "this is fun and awesome";
}
@RequestMapping("/home")
public String home(){
System.out.println("this is Home");
return "home";
}
}
应用程序属性-
spring.mvc.view.prefix= /views/
spring.mvc.view.suffix=.jsp
为了在控制器中使用jsp文件,我在src/main中创建了一个webapp文件夹,其中有一个包含jsp视图文件的文件夹视图。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%-- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%> --%>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello, world!</title>
</head>
<body>
<h1> this is head </h1>
</body>
</html>
我还添加了tomcat嵌入的jasper maven依赖项来运行我的jsp文件。
这是我POM的链接。
https://ide.geeksforgeeks.org/tryit.php/cOtzkizjYD
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.4)
2021-03-29 01:43:49.289 INFO 33924
2021-03-29 01:43:49.295 INFO 33924
2021-03-29 01:43:49.432 INFO 33924
2021-03-29 01:43:49.432 INFO 33924
2021-03-29 01:43:50.539 INFO 33924
2021-03-29 01:43:50.633 INFO 33924
2021-03-29 01:43:51.367 INFO 33924
2021-03-29 01:43:51.404 INFO 33924
2021-03-29 01:43:51.404 INFO 33924
2021-03-29 01:43:51.672 ERROR 33924