TODAY WEB 轻量级 Web 框架开源项目

我要开发同款
匿名用户2019年11月25日
30阅读
开发技术Java
所属分类Web应用开发、Web框架
授权协议GPL

作品详情

TODAYWEB是一个轻量级Web框架

安装<dependency><groupId>cn.taketoday</groupId><artifactId>today-web</artifactId><version>2.3.6.RELEASE</version></dependency>MavenCentral案例DEMO文档Wiki使用说明

通过 @Controller @RestController 配置控制器

//@Controller@RestController@RequestMapping("/users")publicclassIndexController{}

配置请求

@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</>;}

自定义参数转换器

@ParameterConverterpublicclassDateConverterimplementsConverter<String,Date>{@OverridepublicDatedoConvert(Stringsource)throwsConversionException{...}}

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

<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEWeb-ConfigurationPUBLIC"-//TODAYBLOG//Web-ConfigurationDTD2.0//CN""https://taketoday.cn/framework/web/dtd/web-configuration-2.3.3.dtd"><Web-Configuration><controllerprefix="/error/"><actionresource="400"name="BadRequest"status="400"/><actionresource="403"name="Forbidden"status="403"/><actionresource="404"name="NotFound"status="404"/><actionresource="500"name="ServerIsBusy"status="500"/><actionresource="405"name="MethodNotAllowed"status="405"/></controller><controller><actionresource="redirect:https://pipe.b3log.org/blogs/Today"name="today-blog-pipe"/><actionresource="redirect:https://taketoday.cn"name="today"/><actionresource="redirect:https://github.com"name="github"/><actionresource="redirect:/login"name="login.do"/></controller><controllerclass="cn.taketoday.web.demo.controller.XMLController"name="xmlController"prefix="/xml/"><actionname="obj"method="obj"/><actionname="test"resource="test"method="test"/></controller></Web-Configuration>

登录实例

@ControllerpublicclassUserController{/*<controllerprefix="/WEB-INF/view/"suffix=".ftl"><actionresource="login"name="login"/><actionresource="register"name="register"/></controller>*///@GET("login")@RequestMapping(value="/login",method=RequestMethod.GET)publicStringlogin(){return"/login/login";//支持jsp,FreeMarker,Thymeleaf,自定义视图}@Logger("登录")//@POST("/login")//@RequestMapping(value="/login",method=RequestMethod.POST)@ActionMapping(value="/login",method=RequestMethod.POST)publicStringlogin(HttpSessionsession,RedirectModelredirectModel,@ValidUseruser,Errorserror){if(error.hasErrors()){System.err.println(error.getAllErrors());redirectModel.attribute("msg",error.getAllErrors().toString());return"redirect:/login";}Userlogin=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)publicFiledownload(Stringpath){returnnewFile(path);}@GET("/display")publicfinalBufferedImagedisplay(HttpServletResponseresponse)throwsIOException{response.setContentType("image/jpeg");returnImageIO.read(newFile("D:/taketoday.cn/webapps/upload/logo.png"));}@GET("captcha")publicfinalBufferedImagecaptcha(HttpServletRequestrequest)throwsIOException{BufferedImageimage=newBufferedImage(IMG_WIDTH,IMG_HEIGHT,BufferedImage.TYPE_INT_RGB);Graphicsgraphics=image.getGraphics();graphics.setColor(Color.WHITE);graphics.fillRect(0,0,IMG_WIDTH,IMG_HEIGHT);Graphics2Dgraphics2d=(Graphics2D)graphics;drawRandomNum(graphics2d,request);returnimage;}

文件上传,支持多文件

@RequestMapping(value={"/upload"},method=RequestMethod.POST)publicfinalStringupload(@MultipartMultipartFileuploadFile)throwsIOException{Stringupload="D:/www.yhj.com/webapps/upload/";Stringpath=upload+uploadFile.getFileName();Filefile=newFile(path);uploadFile.save(file);return"/upload/"+uploadFile.getFileName();}@POST({"/upload/multi"})publicfinalStringmultiUpload(HttpServletResponseresponse,@MultipartMultipartFile[]files)throwsIOException{Stringupload="D:/www.yhj.com/webapps/upload/";for(MultipartFilemultipartFile:files){Stringpath=upload+multipartFile.getFileName();Filefile=newFile(path);System.out.println(path);if(!multipartFile.save(file)){return"<script>alert('uploaderror!')</script>";//response.getWriter().print("<script>alert('uploaderror!')</script>");}}//response.getWriter().print("<script>alert('uploadsuccess!')</script>");return"<script>alert('uploadsuccess!')</script>";}
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论