博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC 4 配置返回JSP,和Freemarker视图
阅读量:4031 次
发布时间:2019-05-24

本文共 4830 字,大约阅读时间需要 16 分钟。

1.新建Dynamic Web Project。

2.导入Spring及Springmvc 4.0.6 相关库文件

如截图

3.配置web.xml

Freemarker_SpringMVC_example
index.html
index.jsp
spring
org.springframework.web.servlet.DispatcherServlet
1
spring
*.html
说明:配置了一个叫spring的Servlet,然后映射到DispatcherServlet,Spring会自动在WEB-INF目录下查找一个叫spring-servlet.xml的文件,用于配置Springmvc.通过web请求时,spring会把所有的请求拦截,这里配置成允许*.html后缀的请求路径。

4.配置springmvc-servlet.xml

org.springframework.web.servlet.view.InternalResourceView
/WEB-INF/ftl/
utf-8
3600
说明:springmvc允许多视图配置view。通过order来指定查找优先级。这里配置先扫描/WEB-INF/ftl下的Freemarker模板文件,如果没有找到再匹配/WEB-INF/view/下的jsp文件。

这里可参考一篇文章:

freemarker.properties文件用于配置Freemarker属性

datetime_format=yyyy-MM-dd HH:mm:ssdate_format=yyyy-MM-ddtime_format=HH:mm:ssnumber_format=0.######;boolean_format=true,false#auto_import="/WEB-INF/ftl/common/app.ftl" as appwhitespace_stripping=truedefault_encoding=UTF-8tag_syntax=square_bracketurl_escaping_charset=UTF-8 #enable the fault. replaced with "" when 'null' classic_compatible=true
5.在/WEB-INF/下新建ftl和content文件夹,并添加相应的users.ftl文件盒users.jsp文件

编辑users.ftl如下

ViralPatel.net - FreeMarker Spring MVC Hello World
Add User
Firstname:
Lastname:
<#list model["userList"] as user>
Firstname Lastname
${user.firstname} ${user.lastname}
编辑users.jsp如下

<%@ page language="java" contentType="text/html; charset=UTF-8"      pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> ViralPatel.net - FreeMarker Spring MVC Hello World
Add User
Firstname:
Lastname:
Firstname Lastname
${user.firstname} ${user.lastname}
model["userList"]
6.编写Controller控制类

在org.web.controller下新建UserController.java

package org.web.controller;import java.util.ArrayList;import java.util.List;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class UserController {	/**	 * Static list of users to simulate Database	 */	private static List
userList = new ArrayList
(); // Initialize the list with some data for index screen static { userList.add(new User("Bill", "Gates")); userList.add(new User("Steve", "Jobs")); userList.add(new User("Larry", "Page")); userList.add(new User("Sergey", "Brin")); userList.add(new User("Larry", "Ellison")); } /** * Saves the static list of users in model and renders it via freemarker * template. * * @param model * @return The index view (FTL) */ @RequestMapping(value = "/users", method = RequestMethod.GET) public String index(@ModelAttribute("model") ModelMap model) { model.addAttribute("userList", userList); return "users"; } /** * Add a new user into static user lists and display the same into FTL via * redirect * * @param user * @return Redirect to /index page to display user list */ @RequestMapping(value = "/add", method = RequestMethod.POST) public String add(@ModelAttribute("user") User user) { if (null != user && null != user.getFirstname() && null != user.getLastname() && !user.getFirstname().isEmpty() && !user.getLastname().isEmpty()) { synchronized (userList) { userList.add(user); } } return "redirect:users.html"; }}
7.部署到tomcat,访问localhost:8080/springmvcftl/index.html即可

你可能感兴趣的文章
opencv学习——在MFC中读取和显示图像
查看>>
retext出现Could not parse file contents, check if you have the necessary module installed解决方案
查看>>
pyQt不同窗体间的值传递(一)——对话框关闭时返回值给主窗口
查看>>
linux mint下使用外部SMTP(如网易yeah.net)发邮件
查看>>
北京联通华为光猫HG8346R破解改桥接
查看>>
python使用win32*模块模拟人工操作——城通网盘下载器(一)
查看>>
python append 与浅拷贝
查看>>
Matlab与CUDA C的混合编程配置出现的问题及解决方案
查看>>
2017阿里内推笔试题--算法工程师(运筹优化)
查看>>
python自动化工具之pywinauto(零)
查看>>
python自动化工具之pywinauto(四)——批量转换exe视频
查看>>
python一句话之利用文件对话框获取文件路径
查看>>
PaperDownloader——文献命名6起来
查看>>
PaperDownloader 1.5.1——更加人性化的文献下载命名解决方案
查看>>
如何将PaperDownloader下载的文献存放到任意位置
查看>>
C/C++中关于动态生成一维数组和二维数组的学习
查看>>
系统架构:Web应用架构的新趋势---前端和后端分离的一点想法
查看>>
JVM最简生存指南
查看>>
漂亮的代码,糟糕的行为——解决Java运行时的内存问题
查看>>
Java的对象驻留
查看>>