轻量级 Web 框架 TODAY WEB

GPL
Java
跨平台
2019-11-25
TAKETODAY
TODAY WEB 正在参加 2019 年度最受欢迎开源中国软件评选,请投票支持!
TODAY WEB 在 2019 年度最受欢迎开源中国软件评选 中已获得 {{ projectVoteCount }} 票,请投票支持!
投票赢奖品
已投票

TODAY WEB 是一个轻量级 Web 框架

安装

<dependency>
    <groupId>cn.taketoday</groupId>
    <artifactId>today-web</artifactId>
    <version>2.3.6.RELEASE</version>
</dependency>

案例

文档

使用说明

通过 @Controller @RestController 配置控制器

//@Controller
@RestController
@RequestMapping("/users")
public class IndexController {
    
}

配置请求

@GET("index")
@POST("post")
@PUT("articles/{id}")
......
@RequestMapping("/users/{id}")
@RequestMapping(value = "/users/**", method = {RequestMethod.GET})
@RequestMapping(value = "/users/*.html", method = {RequestMethod.GET})
@RequestMapping(value = {"/index.action", "/index.do", "/index"}, method = RequestMethod.GET)
@Interceptor({LoginInterceptor.class, ...})
public (String|List<?>|Set<?>|Map<?>|void|File|Image|...) \\w+ (request, request, session,servletContext, str, int, long , byte, short, boolean, @Session("loginUser"), @Header("User-Agent"), @Cookie("JSESSIONID"), @PathVariable("id"), @RequestBody("users"), @Multipart("uploadFiles") MultipartFile[]) {
    service...
    return </>;
}

自定义参数转换器

@ParameterConverter 
public class DateConverter implements Converter<String, Date> {
    @Override
    public Date doConvert(String source) throws ConversionException {
        ...
    }
}

也可以通过xml文件配置简单视图,静态资源,自定义视图解析器,文件上传解析器,异常处理器,参数解析器

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Web-Configuration PUBLIC 
		"-//TODAY BLOG//Web - Configuration DTD 2.0//CN"
			"https://taketoday.cn/framework/web/dtd/web-configuration-2.3.3.dtd">

<Web-Configuration>

        <controller prefix="/error/">
        <action resource="400" name="BadRequest" status="400" />
        <action resource="403" name="Forbidden" status="403" />
        <action resource="404" name="NotFound" status="404" />
        <action resource="500" name="ServerIsBusy" status="500" />
        <action resource="405" name="MethodNotAllowed" status="405" />
    </controller>

    <controller>
        <action resource="redirect:http://pipe.b3log.org/blogs/Today" name="today-blog-pipe" />
        <action resource="redirect:https://taketoday.cn" name="today" />
        <action resource="redirect:https://github.com" name="github" />
        <action resource="redirect:/login" name="login.do" />
    </controller>

    <controller class="cn.taketoday.web.demo.controller.XMLController" name="xmlController" prefix="/xml/">
        <action name="obj" method="obj" />
        <action name="test" resource="test" method="test"/>
    </controller>

</Web-Configuration>

登录实例

@Controller
public class UserController {

/* 
    <controller prefix="/WEB-INF/view/" suffix=".ftl">
        <action resource="login" name="login" />
        <action resource="register" name="register" />
    </controller> */
    
    // @GET("login")
    @RequestMapping(value = "/login" , method = RequestMethod.GET)
    public String login() {
        return "/login/login";//支持jsp,FreeMarker,Thymeleaf,自定义视图
    }
    
    @Logger("登录")
    //@POST("/login")
    //@RequestMapping(value = "/login" , method = RequestMethod.POST)
    @ActionMapping(value = "/login", method = RequestMethod.POST)
    public String login(HttpSession session, RedirectModel redirectModel, @Valid User user, Errors error) {
    
        if (error.hasErrors()) {
            System.err.println(error.getAllErrors());
            redirectModel.attribute("msg", error.getAllErrors().toString());
            return "redirect:/login";
        }
    
        User login = userService.login(user);
        if (login == null) {
            redirectModel.attribute("userId", user.getUserId());
            redirectModel.attribute("msg", "登录失败");
            return "redirect:/login";
        }
        redirectModel.attribute("msg", "登录成功");
        session.setAttribute(USER_INFO, login);
        return "redirect:/user/info";
    }
    
}

文件下载,支持直接返回给浏览器图片

@RequestMapping(value = {"/download"}, method = RequestMethod.GET)
public File download(String path) {
    return new File(path);
}
@GET("/display")
public final BufferedImage display(HttpServletResponse response) throws IOException {
    response.setContentType("image/jpeg");
    return ImageIO.read(new File("D:/taketoday.cn/webapps/upload/logo.png"));
}

@GET("captcha")
public final BufferedImage captcha(HttpServletRequest request) throws IOException {
    BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics graphics = image.getGraphics();
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
    Graphics2D graphics2d = (Graphics2D) graphics;
    drawRandomNum(graphics2d, request);
    return image;
}

文件上传,支持多文件

@RequestMapping(value = { "/upload" }, method = RequestMethod.POST)
public final String upload(@Multipart MultipartFile uploadFile) throws IOException {

    String upload = "D:/www.yhj.com/webapps/upload/";
    String path = upload + uploadFile.getFileName();
    File file = new File(path);
    uploadFile.save(file);

    return "/upload/" + uploadFile.getFileName();
}

@POST({"/upload/multi"})
public final String multiUpload(HttpServletResponse response, @Multipart MultipartFile[] files) throws IOException {

    String upload = "D:/www.yhj.com/webapps/upload/";
    
    for (MultipartFile multipartFile : files) {
        String path = upload + multipartFile.getFileName();
        File file = new File(path);
        System.out.println(path);
        if (!multipartFile.save(file)) {
            return "<script>alert('upload error !')</script>";
            //response.getWriter().print("<script>alert('upload error !')</script>");
        }
    }
    //response.getWriter().print("<script>alert('upload success !')</script>");
    return "<script>alert('upload success !')</script>";
}
的码云指数为
超过 的项目
加载中

评论(8)

TGVvbmFyZA
TGVvbmFyZA
许可证限定 这框架注定不会被商用 但是作为Spring那套理论的demo 还是值得学习的
TAKETODAY
TAKETODAY 软件作者
是的,做这个本意是为了学习。
拾光TM
拾光TM
太像spring mvc了
TAKETODAY
TAKETODAY 软件作者
就是照着SpringMVC使用方式写的
yong230
yong230
这不是跟spring mvc一样嘛
TAKETODAY
TAKETODAY 软件作者
用法一样
yong230
yong230
看来作者已吃透了spring
TAKETODAY
TAKETODAY 软件作者
吃透是真的不敢当,Spring真心强大,只能说和当初比起来我进步了。

暂无资讯

暂无问答

大学写的一个 Java Web 框架

前言 大学刚毕业,专业是电子信息工程。大一开始学Java,准确的说是高三最后的几周开始的. 果然兴趣是最好的老师, 在大一下学期自己从前端到后台写了我的个人网站:TODAY BLOG 。 从注册域名...

07/25 21:19
14
0
Web application the big change is coming...

Probably never got chance to touch Big Data architecture in my works, but to think of it, still interesting... We had many ideas about computing at 60s, and they wasn't really g...

2015/11/02 20:05
55
0
angularjs 日期格式化

AngularJS是为了克服HTML在构建应用上的不足而设计的。HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了。所以我做了一些工作(你也可以觉得是小花招)来...

2016/04/10 19:41
120
1
WijmoJS 支持模板字符串常量

在V2019.0 Update2 的全新版本中,WijmoJS 支持了模板字符串常量。 模板字符串是ES2015 / ES6中引入的一个非常棒的JavaScript功能,它们允许您使用插入值来构建多行字符串,并使处理嵌入字符...

09/19 15:33
14
0
Spring with WebSocket

还没写完,但是大致有一个WebSocket的概念 有了WebSocket我们可以做很多东西了

2014/11/11 10:25
1K
1
Account Takeover Attacks: An Overview

It’s a fact of life: web applications are inherently insecure. To protect your corporate information assets, your network, and your customers, the most important thing you can ...

2018/05/29 15:48
6
0
十二星座运势接口代码示例分享

运势查询也是一种大众化的娱乐方式,星座运势接口方便查询十二星座的今日运势,下面分享的是今日或明日运势格式和本周或下周运势格式。

2015/09/28 11:27
169
0

没有更多内容

加载失败,请刷新页面

返回顶部
顶部