前几天为了写一个websocket的demo,需要搭建一个web项目,本以为很简单,结果遇到一些问题。网上的文章不是不全面,就是没提示重点,所以就有了这篇文章。
一、项目描述
后台 springboot
前端 jsp
二、pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!--使用内嵌tomcat时,不要加provided,否则会出现访问jsp变成下载文件的问题!-->
<!--<scope>provided</scope>-->
</dependency>
</dependencies>
<build>
<!--让src/main/webapp在编译的时候在resoureces路径下也生成webapp的文件-->
<resources>
<resource>
<directory>src/main/webapp</directory>
<!--注意此次必须要放在此目录下才能被访问到-->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
注意:(这里很重要!)
1. springboot起步依赖spring-boot-starter-parent的不同版本有差异,可能导致一些问题,可以尝试下其他版本。
2. 跳转到jsp一定要添加jstl依赖,否则出现跳转的时候,变成下载该页面。
3. 关于spring-boot-starter-tomcat依赖,分两种情况:
(1)使用外置tomcat部署
1. Application继承SpringBootServletInitializer
2. 排除spring-boot-starter-web中包含内嵌的tomcat容器 spring-boot-starter-tomcat
3. tomcat-embed-jasper依赖中scope必须是provided!否则会出现访问jsp变成下载文件的问题
4. spring-boot-starter-tomcat依赖中scope必须是provided!否则会出现访问jsp变成下载文件的问题
(2)使用内嵌tomcat部署
1. tomcat-embed-jasper依赖中scope不要加provided!否则会出现访问jsp变成下载文件的问题
2. spring-boot-starter-tomcat依赖不用加!
4. 必须加入build标签,将jsp拷贝到META-INF/resources文件夹下。
二、后台代码
package com.asiainfo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.asiainfo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
// http://127.0.0.1:8080/springboot-web/home
@RequestMapping(value = "/home")
public String goHome() {
return "index";
}
// http://127.0.0.1:8080/springboot-web/home/page
@RequestMapping(value = "/home/page")
@ResponseBody
public ModelAndView goHomePage() {
ModelAndView mv = new ModelAndView();
mv.addObject("name", "zhangsan");
mv.setViewName("index");
return mv;
}
}
三、配置文件
server.port=8080
server.servlet.context-path=/springboot-web
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
四、JSP文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>insert title here</title>
</head>
<body>
hi, ${name}!
</body>
</html>
到此,整个搭建过程已经讲完了。如果本教程帮助到了你,请你赏个赞吧,非常感谢!
附项目地址:https://download.csdn.net/download/kuibuzhiqianli/11613996