View


The WebMVC module supports a variety of view technologies, including JSP, Freemarker, Velocity, Text, HTML, JSON, Binary, Forward, Redirect, HttpStatus, etc., and can also implement custom views through the IView interface extension;

Representation method of controller view
  • By returning the IView interface type;
  • Expressing a view type through a string;
  • If there is no return value or the return value is empty, the JspView view corresponding to the current RequestMapping path will be used;
View file path configuration

The base path of the controller view file must start and end with '/', the default value is /WEB-INF/templates/;

  ymp.configs.webmvc.base_view_path=/WEB-INF/templates/
View object operation example

The view file can omit the extension name, and the request parameters and content type can be directly set through the IView interface;

  // 通过View对象创建视图对象
  IView _view = View.jspView("/demo/test")
          .addAttribute("attr1", "value")
          .addAttribute("attr2", 2)
          .addHeader("head", "value")
          .setContentType(Type.ContentType.HTML.getContentType());

  // 直接创建视图对象
  _view = new JspView("/demo/test");

  // 下面三种方式的结果是一样的,使用请求路径对应的视图文件返回
  _view = View.jspView();
  _view = JspView.bind();
  _view = new JspView();
##View provided by WebMVC module
JspView: JSP view;

  View.jspView("/demo/test.jsp");
  // = "jsp:/demo/test"
FreemarkerView: Freemarker view;

  View.freemarkerView("/demo/test.ftl");
  // = "freemarker:/demo/test"
VelocityView: Velocity view;

  View.velocityView("/demo/test.vm");
  // = "velocity:/demo/test"
TextView: text view;

  View.textView("Hi, YMPer!");
  // = "text:Hi, YMPer!"
HtmlView: HTML file content view;

  View.htmlView("<p>Hi, YMPer!</p>");
  // = "html:<p>Hi, YMPer!</p>"
##JsonView: JSON view;

  // 直接传递对象
  User _user = new User();
  user.setId("...");
  ...
  View.jsonView(_user);

  // 传递JSON字符串
  View.jsonView("{id:\"...\", ...}");
  // = "json:{id:\"...\", ...}"
BinaryView: Binary data flow view;

  // 下载文件,并重新指定文件名称
  View.binaryView(new File("/temp/demo.txt"))
          .useAttachment("测试文本.txt");
  // = "binary:/temp/demo.txt:测试文本.txt"
If the file name is not specified , the response header will not contain "attachment;filename=xxx"

ForwardView: Request forwarding view;

  View.forwardView("/demo/test");
  // = "forward:/demo/test"
RedirectView: redirect view;

  View.redirectView("/demo/test");
  // = "redirect:/demo/test"
HttpStatusView: HTTP status view

  View.httpStatusView(404);
  // = "http_status:404"

  View.httpStatusView(500, "系统忙, 请稍后再试...");
  // = "http_status:500:系统忙, 请稍后再试..."
NullView: empty view;

  View.nullView();