使用Spring boot,Thymeleaf,AngularJS从零开始构建一个新的Web应用程序-第1部分

在这一系列博客文章中,我们将使用以下技术堆栈构建完整的响应式Web应用程序:

1)弹簧靴
– Spring MVC网站
– Spring Data JPA –Spring安全 2)Thymeleaf用于服务器端模板 3)客户端MVC的Angular JS(包括带有Bower的JS依赖项管理) 4)在AWS / EBS / Heroku / Openshift上部署应用程序

我们将使用好的老手来开始。

步骤1:使用Thymeleaf的简单Hello world UI

1.)首先,让我们在本地文件系统中创建一个工作区以启动项目。

Anirudhs-MacBook-Pro:~ anirudh$ mkdir practice && cd practice

2.)打开您喜欢的IDE(eclipse / IDEA)并使用maven启动一个新项目(您也可以使用快速入门maven原型)。 提供您选择的组ID和人工ID。
我使用以下内容:

group id : com.practice
   artifact id : ecomm

3.)创建项目后,打开Pom.xml并在下面粘贴以下内容

<?xml version="1.0" encoding="UTF-8"?>
<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.practice</groupId>
    <artifactId>ecomm</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>

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

    <properties>
        <java.version>1.8</java.version>
    </properties>

</project>

4.)现在在您的包中添加SpringBoot的Application.class(位于com.practice)

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);
    }

}

5)添加一个控制器
使用名称控制器在com.practice内制作一个新包,并添加HomeController.Java

package com.practice.controller;

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

@Controller
public class HomeController {

    @RequestMapping("/home")
    public String home(){
        return "index";

    }
}

注意,注释不是“ @RestController”,而只是“ @Controller”,它是Spring MVC控制器,它返回一个视图。

6)最后,我们将制作一个视图HTML文件。 Thymeleaf是此处的模板库,用于生成该库。
将此index.html放在以下位置:src / main / resources / templates(Spring启动约定)

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>First Page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Hi, this is my first landing page using server side templating by Thymeleaf
</body>
</html>

7.)运行应用程序
转到控制台并转到主目录,该目录中有pom.xml文件并运行mvn clean package

Anirudhs-MacBook-Pro:practice anirudh$ mvn clean package

在构建项目之后,使用spring-boot:run运行

Anirudhs-MacBook-Pro:practice anirudh$ mvn spring-boot:run

现在转到http:// localhost:8080 / home并找到您的登录页面。

在下一个博客中,我们将添加更多功能,公开REST服务,并在我们的应用程序中添加介绍Angular JS。

翻译自: https://www.javacodegeeks.com/2016/04/build-new-web-application-scratch-using-spring-boot-thymeleaf-angularjs-part-1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值