
- Python (使用Flask框架)lnkryy.cn
app.py
python
from flask import Flask, render_template, request
app = Flask(name)
假设的商品列表
products = [
{“id”: 1, “name”: “Product 1”, “price”: 100},
{“id”: 2, “name”: “Product 2”, “price”: 200},
# …
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
… 其他路由和逻辑 …
if name == ‘main’:
app.run(debug=True)
2. JavaScript (使用Node.js和Express框架)
server.js
javascript
const express = require(‘express’);
const app = express();
// 假设的商品列表
const products = [
{ id: 1, name: ‘Product 1’, price: 100 },
{ id: 2, name: ‘Product 2’, price: 200 },
// …
];
app.get(‘/’, (req, res) => {
res.render(‘index’, { products });
});
// … 其他路由和逻辑 …
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. Java (使用Spring Boot)
Controller.java
java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
@RestController
public class ProductController {
// 假设的商品列表
private static final List<Product> PRODUCTS = Arrays.asList(
new Product(1, "Product 1", 100),
new Product(2, "Product 2", 200),
// ...
);
@GetMapping("/")
public List<Product> getProducts() {
return PRODUCTS;
}
// 假设的Product类...
static class Product {
private int id;
private String name;
private int price;
// 构造器、getter和setter...
}
}
4. Ruby (使用Rails框架)
在Rails中,你会有多个文件和目录来组织你的代码,但以下是一个简化的示例。
app/controllers/products_controller.rb
ruby
class ProductsController < ApplicationController
def index
@products = [
{ id: 1, name: “Product 1”, price: 100 },
{ id: 2, name: “Product 2”, price: 200 },
# …
]
end
end
app/views/products/index.html.erb
erb
Products
- <% @products.each do |product| %>
- <%= product[:name] %> - <%= product[:price] %>
- <% end %>
- Python (使用Flask框架)
app.py
python
from flask import Flask, render_template, request
app = Flask(name)
假设的商品列表
products = [
{“id”: 1, “name”: “Product 1”, “price”: 100},
{“id”: 2, “name”: “Product 2”, “price”: 200},
# …
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
… 其他路由和逻辑 …
if name == ‘main’:
app.run(debug=True)
2. JavaScript (使用Node.js和Express框架)
server.js
javascript
const express = require(‘express’);
const app = express();
// 假设的商品列表
const products = [
{ id: 1, name: ‘Product 1’, price: 100 },
{ id: 2, name: ‘Product 2’, price: 200 },
// …
];
app.get(‘/’, (req, res) => {
res.render(‘index’, { products });
});
// … 其他路由和逻辑 …
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. Java (使用Spring Boot)
Controller.java
java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
@RestController
public class ProductController {
// 假设的商品列表
private static final List<Product> PRODUCTS = Arrays.asList(
new Product(1, "Product 1", 100),
new Product(2, "Product 2", 200),
// ...
);
@GetMapping("/")
public List<Product> getProducts() {
return PRODUCTS;
}
// 假设的Product类...
static class Product {
private int id;
private String name;
private int price;
// 构造器、getter和setter...
}
}
4. Ruby (使用Rails框架)
在Rails中,你会有多个文件和目录来组织你的代码,但以下是一个简化的示例。
app/controllers/products_controller.rb
ruby
class ProductsController < ApplicationController
def index
@products = [
{ id: 1, name: “Product 1”, price: 100 },
{ id: 2, name: “Product 2”, price: 200 },
# …
]
end
end
app/views/products/index.html.erb
erb
Products
- <% @products.each do |product| %>
- <%= product[:name] %> - <%= product[:price] %>
- <% end %>
740

被折叠的 条评论
为什么被折叠?



