angularJs前后台交互,简单实现

前台代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>angularjs前后台交互</title>
    <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

</head>
<body>
<div ng-app="aaa" ng-controller="ccc">
    <table align="center" border="1px" cellpadding="0" cellspacing="0"
           style="width: 60%; height: 10%; text-align: center;">
        <thead>
        <td>id</td>
        <td>姓名</td>
        <td>密码</td>
        </thead>

        <tr ng-repeat="x in user">
        <td>{{ x.id }}</td>
        <td>{{ x.username }}</td>
        <td>{{ x.password }}</td>
        </tr>
    </table>



</div>
</body>
<script>
    var app=angular.module('aaa',[]);
    app.controller('ccc',function ($scope,$http) {
       /* $http({
            method: 'GET',
            url: 'http://localhost:8899/angular/user/find-by-all',
            /!*headers:{'Content-Type': 'application/json',
                "Access-Control-Allow-Origin": "*",
                'Accept': 'application/json'}*!/
        }).then(function successCallback(response) {
            // 请求成功执行代码

            $scope.user = response.data;
        }, function errorCallback(response) {

            debugger
            // 请求失败执行代码
            alert("请求失败")
        });*/
        $http.get("http://localhost:8899/angular/user/find-by-all").then( function (response) {
            $scope.user = response.data;
        })
    })
</script>
</html>

后台代码,仅供参考
controll层

package com.it.controll;

import com.it.entity.User;
import com.it.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @Author: machi
 * @Date:2019/2/22
 */
@Controller
@RequestMapping("/user")
public class UserController {
    private UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/find-by-all")
    @ResponseBody
    public List<User> findByAll() {
       /* List<User> list = userService.findByAll();
        for (User user : list) {
            System.out.println(user.getUsername());
        }*/
        return userService.findByAll();
    }


}

mapper层

package com.it.mapper;

import com.it.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author: machi
 * @Date:2019/2/21
 */
@Mapper
public interface UserMapper{
    /**
     *
     * @return
     */

    @Select("SELECT * from user")
    public List<User> findByAll();
}

serviceImpl层

package com.it.service.impl;

import com.it.entity.User;
import com.it.mapper.UserMapper;
import com.it.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: machi
 * @Date:2019/2/21
 */
@Service
public class UserServiceImpl implements UserService {

    private UserMapper userMapper;

 public UserServiceImpl(UserMapper userMapper){

     this.userMapper=userMapper;
 }

    @Override
    public List<User> findByAll() {
        System.out.println(userMapper);
        return userMapper.findByAll();
    }


}

service层

package com.it.service;

import com.it.entity.User;

import java.util.List;

/**
 * @Author: machi
 * @Date:2019/2/21
 */

public interface UserService {
    /**
     *
     * @return
     */
    public List<User> findByAll();
}

实体层

package com.it.entity;


import javax.persistence.*;
import java.io.Serializable;


/**
 * @Author: machi
 * @Date:2019/2/21
 */
@Entity
@Table(name = "user")
public class User implements Serializable {

    @GeneratedValue(strategy = GenerationType.AUTO)//主键生成策略
    @Id
    private int id;
    @Column(name = "username")
    private String username;
    @Column(name = "password")
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

application.yml 文件


server:
  port: 8081
  servlet:
    context-path: /angular
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root



静态页面一般就用nginx代理。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Flutter后台交互实现方法有以下几种: 1. 使用http库进行网络请求:Flutter中有多个http库可供选择,如Dio、http、flutter_http等。开发者可以利用这些库发送HTTP请求到后台服务器,获取响应数据并进行处理。在台,开发者可以使用这些库发送post、get等请求,后台收到请求后,进行处理并返回相应的结果。 2. 使用WebSocket进行实时通信:WebSocket是一种在单个TCP连接上进行全双工通信的协议,它可以在后台之间建立持久连接,实现实时通信。开发者可以在Flutter中使用web_socket库建立WebSocket连接,台可以通过WebSocket发送数据到后台服务器,并通过监听收到后台的实时响应和通知。 3. 使用平台通道进行原生方法调用:Flutter提供了Platform Channel机制,开发者可以通过与原生平台(如Android和iOS)进行通信,调用平台上的原生代码实现后台交互。开发者可以在Flutter中通过MethodChannel、EventChannel等通道发送方法调用请求和接收平台传递过来的数据。 4. 使用第三方插件或SDK:有些后台服务提供商提供了与Flutter集成的插件或SDK,开发者可以直接使用这些插件或SDK与后台进行交互。比如Firebase提供了Firebase SDK,可以用于Flutter应用与Firebase后台进行数据交互、推送通知等。 5. 使用其他网络协议:根据具体需求,还可以使用其他网络协议进行后台交互,比如使用TCP、UDP等协议。开发者可以根据项目的实际需求选择合适的网络协议进行后台交互

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mcxiaochi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值