# carson-web-mvc **Repository Path**: tzjzcy/carson-web-mvc ## Basic Information - **Project Name**: carson-web-mvc - **Description**: 基于Spring MVC的轻量级扩展,借鉴了ASP.Net MVC及JFinal框架的思路,提供了一种更简洁的MVC路由模式。 - **Primary Language**: Java - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 30 - **Forks**: 10 - **Created**: 2017-01-20 - **Last Updated**: 2024-04-08 ## Categories & Tags **Categories**: webframework **Tags**: None ## README ## Carson-Web-MVC Carson-Web-MVC是基于Spring MVC的轻量级扩展,借鉴了ASP.Net MVC及JFinal框架的思路,提供了一种更简洁的MVC路由模式。 该框架依赖于spring-webmvc和jackson。 - URL路由无配置,无注解,遵循COC原则,自动根据Controller类名及方法名映射 - 提供多种Result类型,返回字符串、JSON、文件都很简单 - 仅扩展了Spring MVC的路由功能,不影响Spring MVC本身的功能 - 支持最新的spring boot 2.x ## 如何使用 如何使用该框架? 下载代码,运行carson-web-mvc-bootdemo,即可看到使用案例,你可以自由的修改代码,做任何你需要的定制。 ## 案例演示 下载代码,运行carson-web-mvc-bootdemo,即可看到使用案例 ## 版本更新 - 2017-03-11 修改json组件为更为通用的jackson - 2019-09-04 升级jdk为1.8版本,增加spring boot的demo ## 使用指南 1. Spring配置文件添加: 2. 让你的Controller继承CarsonActionController ### 1 返回页面 URL:/demo/modelAndViewDemo ModelAndView即Spring MVC中的ModelAndView,通过addObject方法可向页面传递参数 ```java @Controller public class DemoController extends CarsonActionController { public ModelAndView modelAndViewDemo() { ModelAndView mv = new ModelAndView("demo/modelAndViewDemo"); mv.addObject("title", "Carson-Web-MVC演示"); return mv; } ... } ``` ### 2 返回字符串 URL:/demo/contentResultDemo 使用ContentResult将字符串响应到客户端 ```java @Controller public class DemoController extends CarsonActionController { public ContentResult contentResultDemo() { return Content("

我是后台返回的ContentResult

"); } ... } ``` ### 3 返回JSON URL:/demo/jsonResultDemo 使用JsonResult将java对象序列化成JSON并响应到客户端(使用jackson实现,支持Map和JavaBean) ```java @Controller public class DemoController extends CarsonActionController { public JsonResult jsonResultDemo() { User user=new User(); user.setUsername(getPara("username")); user.setAge(Integer.parseInt(getPara("age"))); user.setModifyDate(new Date()); return Json(user); } ... } ``` ### 4 返回JavaScript URL:/demo/javaScriptResultDemo 使用JavaScriptResult将js代码响应到客户端 ```java @Controller public class DemoController extends CarsonActionController { public JavaScriptResult javaScriptResultDemo() { return JavaScript("alert('我是后台返回的javaScript');"); } ... } ``` ### 5 URL重定向 URL:/demo/redirectResultDemo 使用RedirectResult重定向URL(302) ```java @Controller public class DemoController extends CarsonActionController { public RedirectResult redirectResultDemo() { return Redirect("http://www.baidu.com"); } ... } ``` ### 6 文件下载 URL:/demo/filePathResultDemo 使用FilePathResult,返回文件路径,响应到客户端下载 URL:/demo/fileStreamResultDemo 使用FileStreamResult,返回文件流,响应到客户端下载 URL:/demo/fileContentResultDemo 使用FileContentResult,返回文件内容byte数组,响应到客户端下载 ```java @Controller public class DemoController extends CarsonActionController { /** * 文件下载案例(文件路径) */ public FilePathResult filePathResultDemo() { String rootPath = getRequest().getServletContext().getRealPath("/"); String filePath = Paths.get(rootPath, "attachment", "testfile.zip").toString(); return File(filePath, "application/zip"); } /** * 文件下载案例(文件流) */ public FileStreamResult fileStreamResultDemo() throws FileNotFoundException { String rootPath = getRequest().getServletContext().getRealPath("/"); String filePath = Paths.get(rootPath, "attachment", "testfile.zip").toString(); FileInputStream fileInputStream = new FileInputStream(filePath); return File(fileInputStream, "application/zip", "testfile.zip"); } /** * 文件下载案例(文件内容byte数组) */ public FileContentResult fileContentResultDemo() throws IOException { String rootPath = getRequest().getServletContext().getRealPath("/"); String filePath = Paths.get(rootPath, "attachment", "testfile.zip").toString(); File file = new File(filePath); long len = file.length(); byte[] bytes = new byte[(int) len]; FileInputStream fileInputStream = new FileInputStream(file); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); int r = bufferedInputStream.read(bytes); bufferedInputStream.close(); return File(bytes, "application/zip", "testfile.zip"); } ... } ``` ### 7 URL传参数案例 URL:/demo/urlParaDemo/a/123 其中a和123是参数 ```java @Controller public class DemoController extends CarsonActionController { public ModelAndView urlParaDemo(){ String urlPara0=getPara(0);//value:a String urlPara1=getPara(1);//value:123 ModelAndView mv=new ModelAndView("demo/urlParaDemo"); mv.addObject("urlPara0",urlPara0); mv.addObject("urlPara1",urlPara1); return mv; } ... } ``` ### 8 表单数据反序列化到对象 前台表单 ```html
员工1
姓名 年龄
员工2
姓名 年龄
``` 后台代码,通过getModel方法可将表单数据转为javaBean(使用jackson实现) ```java @Controller public class DemoController extends CarsonActionController { public ModelAndView formDataDemo(){ User user = getModel(User.class); User user2 = getModel(User.class,"user2"); ModelAndView mv=new ModelAndView("demo/formDataDemo"); mv.addObject("user",user); mv.addObject("user2",user2); return mv; } ... } ``` ## Prerequisites [JDK 8 update 20 or later][JDK8 build] ## Check out sources `git clone https://gitee.com/tzjzcy/carson-web-mvc.git` ## License The Carson-Web-MVC is released under [MIT License][]. [JDK8 build]: http://www.oracle.com/technetwork/java/javase/downloads [MIT License]: https://gitee.com/tzjzcy/carson-web-mvc/blob/master/LICENSE