随笔 - 434,  文章 - 0,  评论 - 463,  阅读 - 46万
<!-- servlet依赖. -->
<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
       
</dependency>
      <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>jstl</artifactId>
      </dependency>
<!-- tomcat的支持.-->
<dependency>
       <groupId>org.apache.tomcat.embed</groupId>
       <artifactId>tomcat-embed-jasper</artifactId>
        
</dependency>

添加以上配置,不然没法正确访问到jsp

 application.yml

spring:

  jpa:
    database: mysql
    hibernate:
      ddl-auto: update
      show-sql: true
  #资源目录  
  resources:
    static-locations: classpath:static/
    
  #视图解析器  
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp

这些配置主要是为了说明资源目录和视图的位置。

在src下新建一个com包,里面写一个启动类:

package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * SpringBoot启动类
 * @author Administrator
 *
 */
@SpringBootApplication(scanBasePackages = "com")
public class Application {

   public static void main(String[] args) {
       SpringApplication.run(Application.class, args);
   }

}

项目肯定需要一个欢迎页,我们需要这样的一个目录结构:

index.jsp里面的内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>

	恭喜,JSP整合成功!


</body>
</html>

页面有了,下一步就是如何访问这个页面,老规矩,创建Controller

package com.app.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {
	
	@RequestMapping("/")
	public ModelAndView index(ModelAndView mav){
		mav = new ModelAndView("index");
		return mav;
	}

}

默认访问的就是index.jsp

启动项目,访问 http://localhost

Jsp中获得项目根路径的方式 

 

1.可以使用EL表达式:${pageContext.request.contextPath}
这种方式是的取到pageContext对象,然后在得到HttpServletRequest对象,最后再拿到contextPath。

2.可以采用JSP的形式来表示:
<%=request.getContextPath()%>

3. jstl set自定义标签 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="basePath" value="${pageContext.request.contextPath}"></c:set>

上面3种方式都是可以的,我们以第三种方式为例。

更新index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="basePath" value="${pageContext.request.contextPath}"></c:set>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>

	恭喜,JSP整合成功! ${basePath}

</body>
</html>

修改application.yml

server:
  port: 80
  context-path: /app

意思就是修改项目根目录为app。

重启,访问 http://localhost/app

 

 

posted on   剽悍一小兔  阅读(28)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-01-16 基于SpringBoot打造在线教育系统(8)-- 二级分类新增

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示