首页 >> 知识 >> spring

spring

使用标准的 maven-archetype-webapp 创建的工程目录

在java中整合模板引擎就不得不说 ModelAndView 对象,看字面意思就知道是模型和视图的对象,通过 ModelAndView 对象可将包含的数据对象与模板引擎进行绑定。通过 ModelAndView 对象的 addObject() 方法给视图设置需要返回的属性。

ModelAndView 对象默认使用请求转发跳转页面。如果要使用重定向跳转页面,则使用 new ModelAndView("redirect:/url")来跳转。

spring-mvc 整合 JSP

spring-mvc默认的模板引擎是JSP,可以看到在 application-context.xml 中什么配置都没有使用

项目结构

project│ pom.xml│ ├─.idea│ └─src └─main ├─java │ └─com │ └─cisbest │ └─controller │ JspController.java │ ├─resources │ application-context.xml │ └─webapp │ index.jsp │ └─WEB-INF web.xml

pom.xml 需要的依赖

4.0.0com.cisbesttemplate1.0-SNAPSHOTspring-mvc整合JSP、Thymeleaf、FreeMarker,http://www.cnblogs.com/cisbest/p/13503601.htmlwarspring-mvc整合JSP、Thymeleaf、FreeMarkerhttp://www.cnblogs.com/cisbest/p/13503601.html UTF-8 1.8 1.8 org.springframework spring-webmvc 5.1.5.RELEASE

JspController.java

package com.cisbest.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/cisbest")public class JspController { // 测试地址 http://localhost:8080/cisbest/index?name=是否乱码 @GetMapping("/index") public ModelAndView view(String name){ System.out.println("测试接受的数据是否乱码:user="+name); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/index.jsp"); modelAndView.addObject("name" , name); return modelAndView; }}

application-context.xml

index.jsp

Title响应数据=====>>>>> ${name}

web.xml

spring-mvc org.springframework.web.servlet.DispatcherServlet contextConfiglocations classpath:application-context.xml spring-mvc / CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 CharacterEncodingFilter /* spring-mvc 整合 FreeMarker

FreeMarker配置参考 http://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-view-freemarker

FreeMarker语法参考 http://freemarker.apache.org/docs/index.html

项目结构

project│ pom.xml│ ├─.idea│ └─src └─main ├─java │ └─com │ └─cisbest │ └─controller │ FreeMarkerController.java │ ├─resources │ application-context.xml │ └─webapp └─WEB-INF │ web.xml │ └─freeMarker index.ftl

pom.xml 需要的依赖

4.0.0com.cisbesttemplate1.0-SNAPSHOTspring-mvc整合JSP、Thymeleaf、FreeMarker,http://www.cnblogs.com/cisbest/p/13503601.htmlwarspring-mvc整合JSP、Thymeleaf、FreeMarkerhttp://www.cnblogs.com/cisbest/p/13503601.html UTF-8 1.8 1.8 org.springframework spring-webmvc 5.1.5.RELEASE org.springframework spring-context-support 5.1.5.RELEASE org.freemarker freemarker 2.3.28

FreeMarkerController.java

package com.cisbest.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/f")public class FreeMarkerController { // 测试地址:http://localhost:8080/f/index?name=貂蝉 @GetMapping("/index") public ModelAndView view(String name){ System.out.println("接受的数据:" + name); ModelAndView modelAndView = new ModelAndView("/index"); modelAndView.addObject("name",name); return modelAndView; }}

application-context.xml

UTF-8

web.xml

spring-mvc org.springframework.web.servlet.DispatcherServlet contextConfiglocations classpath:application-context.xml 0 spring-mvc / CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 CharacterEncodingFilter /*

index.ftl

Title

响应的数据>>>> ${name}

spring-mvc 整合 Thymeleaf

Thymeleaf配置参考 http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#integrating-thymeleaf-with-spring

项目结构

project│ pom.xml│ ├─.idea│ └─src └─main ├─java │ └─com │ └─cisbest │ └─controller │ ThymeleafController.java │ ├─resources │ application-context.xml │ └─webapp └─WEB-INF │ web.xml │ └─templates user.html

pom.xml

4.0.0com.cisbesttemplate1.0-SNAPSHOTspring-mvc整合JSP、Thymeleaf、FreeMarker,http://www.cnblogs.com/cisbest/p/13503601.htmlwarspring-mvc整合JSP、Thymeleaf、FreeMarkerhttp://www.cnblogs.com/cisbest/p/13503601.html UTF-8 1.8 1.8 org.springframework spring-webmvc 5.1.5.RELEASE org.thymeleaf thymeleaf-spring5 3.0.11.RELEASE

ThymeleafController.java

package com.cisbest.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/t")public class ThymeleafController { // 测试地址:http://localhost:8080/t/user?name=小乔 @GetMapping("/user") public ModelAndView view(String name){ System.out.println("接受的数据:" + name); ModelAndView modelAndView = new ModelAndView("/user"); modelAndView.addObject("name",name); return modelAndView; }}

application-context.xml

web.xml

spring-mvc org.springframework.web.servlet.DispatcherServlet contextConfiglocations classpath:application-context.xml spring-mvc / CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 CharacterEncodingFilter /*

user.html

Title响应的数据>>>>

参考汇总

spring-mvc参考 http://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-view

FreeMarker配置参考 http://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-view-freemarker

FreeMarker语法参考 http://freemarker.apache.org/docs/index.html

Thymeleaf配置参考 http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#integrating-thymeleaf-with-spring

网站地图