SpringMVC view integration (1). springmvc view Integration
Spring provides first-class support for most view technologies that are likely to be used in Web MVC applications.
SpringMVC also supports multiple views, such as JSP, Velocity, and Freemarker. Configure rules using different request paths and map them to different view files. The view MODEL returned by the Controller is displayed on the page.
JSP
JSP is compiled into servlet and has complete java language capabilities. Therefore, domain logic and even data access logic can be easily encoded as JSP. The JSPs of the application are all placed in the war file's WEB-INF directory, and the customer cannot access it. That is, JSP displays the model generated by the Controller.
The previous blog wrote to access the JSP configuration under the WEB-INF directory.
Controller model code:
@RequestMapping("/add")public String addStudent(Student student,HttpServletRequestrequest){ studentBean.save(student); return"/list"; }
When configuring access to the JSP configuration file, you need to configure the attribute id as ViewResolver, which defines the View implementation class to be used and specifies the prefix and suffix before and after the logical name. Therefore, when DispatcherServlet requests the View name of the parser, the latter will return an object packaging JSP, JSP located in/WEB-INF/jsp/list. jsp.
Template Technology
Both Velocity and Freemarker Are Template technologies. They are purely text-based engines, both of which are widely used in various applications that generate text output.
Unlike JSp, templates are not compiled into Java classes, but interpreted by their respective template engines.
Advantage: separates the presentation layer from the business logic layer.
Template + Data Model = output. The template is only responsible for the presentation of data on the page and does not involve any logic code. All logics are processed by the data model.
You cannot easily break through the template language to write Java code.
Disadvantage: If the HTML page is not updated in time, the old data will also be generated after the template needs to be modified.
Configuration:
<! -- Velocity configuration start Liu ti --> <beanid = "velocityResolver" class = "org. springframework. web. servlet. view. velocity. velocityViewResolver "> <property name =" prefix "value =" "/> <property name =" suffix "value = ". vm "/> </bean> <! -- Velocity configure end Liu ti --> <! -- Freemarker configuration start Liu ti --> <beanid = "freemarkerResolver" class = "org. springframework. web. servlet. view. freemarker. freeMarkerViewResolver "> <property name =" prefix "value =" "/> <property name =" suffix "value = ". ftl "/> </bean> <! -- Freemarker configure end Liu ti -->
Summary:
SpringMVC supports multiple view layers flexibly. In addition to JSp, Velocity, and Freemarker templates, SpringMVC can also write its own view types. These views are similar in principle. Different configurations are used to display the desired results.
Next, let's continue with the specific implementation, so stay tuned!