javaweb编写mvc框架案例
以下是一个简单的JavaWeb MVC框架案例:
1. 创建一个名为"MVCExample"的JavaWeb项目。
2. 在项目中创建以下目录结构:
- src
- main
- java
- controller
- HomeController.java
- model
- User.java
- view
- HomeView.java
- Main.java
- webapp
- WEB-INF
- l
- views
- home.jsp
3. HomeController.java:
```java
package controller;
import model.User;
import view.HomeView;
public class HomeController {
public void index() {
User user = new User("John Doe", 25);
HomeView view = new HomeView(user);
der();
}
}
```
4. User.java:
```java
package model;
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and setters
}
```jsp和html哪个更好
5. HomeView.java:
```java
package view;
import model.User;
public class HomeView {
private User user;
public HomeView(User user) {
this.user = user;
}
public void render() {
System.out.println("Welcome, " + Name() + "!");
System.out.println("Age: " + Age());
}
}
```
6. Main.java:
```java
import controller.HomeController;
public class Main {
public static void main(String[] args) {
HomeController controller = new HomeController();
controller.index();
}
}
```
7. home.jsp:
```jsp
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome, ${user.name}!</h1>
<p>Age: ${user.age}</p>
</body>
</html>
```
8. l:
```xml
<web-app>
<servlet>
<servlet-name>HomeServlet</servlet-name>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论