SpringBoot project started, using the Eclipse project to create Springboot

Getting SpringBoot project (Eclipse create the project)

Recently the company project is not urgent, so there is plenty of time to learn new knowledge, recently discovered the Internet springBoot very fire, then try out their own project to build a springboot, will share out some of these doubts and solutions to facilitate subsequent viewing also facilitate new learners, reduce pain.
The first blog, please correct problem areas. (This tutorial requires you to dependency injection spring, springMVC, web developers have some knowledge)

About springBoot
the Spring official business for the convenience of developers to develop the code, rather than repeat the definition of a model of the configuration, thus speeding up the framework of the project to build.

Plug-in installation
because the official STS spring this plugin can be very convenient for the development springBoot project, so install STS plug.
Open Eclipse select Help / EclipseMarketspace open the plug-in market, STS enter search plug as shown:
Click Install plug-in
Click install to install, need to restart after the installation is complete, then we can create springboot project through the plugin

Create a project
in Eclipse File / New / Other input box to search sping you can see:
Write pictures described here
enter some information about the project:
Write pictures described here
Click Next:
Write pictures described here
Select springboot version, I chose 2.0.4. Select the web dependency, click finish, so we have a springboot been created:
Write pictures described here
the directory structure of the project has been on the drawing instructions, look at the dependence pom.xml file:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

Found two dependencies: spring-boot-starter-web , spring-boot-starter-test, to know that here in front of our project is to create a web selected here is automatically added dependence of the web, there is a default unit testing Additional.
Project created, let's test the success of the project running, spring does not officially recommend the use of JSP, thymeleaf as the default template, so the following page is a thymeleaf.
First of all need to join thymeleaf rely on pom.xml file

<!-- Thymeleaf组件 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

springBoot default annotations inlet scanning start and packet class is injected below the bean package, of course, the package can also be specified by annotating the inlet categories:

@ComponentScan(basePackages = {"com.yss.common"})

I'm not here to specify other packages, so this structure my bag:
Write pictures described here
New User.java entity class

public class User {
    private String name;
    private String sex;
    private String hobby;

    public User() {
        super();
    }
    public User(String name, String sex, String hobby) {
        super();
        this.name = name;
        this.sex = sex;
        this.hobby = hobby;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getHobby() {
        return hobby;
    }
    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
}

New UserController.java

@Controller
public class UserController {

    @RequestMapping(value="/user")
    public String index(Model model) {
        List<User> users = new ArrayList<User>();
        users.add(new User("皮皮虾", "男", "喜欢皮"));
        users.add(new User("光头强", "男", "砍树"));
        users.add(new User("佩奇", "男", "叫"));
        model.addAttribute("users",users);
        return "user";
    }
}

Here is the injection @Controller spring to become a controller, there is a comment in SpringBoot in fact, is @ResController @ResponseBody and @Controller combination, the method does not return all view controller returns only data, and the methods you Add on the same @ResponseBody.
SpringBoot return thymeleaf default template view, so here is the default method returns the user to look at user.html go templates, of course, JSP also can be configured, we can not configure, say springboot official is not recommended JSP, the code coupling is too high.

New user.html template folder file:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title >Title</title>
</head>
<body>
    <table>
        <tr>
            <td>姓名</td>
            <td>性别</td>
            <td>爱好</td>
            <td>性别</td>
        </tr>
        <tr th:each="user:${users}" th:object="${user}">
            <td th:text="${user.name}"></td>
            <td th:text="${user.sex}"></td>
            <td th:text="${user.hobby}"></td>
            <td th:text="*{sex}"></td>
        </tr>
    </table>
</body>
</html>

Note the need to add thymeleaf namespace, or can not use th: each and so on, this is thymeleaf grammar, interested students can study by themselves, use and EL, JSTL like.
Okay so the files are created.
Right-click the file springBoot entrance to start the project, so that we can through http: // localhost: 8080 / user visited:
Write pictures described here

Follow-up:
We can modify some of the default configuration file in springBoot, the current I modified some of them:

#项目访问根路径
server.servlet.context-path=/demo_sxh
#端口
server.port=8888
server.tomcat.uri-encoding=UTF-8

#thymeleaf缓存
spring.thymeleaf.cache=false    

#热编译  
spring.devtools.restart.enabled=true   
#热编译监听目录
spring.devtools.restart.additional-paths=src/main/java 

#国际化配置
spring.messages.basename=i18n/messages
spring.messages.encoding=UTF-8

#logging.level.com.bonc = info

#springmvc视图解析器
spring.mvc.view.prefix=/templates/
spring.mvc.view.suffix=.html

Follow-up will write some springBoot configuration internationalization, reads the configuration file, configuration shiro, myBatis, redis, and so some of the blog.
He himself was learning, encourage each other.

Original difficult - please indicate the source.

Released three original articles · won praise 5 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_35487047/article/details/82291400