Spring项目中JSP文件的正确打开与访问方式
Spring是一个著名的框架,因为它支持View层的许多技术。Spring 支持的View层 技术 包括 JSP,Thymeleaf,Freemarker等。由于Thymeleaf的简单性 ,它作为SpringBoot的View层的默认支持,并由Spring Boot自动配置 。因此,如果为View层选择 JSP,则需要对其进行配置。
在本文中,我将向您展示如何使用Spring Boot创建Web应用程序 以及如何使用 JSP 显示数据。内容将在这篇文章中提到:
在本文中,我将向您展示如何使用Spring Boot创建Web应用程序 以及如何使用 JSP 显示数据。内容将在这篇文章中提到:
配置为将 JSP 用于视图层解释Controller&JSP的工作原理8.1 创建Spring Boot项目
在 Eclipse上 创建一个 Spring Boot 项目。
输入:
Name: SpringBootJSPGroup: me.laocatDescription: Spring Boot + JSPPackage: me.laocat.jspSpringBootJspApplication.java
package me.laocat.jsp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootJspApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootJspApplication.class, args);
}
}8.2 配置pom.xml
在 pom.xml文件中 配置JSP / Servlet所需的库 :
org.apache.tomcat.embed
tomcat-embed-jasper
javax.servlet
jstl
pom.xml文件 的完整内容 :
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
me.laocat
SpringBootJSP
0.0.1-SNAPSHOT
SpringBootJSP
Spring Boot and JSP
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.apache.tomcat.embed
tomcat-embed-jasper
javax.servlet
jstl
org.springframework.boot
spring-boot-maven-plugin
8.3 配置JSP视图
在 src / main目录中,创建一个 webapp / WEB-INF / jsp子目录,您的 JSP 文件将放置在此目录中 。
在下一步中,您需要配置以告知 Spring Boot 您将放置 JSP文件的位置。OK,打开 application.properties 文件并添加以下内容 性质 :
application.properties
# =============================================
# 视图解析
# =============================================
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp8.4 控制器和JSP
User.java
public class User {
private String userName;
private String passWord;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public User() {
}
public User(String userName, String passWord) {
super();
this.userName = userName;
this.passWord = passWord;
}
}UserController.java
package me.laocat.jsp.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import me.laocat.jsp.model.User;
@Controller
public class UserController {
private static List users = new ArrayList();
static {
users.add(new User("zhangsan","123"));
users.add(new User("lisi","456"));
}
@RequestMapping(value= {"/","/index"},method=RequestMethod.GET)
public String index(Model model){
String message = "Hello Spring Boot + JSP";
model.addAttribute("message", message);
return "index";
}
@RequestMapping(value= {"/userList"},method=RequestMethod.GET)
public String getUsers(Model model) {
model.addAttribute("users", users);
return "userList";
}
} index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
首页
欢迎,首页
${message}
User List
userList.jsp
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
用户列表
用户列表
用户名
密码
${user.userName}
${user.passWord}
8.5 运行应用程序
在 Eclipse上,右键单击项目,然后选择:
Run As/Spring Boot Apphttp://localhost:8080
声明:本文由"麦兜"发布,不代表"破站"立场,转载联系作者并注明出处:https://www.848586.cn/231562.html
