MVC framework based on the full development of AIO architecture smarthttp
Wrote a "minimal complete MVC framework jdk comes httpserver based development", write articles on the MVC AIO
DEMO Start Time: 0.1s (should be considered less of it?)
smart-http is a relatively simple http server, the communication core uses the latest smart-socket version v1.4.6. But want to provide a friendly WEB service, you need a lot of other features.
By fusion with solon framework, MVC will be able to provide friendly service and AOP:
- solon shell is a framework to achieve the MVC, IOC, AOP, annotations, plug-in mechanism and so on. Put the shells on the smart-http, it will be able to provide mvc capabilities.
- smarthttp is adapted to smarthttp so that it can put the shell solon; also can enjoy the capabilities of other plug brought solon, such as session, json like.
- snack3 and provide json serialization support, but also small enough; one has adapted to the serialization framework solon.
- enjoy hard to find less than its template engine, and also fast people want to cry.
(A) Create a new empty project maven
(B) adding a reference maven
<dependencies>
<!-- 基于 smart-http 封装的 solon.boot -->
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon.boot.smarthttp</artifactId>
<version>1.0.4</version>
</dependency>
<!-- 一个小巧的JSON框架 -->
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon.serialization.snack3</artifactId>
<version>1.0.4</version>
</dependency>
<!-- enjoy模板引擎 -->
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon.view.enjoy</artifactId>
<version>1.0.4</version>
</dependency>
<!-- 静态文件支持 -->
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon.extend.staticfiles</artifactId>
<version>1.0.4</version>
</dependency>
</dependencies>
(C) add files
- java/webapp/controller/HelloworldController.java
- java/webapp/controller/HomeController.java
- java/webapp/model/UserModel.java
- java/webapp/widget/FooterTag.java
- java/webapp/AioApp.java
- resources/static/jinjin.htm
- resources / WEB-INF / view / helloworld.shtm (shtm, is adapted to enjoy the default extension)
- // no configuration
Additional information:
//资源路径说明(不用配置)
resources/application.properties(或 application.yml) 为应用配置文件
resources/static/ 为静态文件根目标
resources/WEB-INF/view/ 为视图文件根目标(支持多视图共存)
//模板调试模式(或加热加载模式):
启动参数添加:-deubg=1
(D) Code
- webapp/AioApp.java
public class AioApp {
public static void main(String[] args) {
XApp.start(AioApp.class, args);
}
}
- webapp/widget/FooterTag.java
@XBean("view:footer")
public class FooterTag extends Directive {
@Override
public void exec(Env env, Scope scope, Writer writer) {
StringBuffer sb = new StringBuffer();
sb.append("<footer>");
sb.append("我是自定义标签,FooterTag");
sb.append("</footer>");
try {
writer.write(sb.toString());
}catch (Exception ex){
ex.printStackTrace();
}
}
}
- webapp/model/UserModel.java
@Data
public class UserModel {
private long id;
private String name;
private int sex;
private String label;
}
- webapp/controller/HomeController.java
@XController
public class HomeController {
@XMapping(value = "/", produces = "text/html;charset=utf-8")
public String home(){
return "<a href='/helloworld'>/helloworld</a>";
}
}
- webapp/controller/HelloworldController.java
@XController
public class HelloworldController {
@XMapping("/helloworld")
public Object helloworld(){
UserModel m = new UserModel();
m.setId(10);
m.setName("刘之西东");
m.setSex(1);
ModelAndView vm = new ModelAndView("helloworld.shtm");
vm.put("title","demo");
vm.put("message","hello world!");
vm.put("m",m);
return vm;
}
}
- resources/WEB-INF/view/helloworld.shtm
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>#(title)</title>
</head>
<body>
<div>
#(m.name) : #(message) (我想<a href="/jinjin.htm">静静</a>)
</div>
#footer()
</body>
</html>
(E) run AioApp.main ()
- Open your browser: http: // localhost: 8080 /
- Browser output: / the HelloWorld