Python (Flask 后端)
python
from flask import Flask, render_template, request
app = Flask(name) filesmax.com
假设的商品列表
products = [
{‘id’: 1, ‘name’: ‘商品1’, ‘price’: 100},
{‘id’: 2, ‘name’: ‘商品2’, ‘price’: 200},
# …
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
… 其他路由和逻辑
if name == ‘main’:
app.run(debug=True)
JavaScript (Node.js 后端, 使用 Express)
javascript
const express = require(‘express’);
const app = express();
// 假设的商品列表
const products = [
{id: 1, name: ‘商品1’, price: 100},
{id: 2, name: ‘商品2’, price: 200},
// …
];
app.get(‘/’, (req, res) => {
res.render(‘index’, { products });
});
// … 其他路由和逻辑
app.listen(3000, () => console.log(‘Server started on port 3000’));
Java (Spring Boot 后端)
(这里只展示 Controller 部分)
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, "商品1", 100),
new Product(2, "商品2", 200)
// ...
);
@GetMapping("/")
public List<Product> getAllProducts() {
return products;
}
// ... 其他 Controller 方法和 DTO 定义
static class Product {
private int id;
private String name;
private double price;
// ... 构造函数、getter 和 setter
}
}
HTML/CSS/JavaScript (前端示例)
html
欢迎来到购物商城
- {% for product in products %}
- {{ product.name }} - 价格: {{ product.price }}
- {% endfor %}
Python (Flask 后端)
python
from flask import Flask, render_template, request
app = Flask(name)
假设的商品列表
products = [
{‘id’: 1, ‘name’: ‘商品1’, ‘price’: 100},
{‘id’: 2, ‘name’: ‘商品2’, ‘price’: 200},
# …
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
… 其他路由和逻辑
if name == ‘main’:
app.run(debug=True)
JavaScript (Node.js 后端, 使用 Express)
javascript
const express = require(‘express’);
const app = express();
// 假设的商品列表
const products = [
{id: 1, name: ‘商品1’, price: 100},
{id: 2, name: ‘商品2’, price: 200},
// …
];
app.get(‘/’, (req, res) => {
res.render(‘index’, { products });
});
// … 其他路由和逻辑
app.listen(3000, () => console.log(‘Server started on port 3000’));
Java (Spring Boot 后端)
(这里只展示 Controller 部分)
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, "商品1", 100),
new Product(2, "商品2", 200)
// ...
);
@GetMapping("/")
public List<Product> getAllProducts() {
return products;
}
// ... 其他 Controller 方法和 DTO 定义
static class Product {
private int id;
private String name;
private double price;
// ... 构造函数、getter 和 setter
}
}
HTML/CSS/JavaScript (前端示例)
html
欢迎来到购物商城
- {% for product in products %}
- {{ product.name }} - 价格: {{ product.price }}
- {% endfor %}