Based on a simple login interface to achieve SpringBoot

1. Create a project SpringBoot

  • The first way: create a page in the official Web SpringBoot (address: https://start.spring.io ) interface is as follows:

springboot

Fill project, language, version SpringBoot, the project meta-information (group, Artifact), then enter the Dependencies in the "web", click on the search results "SpringBoot" plus sign in the back above the interface, and click the Generate-Ctrl downloads project files.

  • The second way: to create the command line (refer to Ma Ying-jeou of the book "SpringBoot programming ideas")

File using the Maven Archetype plug-in, first need to create a project folder, open a command line window, the command is as follows:

mvn archetype:generate -DgroupId=thinking-in-spring-boot -DartifactId=first-spring-boot-application -Dversion=1.0.0-SNAPSHOT -DinteractiveMode=false -Dpackage=thinking.in.spring.boot

Command interpreter:

archetype: generate goal plugin

groupId:your groupId

artifactId:your artifactId

version:your version

package:your package

interactiveMode When the parameter is false to build a non-interactive, which is often said that "silently"

Results are as follows:

startup

Of course, this should have a maven based on your local installation.

2, the project into the compiler

This is not simple, this is not a write.

3, edit POM.xml file, the introduction of the package needed for the project

There is also a trick, is to learn from Ma Ying-jeou came. Hold down the CTRL key is left click

spring-boot-starter-parent version number, as shown below:
1
进入spring-boot-starter-parent-2.2.1.RELEASE.pom,然后继续点击版本号,如下图所示
2
然后里面就可以看到各种依赖的包,例如mysql,CRTL+f 搜索你需要添加的依赖,可以知道支持的版本号。
这里需要注意下,如果引入的依赖版本之间有冲突,会报错。

4、配置访问路径和项目根路径

这里也有两种方式,具体如下:

  • 配置文件方式

在application.properties配置文件中加入配置

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
  • 代码编程方式

启动类加入如下代码:

@Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
*//** 设置视图路径的前缀 *//*
resolver.setPrefix("/WEB-INF/views/");
*//** 设置视图路径的后缀 *//*
resolver.setSuffix(".jsp");
return resolver;
}
这里面有一个问题,就是如果将resolver.setSuffix属性为.html,访问不到页面。如果要使用html要采用别的配置方式。
配置项目根路径:
在application.properties配置文件中加入配置
server.servlet.context-path=/你的根路径

5、编辑crontroller

编辑controller,将访问根目录的reuest跳转到具体的页面,代码如下:

@Controller
@RequestMapping("/")
public class IndexPageController {

@RequestMapping("/")
public String index(){
return "login";
}
}
这里有一个问题需要注意下,就是controller的映射地址应该在@RequestMapping里面配置,而不应该加在@Controller里面,我就是因为把映射地址写在@Controller里面报错了,错误如下:
a87e6514b209a9dced4433b2a756c3af

6、编辑登录界面

登录页面的位置看你的访问路径设置,如果是/WEB-INF/views/就在/WEB-INF/views/文件夹下面窗机看login.jsp,我的路径src\main\webapp\WEB-INF\views\login.jsp

7、启动类添加@SpringBootApplication注解

@SpringBootApplication注解有如下三个元注解

@Configuration

allow to register extra beans in the context or import additional configuration
classes

允许在上下文中注册额外的bean或导入额外的配置类

@ComponentScan

enable @Component scan on the package where the application is located (see the
best practices)

The guide in the package where the scanned object class

@EnableAutoConfiguration 

Enable automatic configuration mechanism of Spring Boot

8 to start the project, access the login screen

In the startup class blank right click and select Run, so that the project started for a while, open your browser and enter the access path http: // localhost: 8080 / You root path configuration

3

tomcat default port 8080 to publish a project, but can also be configured in port projects in application.properties.

9, several problems encountered

  • mysql driven obsolescence

When the project started being given the Registered driver with driverClassName = com.mysql.jdbc.Driver was not found, trying direct instantiation.

This is because the latest driver mysql has become com.mysql.cj.jdbc.Driver. Only need to modify mysql in the driver configuration can be application.properties

#spring.datasource.driver-class-name = com.mysql.jdbc.Driver

spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver

  • Include the configuration of official documents springboot project
https://spring.io/guides/gs/centralized-configuration/

Guess you like

Origin www.cnblogs.com/marshZe/p/11921627.html