- Python (Flask 框架)tcjmbj.cn
Flask App (app.py):
python
from flask import Flask, render_template, request
app = Flask(name)
假设有一个简单的产品列表
products = [
{‘id’: 1, ‘name’: ‘Product 1’, ‘price’: 10.00},
# … 更多产品
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
… 其他路由和视图函数
if name == ‘main’:
app.run(debug=True)
HTML 模板 (templates/index.html):
html
Products
- {% for product in products %}
- {{ product.name }} - {{ product.price }}
- {% endfor %}
javascript
const express = require(‘express’);
const app = express();
// 假设有一个简单的产品列表
const products = [
{id: 1, name: ‘Product 1’, price: 10.00},
// … 更多产品
];
app.get(‘/’, (req, res) => {
res.render(‘index’, { products });
});
// … 其他路由和中间件
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(Server is running on port ${PORT}.
);
});
EJS 模板 (views/index.ejs):
ejs
Products
- <% products.forEach(function(product){ %>
- <%= product.name %> - <%= product.price %>
- <% }); %>
Controller (ProductController.java):
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, "Product 1", 10.00),
// ... 更多产品
);
@GetMapping("/")
public String index(Model model) {
model.addAttribute("products", products);
return "index";
}
// 简单的 Product 类和其他控制器/服务将在这里定义
}
Thymeleaf 模板 (src/main/resources/templates/index.html):
html
Products
- -
- Python (Flask 框架)
Flask App (app.py):
python
from flask import Flask, render_template, request
app = Flask(name)
假设有一个简单的产品列表
products = [
{‘id’: 1, ‘name’: ‘Product 1’, ‘price’: 10.00},
# … 更多产品
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
… 其他路由和视图函数
if name == ‘main’:
app.run(debug=True)
HTML 模板 (templates/index.html):
html
Products
- {% for product in products %}
- {{ product.name }} - {{ product.price }}
- {% endfor %}
javascript
const express = require(‘express’);
const app = express();
// 假设有一个简单的产品列表
const products = [
{id: 1, name: ‘Product 1’, price: 10.00},
// … 更多产品
];
app.get(‘/’, (req, res) => {
res.render(‘index’, { products });
});
// … 其他路由和中间件
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(Server is running on port ${PORT}.
);
});
EJS 模板 (views/index.ejs):
ejs
Products
- <% products.forEach(function(product){ %>
- <%= product.name %> - <%= product.price %>
- <% }); %>
Controller (ProductController.java):
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, "Product 1", 10.00),
// ... 更多产品
);
@GetMapping("/")
public String index(Model model) {
model.addAttribute("products", products);
return "index";
}
// 简单的 Product 类和其他控制器/服务将在这里定义
}
Thymeleaf 模板 (src/main/resources/templates/index.html):
html
Products
- -