5个业务框架的简单使用案例

5个业务框架的简单使用案例:

  1. React

React 可以用于构建各种类型的前端应用,包括单页面应用、多页面应用、移动应用等等。以下是一个简单的 React 组件,用于展示一个带有按钮的计数器:

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
  }

  return (
    <div>
      <p>当前计数为:{count}</p>
      <button onClick={handleClick}>增加计数</button>
    </div>
  );
}

export default Counter;
  1. Vue

Vue 可以用于开发各种类型的 Web 应用程序,并且提供了许多有用的功能和工具。以下是一个基于 Vue CLI 创建的简单 TodoMVC 应用程序:

<template>
  <div class="todos">
    <header>
      <h1>Todo List</h1>
      <form @submit.prevent="addTodo">
        <input type="text" v-model="newTodo" placeholder="添加任务" />
      </form>
    </header>
    <ul>
      <li v-for="(todo, index) in todos" :key="todo.id">
        <input type="checkbox" v-model="todo.completed" />
        <span :class="{ completed: todo.completed }">{{todo.title}}</span>
        <button @click="removeTodo(index)">删除</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "TodoList",
  data() {
    return {
      todos: [
        { id: 1, title: "学习 Vue", completed: false },
        { id: 2, title: "构建 Todo 应用", completed: true },
        { id: 3, title: "学习 Vuex", completed: false },
      ],
      newTodo: "",
    };
  },
  methods: {
    addTodo() {
      if (this.newTodo.trim() !== "") {
        this.todos.push({
          id: Date.now(),
          title: this.newTodo,
          completed: false,
        });
        this.newTodo = "";
      }
    },
    removeTodo(index) {
      this.todos.splice(index, 1);
    },
  },
};
</script>
  1. Django

Django 可以用于开发各种类型的 Web 应用程序,包括社交网络、电子商务应用等等。以下是一个简单的 Django 视图,用于展示一个带有表单的登录页面:

from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login

def login_view(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return redirect('home')
        else:
            message = '无法登录,请检查用户名和密码。'
    else:
        message = ''
    return render(request, 'login.html', {'message': message})
  1. Ruby on Rails

Ruby on Rails 可以用于开发各种类型的 Web 应用程序,包括博客、论坛、新闻网站等等。以下是一个简单的 Ruby on Rails 控制器,用于展示一个带有文章列表的博客页面:

class PostsController < ApplicationController
  def index
    @posts = Post.all
  end

  def show
    @post = Post.find(params[:id])
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)
    if @post.save
      redirect_to posts_path, notice: '文章创建成功!'
    else
      render :new
    end
  end

  private

  def post_params
    params.require(:post).permit(:title, :content)
  end
end
  1. Spring

Spring 可以用于构建各种类型的企业应用程序,包括 CRM、ERP、管理系统等等。以下是一个基于 Spring Boot 的简单 RESTful API,用于展示一个带有用户信息的用户管理系统:

@RestController
@RequestMapping("/users")
public class UserController {

  @Autowired
  private UserRepository userRepository;

  @GetMapping("")
  public List<User> getAllUsers() {
    return userRepository.findAll();
  }

  @PostMapping("")
  public User createUser(@RequestBody User user) {
    return userRepository.save(user);
  }

  @GetMapping("/{id}")
  public User getUserById(@PathVariable Long id) {
    Optional<User> optionalUser = userRepository.findById(id);
    if (optionalUser.isPresent()) {
      return optionalUser.get();
    } else {
      throw new ResponseStatusException(HttpStatus.NOT_FOUND, "用户未找到");
    }
  }

  @PutMapping("/{id}")
  public User updateUserById(@PathVariable Long id, @RequestBody User user) {
    Optional<User> optionalUser = userRepository.findById(id);
    if (optionalUser.isPresent()) {
      User existingUser = optionalUser.get();
      existingUser.setName(user.getName());
      existingUser.setEmail(user.getEmail());
      return userRepository.save(existingUser);
    } else {
      throw new ResponseStatusException(HttpStatus.NOT_FOUND, "用户未找到");
    }
  }

  @DeleteMapping("/{id}")
  public void deleteUserById(@PathVariable Long id) {
    Optional<User> optionalUser = userRepository.findById(id);
    if (optionalUser.isPresent()) {
      userRepository.deleteById(id);
    } else {
      throw new ResponseStatusException(HttpStatus.NOT_FOUND, "用户未找到");
    }
  }
}

以上是几个业务框架的简单使用案例,可以帮助您了解每个框架的基本用法和语法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hhb_618

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

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

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

打赏作者

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

抵扣说明:

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

余额充值