Carson-Web-MVC是基于Spring MVC的轻量级扩展,借鉴了ASP.Net MVC及JFinal框架的思路,提供了一种更简洁的MVC路由模式。 该框架依赖于spring-webmvc和jackson。
如何使用该框架? 下载代码,运行carson-web-mvc-bootdemo,即可看到使用案例,你可以自由的修改代码,做任何你需要的定制。
下载代码,运行carson-web-mvc-bootdemo,即可看到使用案例
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;
}
...
}
URL:/demo/contentResultDemo 使用ContentResult将字符串响应到客户端
@Controller
public class DemoController extends CarsonActionController {
public ContentResult contentResultDemo() {
return Content("<p style="color:red">我是后台返回的ContentResult</p>");
}
...
}
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);
}
...
}
URL:/demo/javaScriptResultDemo 使用JavaScriptResult将js代码响应到客户端
@Controller
public class DemoController extends CarsonActionController {
public JavaScriptResult javaScriptResultDemo() {
return JavaScript("alert('我是后台返回的javaScript');");
}
...
}
URL:/demo/redirectResultDemo 使用RedirectResult重定向URL(302)
@Controller
public class DemoController extends CarsonActionController {
public RedirectResult redirectResultDemo() {
return Redirect("http://www.baidu.com");
}
...
}
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");
}
...
}
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;
}
...
}
前台表单
<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;
}
...
}
git clone https://gitee.com/tzjzcy/carson-web-mvc.git
The Carson-Web-MVC is released under MIT License.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型