Idea创建一个JSP web项目

前言

一直在使用eclipse,对idea嗤之以鼻。前些日子换成了idea以后觉得太香了,但是idea和eclipse又有很多不通的地方。比如笔者把Web项目迁移过来就折腾了好多时日,这里就写出来总结一下。Idea版本2020.13。

创建项目

首先新建项目,File->New->Project
在这里插入图片描述
选择Spring Initializr和你的JDK下一步
在这里插入图片描述

选择war包和Java版本,这里一定要选择war,不然打不出来包的。
在这里插入图片描述
接着选开发组件,我这里选择Developer Tools,Spring Web,以及SQL里面的几个常用组件。其实如果就是个测试的话Spring Web就够了,而且还可以避免一个启动时数据源的问题,但是一般做web项目,谁不用数据库呢。下一步。
在这里插入图片描述
然后直接Finish
在这里插入图片描述
初始化好了以后就是这样子。
在这里插入图片描述

构建页面目录

Idea并没有给大家创建好各种目录,所以要手动创建。要整一个webapp目录,用来放jsp文件。注意这个目录要在main目录下。
在这里插入图片描述
最终笔者建立的目录如下,pages这个二级目录可有可无,只要配置的对了名字无所谓的。比如笔者叫pages,你也可以叫做jspfolder等等,或者直接放jsp文件也可以。但是webapp这个名字要敲对了。
在这里插入图片描述
然后里面还包含一个jsp页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
 this is a jsp测试页面
</body>
</html>

配置application.properties

#应用名称可有可无
spring.application.name=demo

#访问端口号,默认是8080
server.port=8000

#编码格式
server.tomcat.uri-encoding=utf-8

#配置前缀,默认的位置是src/main/webapp 这里可以更换,比如/pages/ 那么目录就变成了src/main/webapp/pages
spring.mvc.view.prefix=/pages/
#后缀
spring.mvc.view.suffix=.jsp;

#项目启动名 这个配置了以后 http://127.0.0.1:8000/name/index
#如果不配置http://127.0.0.1:8000/index
#server.servlet.context-path=/name

#配置数据库信息,如果不配置会报错,一会儿报什么错误再说,就算你没有数据库直接这样写也行。
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=name
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://10.111.11.111:3306/name?&characterEncoding=utf-8&useSSL=true

配置porm文件

即便生成了Web项目,要开发jsp项目也需要额外的包,给自动生成的porm添加jsp支持包。注意:如果没有这三个包,你每次进入页面都会变成下载这个页面而不是在浏览器解析出来内容。

        <!-- 1.servlet依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- 2.jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- 3.jsp支持 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

构建Controller验证

为了验证好不好用,我们构建一个DefaultController测试一下:

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/index")
public class DefaultController {
    
    
    @RequestMapping("")
    public String index() {
    
    
        return "index";
    }
}

路径如下:
在这里插入图片描述

启动SpringBoot

既然配置都已经做好了,那就找到main方法,执行。

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

看到如下说明启动完成了:

2020-08-04 09:48:26.597  INFO 99196 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-08-04 09:48:26.669  INFO 99196 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8000 (http) with context path ''
2020-08-04 09:48:26.680  INFO 99196 --- [  restartedMain] com.example.demo.DemoApplication         : Started DemoApplication in 4.801 seconds (JVM running for 6.383)
2020-08-04 09:48:30.860  INFO 99196 --- [nio-8000-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-08-04 09:48:30.861  INFO 99196 --- [nio-8000-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-08-04 09:48:30.868  INFO 99196 --- [nio-8000-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 7 ms

打开浏览器输入http://localhost:8000/index这里就显示出来了。

在这里插入图片描述
到此一个Idea下的SpringBoot-JSP Web项目的Demo就搭建完了。

数据源DataSource报错

如果没有数据库配置,你就会报下面的错误。

2020-08-04 09:57:02.109 ERROR 38684 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class

这是因为添加了数据库组件,autoconfig启动时要自动读取数据源配置,如果项目没有配置数据源,那就读取不到,肯定会报错。如果只用Web组件,这个问题一般就不会有。
如果暂时不想添加数据源配置,可以在启动类中添加排除数据源检测的配置(exclude = {DataSourceAutoConfiguration.class})如下:再启动就没问题了。

@SpringBootApplication(exclude = {
    
    DataSourceAutoConfiguration.class})
public class DemoApplication {
    
    

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

猜你喜欢

转载自blog.csdn.net/Smallc0de/article/details/107768468