Eclipse framework and integrated manually set up SpringBoot FreeMaker Detailed Tutorial

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq262593421/article/details/99624848

Project structure

1, first create a maven project

2, the configuration file pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jmxk</groupId>
  <artifactId>SpringBoot-Start</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  	
  	<!-- SpringBoot自动整合版本号信息 -->
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
	<parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>2.1.1.RELEASE</version>
	</parent>
  
  <dependencies>
    <!-- SpringMVC内部依赖整合 -->
	<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<!-- SpringBoot整合Fremarker -->
	<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker -->
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-freemarker</artifactId>
	</dependency>
	<!-- SpringBoot热部署 -->
	<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-devtools</artifactId>
	    <optional>true</optional>
	</dependency>
  	
  </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <!-- 自动重启 -->
        <configuration>
          <fork>true</fork>
          <!-- 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
        </configuration>
      </plugin>
    </plugins>
  </build>  
  
</project>

3. Create SpringBoot start the program

Note: springBoot Controller must be placed on top of the pack

package com.jmxk;

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

@SpringBootApplication
public class SpringBoot_Start {

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

4, create HTTP request connection class

package com.jmxk.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@RestController = @Conctroller + @ResponseBody
@RestController
public class HelloController {
	
	@RequestMapping("/helloIndex")
	public String HelloIndex() {
 		return "欢迎使用springBoot2.0!";
	}
}

5, start the program SpringBoot

Spring style control information icon appears and the program has been in operation, it said not being given the successful launch!


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2019-08-15 11:25:58.306  INFO 15224 --- [  restartedMain] com.jmxk.SpringBoot_Start                : Starting SpringBoot_Start on DESKTOP-A9VMB8N with PID 15224 (F:\Eclipse-WorkSpace\JavaEE\SpringBoot_Start\target\classes started by com in F:\Eclipse-WorkSpace\JavaEE\SpringBoot_Start)
2019-08-15 11:25:58.309  INFO 15224 --- [  restartedMain] com.jmxk.SpringBoot_Start                : No active profile set, falling back to default profiles: default
2019-08-15 11:25:58.357  INFO 15224 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-08-15 11:25:58.357  INFO 15224 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-08-15 11:25:59.419  INFO 15224 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-08-15 11:25:59.441  INFO 15224 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-08-15 11:25:59.441  INFO 15224 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2019-08-15 11:25:59.449  INFO 15224 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jdk1.8/bin/../jre/bin/server;C:/Program Files/Java/jdk1.8/bin/../jre/bin;C:/Program Files/Java/jdk1.8/bin/../jre/lib/amd64;C:\Python27\;C:\Python27\Scripts;C:\Python\Python37\Scripts\;C:\Python\Python37\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.8\bin;MYSQL_HOME%\bin;D:\Maven\apache-maven-3.6.0\bin;D:\Node\nodejs\;C:\ProgramData\chocolatey\bin;C:\Program Files\TortoiseGit\bin;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\TXsoft\Git\cmd;C:\Users\com\AppData\Local\Microsoft\WindowsApps;D:\Node\nodejs\node_global;;D:\JetBrains\IntelliJ IDEA 2019.1\bin;;D:\JetBrains\WebStorm 2019.1\bin;;C:\WINDOWS\system32;;.]
2019-08-15 11:25:59.522  INFO 15224 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-08-15 11:25:59.523  INFO 15224 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1165 ms
2019-08-15 11:25:59.725  INFO 15224 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-08-15 11:25:59.806  INFO 15224 --- [  restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index
2019-08-15 11:25:59.905  INFO 15224 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2019-08-15 11:25:59.954  INFO 15224 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-08-15 11:25:59.958  INFO 15224 --- [  restartedMain] com.jmxk.SpringBoot_Start                : Started SpringBoot_Start in 1.936 seconds (JVM running for 3.838)

6, the browser visit  http: // localhost: 8080 / helloIndex

springboot not need to enter the project name, the input format is the local IP address + port number request path +

Local IP address: localhost or 127.0.0.1

The default port number: 8080

Controller class path request annotations @RequestMapping ( "/ helloIndex") custom value

7, SpringBoot integration fremarker, add configuration information in pom.xml (previous pom.xml already has)

	<!-- SpringBoot整合Fremarker -->
	<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker -->
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-freemarker</artifactId>
	</dependency>

8, create a folder templates in resources, create a ftl file in the folder

 

Spring整合FreeMarker!

${map.id}
${map.name}
${map.sex}
${map.age}

9, a new class Controller

package com.jmxk.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

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

@Controller
public class FTLController {

	@RequestMapping("/index")
	public String index(Map<String, Object> map,HttpServletRequest request) {
		
		map.put("id", 1);
		map.put("name","李白");
		map.put("age",20);
		map.put("sex","男");
		request.setAttribute("map", map);
		
		return "index";
	}
}

10, start springboot, access address  http://127.0.0.1:8080/index

Summary: When learning new framework, be sure to manually set up, knocking the code manually, to understand the role of each code and configuration. Understand the underlying principles and framework source code more conducive to learning!

 

 

 

Guess you like

Origin blog.csdn.net/qq262593421/article/details/99624848
Recommended