18 Star 29 Fork 10

羊望 / carson-web-mvc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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方法可向页面传递参数

    @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将字符串响应到客户端

    @Controller
    public class DemoController extends CarsonActionController {
        public ContentResult contentResultDemo() {
            return Content("<p style="color:red">我是后台返回的ContentResult</p>");
        }
        ...
    }

3 返回JSON

URL:/demo/jsonResultDemo 使用JsonResult将java对象序列化成JSON并响应到客户端(使用jackson实现,支持Map和JavaBean)

    @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代码响应到客户端

    @Controller
    public class DemoController extends CarsonActionController {
        public JavaScriptResult javaScriptResultDemo() {
            return JavaScript("alert('我是后台返回的javaScript');");
        }
        ...
    }

5 URL重定向

URL:/demo/redirectResultDemo 使用RedirectResult重定向URL(302)

    @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数组,响应到客户端下载

    @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是参数

    @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 表单数据反序列化到对象

前台表单

    <form method="post" action="/demo/formDataDemo">
        <div>
            员工1
            <br/>
            姓名<input name="username">
            年龄<input type="number" name="age">
            <br/>
            员工2
            <br/>
            姓名<input name="user2.username">
            年龄<input type="number" name="user2.age">
        </div>
        <button id="btnFormDataDemo">点击测试</button>
    </form>

后台代码,通过getModel方法可将表单数据转为javaBean(使用jackson实现)

    @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

Check out sources

git clone https://gitee.com/tzjzcy/carson-web-mvc.git

License

The Carson-Web-MVC is released under MIT License.

MIT License Copyright (c) 2019 羊望 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于Spring MVC的轻量级扩展,借鉴了ASP.Net MVC及JFinal框架的思路,提供了一种更简洁的MVC路由模式。 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/tzjzcy/carson-web-mvc.git
git@gitee.com:tzjzcy/carson-web-mvc.git
tzjzcy
carson-web-mvc
carson-web-mvc
master

搜索帮助

14c37bed 8189591 565d56ea 8189591