Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lihengming/potato-webmvc
:pencil:一个简单的Web MVC框架,寂寞消遣之作~
https://github.com/lihengming/potato-webmvc
java mvc web
Last synced: 2 months ago
JSON representation
:pencil:一个简单的Web MVC框架,寂寞消遣之作~
- Host: GitHub
- URL: https://github.com/lihengming/potato-webmvc
- Owner: lihengming
- License: mit
- Created: 2016-03-24T08:17:45.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-10-21T09:55:21.000Z (about 8 years ago)
- Last Synced: 2024-10-14T08:15:54.072Z (3 months ago)
- Topics: java, mvc, web
- Language: Java
- Homepage:
- Size: 41 KB
- Stars: 26
- Watchers: 5
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## 简介
Potato WebMVC 是用Java实现的一个简单的MVC框架,由于其简单性所以只俱备基本MVC框架的功能和以下特性~1. 约定优于配置,实现最简甚至零配置
2. Model表单、请求参数、依赖对象自动构造注入所请求的HandleMethod
3. 拦截器
4. JSP View 、JSON响应支持## 如何使用?
1.引入Maven 依赖
```xml
com.github.lihengming
potato-webmvc
1.0.1ch.qos.logback
logback-classic
1.1.7```
2.配置你的web.xml
```xmlDispatcherServlet
cn.potato.web.Dispatcher
basePackage
example.web.controller
viewPrefix
/WEB-INF/views/
viewSuffix
.jsp
DispatcherServlet
/
```3.创建Controller
```java
package example.web.controller;public class HomeController {
//index()映射路径 - > ‘/home’
public Result index(Result result){
result.setViewName("home");//视图
result.addData("name", "Potato");//模型
return result;
}
}
```4.创建JSP
Reference:WEB-INF/views/home.jsp
```java
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
This Home Page,Hello ${name}!
```
5.部署后访问它
请求:[http://localhost/home](http://localhost/home)
响应:This Home Page,Hello Potato!
## 使用其他特性
- 使用JSON响应支持构造REST API ,以User服务为例1.创建Model
```java
public class User {
private Long id;
private String username;
private Integer age;
//省略getter、setter方法
}
```
2.创建UserController
```java
package example.web.controller;
public class UserController {//模拟持久层
private Map repository = new HashMap();//映射路径add() - > ‘/user/add’
public boolean add(User user) {
repository.put(user.getId(), user);
return true;
}//映射路径add() - > ‘/user/list’
public Collection list() {
return repository.entrySet();
}//映射路径add() - > ‘/user/find’
public User find(Long id) {
return repository.get(id);
}}
```
3.使用AJAX调用API
```javascript
function add(){
var url = '/user/add';
var data = {
'user.id': 1,
'user.username': 'potato',
'user.age': '24'
};
$.post(url, data).done(function (success) {
console.log(success);});
}
function query(){
var url = '/user/find';
$.get(url, {id: 1}).done(function (result) {
console.log(result);
});
}
add();//console out:true
query();//console out:Object {age: 24, id: 1, username: "potato"}```
- 使用拦截器支持做统一的处理
1.创建拦截器Class并实现Interceptor接口
```java
package example.web.interceptor;
/**
* 示例拦截器,统计方法执行时间
*/
public class ExampleInterceptor implements Interceptor {
private final ThreadLocal threadLocal = new ThreadLocal();public void before(HttpServletRequest request, HttpServletResponse response, Method handleMethod) {
System.out.println("执行方法之前,方法名称:" + handleMethod.getName());
threadLocal.set(System.currentTimeMillis());
}public void after(HttpServletRequest request, HttpServletResponse response, Object result) {
long time = System.currentTimeMillis() - threadLocal.get();
System.out.println("方法执行之后,返回Result:" + result + ",耗时" + time + "毫秒");
}
}
```
2.在Controller使用@EnableIntercept注解启动拦截器
```java
@EnableIntercept(classes=ExampleInterceptor.class)
public class UserController {}
```
3.测试请求:[http://localhost/user/list](http://localhost/user/list)
查看控制台输出:
```shell
执行方法之前,方法名称:list
14:13:43.951 [http-bio-80-exec-9] DEBUG cn.potato.web.Dispatcher - Interceptor: invoke example.web.interceptor.ExampleInterceptor.before()
14:13:43.952 [http-bio-80-exec-9] DEBUG cn.potato.web.Dispatcher - ##------------Service Begin--------------##
14:13:43.952 [http-bio-80-exec-9] DEBUG cn.potato.web.Dispatcher - Request: [/user/list] --> [example.web.controller.UserController.list()]
14:13:43.952 [http-bio-80-exec-9] DEBUG cn.potato.web.Dispatcher - Handle Finish!
方法执行之后,返回Result:[1=example.model.User@6e3114f5],耗时1毫秒
14:13:43.952 [http-bio-80-exec-9] DEBUG cn.potato.web.Dispatcher - Interceptor: invoke example.web.interceptor.ExampleInterceptor.after()
14:13:43.952 [http-bio-80-exec-9] DEBUG cn.potato.web.Dispatcher - Response: [Type:JSON],[Data:[{"key":1,"value":{"age":24,"id":1,"username":"potato"}}]]
14:13:43.952 [http-bio-80-exec-9] DEBUG cn.potato.web.Dispatcher - ##------------Service End--------------##
```
## 使用建议
本框架仅限于娱乐,因为它可能只是一个简简单单的玩具轮子,并且随时可能会爆胎,所以并不建议你使用它上路~。