- Python (使用Flask框架)tcjmbj.cn
后端 (app.py):
python
from flask import Flask, render_template, request
app = Flask(name)
假设有一个商品列表
products = [
{‘id’: 1, ‘name’: ‘商品1’, ‘price’: 100},
# … 其他商品
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
… 其他路由和逻辑
if name == ‘main’:
app.run(debug=True)
前端 (templates/index.html,使用Jinja2模板引擎):
html
商品列表
- {% for product in products %}
- {{ product.name }} - 价格: {{ product.price }}
- {% endfor %}
javascript
const express = require(‘express’);
const app = express();
// 假设有一个商品列表
const products = [
{ id: 1, name: ‘商品1’, price: 100 },
// … 其他商品
];
app.get(‘/’, (req, res) => {
res.render(‘index’, { products });
});
// 使用Pug作为模板引擎(需要安装pug包)
app.set(‘view engine’, ‘pug’);
// … 其他路由和逻辑
app.listen(3000, () => console.log(‘Server started on port 3000’));
前端 (views/index.pug):
pug
html
head
title 购物商城
body
h1 商品列表
ul
for product in products
li #{product.name} - 价格: #{product.price}
3. Java (使用Spring Boot)
由于Spring Boot项目较为复杂,以下仅为Controller层的伪代码:
Controller:
java
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Arrays;
import java.util.List;
@Controller
public class ProductController {
// 假设有一个商品列表
private List<Product> products = Arrays.asList(
new Product(1, "商品1", 100),
// ... 其他商品
);
@GetMapping("/")
public String index(Model model) {
model.addAttribute("products", products);
return "index";
}
// ... 其他逻辑
// 假设的商品类
static class Product {
int id;
String name;
double price;
// 构造器、getter和setter等...
}
}
前端 (使用Thymeleaf模板引擎):
index.html (位于src/main/resources/templates/目录下):
html
商品列表
- - 价格:
- Python (使用Flask框架)
后端 (app.py):
python
from flask import Flask, render_template, request
app = Flask(name)
假设有一个商品列表
products = [
{‘id’: 1, ‘name’: ‘商品1’, ‘price’: 100},
# … 其他商品
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
… 其他路由和逻辑
if name == ‘main’:
app.run(debug=True)
前端 (templates/index.html,使用Jinja2模板引擎):
html
商品列表
- {% for product in products %}
- {{ product.name }} - 价格: {{ product.price }}
- {% endfor %}
javascript
const express = require(‘express’);
const app = express();
// 假设有一个商品列表
const products = [
{ id: 1, name: ‘商品1’, price: 100 },
// … 其他商品
];
app.get(‘/’, (req, res) => {
res.render(‘index’, { products });
});
// 使用Pug作为模板引擎(需要安装pug包)
app.set(‘view engine’, ‘pug’);
// … 其他路由和逻辑
app.listen(3000, () => console.log(‘Server started on port 3000’));
前端 (views/index.pug):
pug
html
head
title 购物商城
body
h1 商品列表
ul
for product in products
li #{product.name} - 价格: #{product.price}
3. Java (使用Spring Boot)
由于Spring Boot项目较为复杂,以下仅为Controller层的伪代码:
Controller:
java
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Arrays;
import java.util.List;
@Controller
public class ProductController {
// 假设有一个商品列表
private List<Product> products = Arrays.asList(
new Product(1, "商品1", 100),
// ... 其他商品
);
@GetMapping("/")
public String index(Model model) {
model.addAttribute("products", products);
return "index";
}
// ... 其他逻辑
// 假设的商品类
static class Product {
int id;
String name;
double price;
// 构造器、getter和setter等...
}
}
前端 (使用Thymeleaf模板引擎):
index.html (位于src/main/resources/templates/目录下):
html
商品列表
- - 价格: