【JAVA基础】springMVC 整合freemarker(maven)
当今最流行的java模板引擎,莫过于freemarker和velocity。这里我弄个springMVC整合freemark的例子分享给大家。
基本的就不说了,直接入正题。
第一步:pom文件的依赖包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.4.RELEASE</version> </dependency>第二步:web.xml配置
<servlet> <servlet-name>demo</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>demo</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>第三步:在WEN-INF下面 再建一个demo-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com"/> <!-- ... --> </beans>第四步:在java/main/resource目录下新增freemarker.properties配置文件
#u8BBEu7F6Eu6807u7B7Eu7C7Bu578BuFF1Asquare_bracket:[] auto_detect:[]<> tag_syntax=auto_detect #u6A21u7248u7F13u5B58u65F6u95F4uFF0Cu5355u4F4DuFF1Au79D2 template_update_delay=0 default_encoding=UTF-8 output_encoding=UTF-8 locale=zh_CN #u8BBEu7F6Eu6570u5B57u683Cu5F0F uFF0Cu9632u6B62u51FAu73B0 000.00 number_format=# #u53D8u91CFu4E3Au7A7Au65F6uFF0Cu4E0Du4F1Au62A5u9519 classic_compatible=true #auto_import="/WEB-INF/templates/index.ftl" as do第五步:在demo-servlet.xml中增加freemarker视图配置
<!-- 设置freeMarker的配置文件路径 --> <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:freemarker.properties"/> </bean> <!-- 配置freeMarker的模板路径 --> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="freemarkerSettings" ref="freemarkerConfiguration"/> <property name="templateLoaderPath"> <value>/WEB-INF/ftl/</value> </property> </bean> <!-- 配置freeMarker视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/> <property name="contentType" value="text/html; charset=utf-8"/> <property name="cache" value="true"/> </bean>第六部:controller
package com.testFreeMarker.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.RequestMapping; import com.testFreeMarker.model.User; @Controller public class FreemarkerController { @RequestMapping("/hi") public String sayHello(ModelMap map){ System.out.println("哈哈哈"); map.put("name", "测试一下这个freeMarker"); return "/hi.ftl"; } @RequestMapping("/user") public String helloUser(ModelMap modelMap) { User user1=new User(1, "罗宾", "123456"); User user2=new User(2, "老施", "123456"); User user3=new User(3, "老大", "123456"); User user4=new User(4, "老板", "123456"); List<User> list=new ArrayList<User>(); list.add(user1); list.add(user2); list.add(user3); list.add(user4); modelMap.addAttribute("userDo", list) ; return "/userList.ftl"; } }User
package com.testFreeMarker.model; public class User { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public User(int id, String username, String password) { super(); this.id = id; this.username = username; this.password = password; } }hi.ftl
<html> <body> <h1>say hello ${name}</h1><br/> ${(1 != 1)?string("yes", "no")} </body> </html>userList.ftl
<#setting classic_compatible=true> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>User List</title> <style type="text/css"> <!-- .STYLE1 { font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: 36px; color: #FF0000; } .STYLE13 {font-size: 24} .STYLE15 {font-family: Arial, Helvetica, sans-serif; font-size: 24px; } --> </style> </head> <body> <table width="1500" height="600" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="500" height="200"> </td> <td width="500" height="200" align="center" valign="middle"><div align="center"><span class="STYLE1">User List </span></div></td> <td width="500" height="200"> </td> </tr> <tr> <td width="500" height="200"> </td> <td width="500" height="200"><table width="500" height="200" border="1" cellpadding="0" cellspacing="0"> <tr> <td width="160" height="65" align="center" valign="middle"><span class="STYLE15">ID</span></td> <td width="160" height="65" align="center" valign="middle"><span class="STYLE15">Username</span></td> <td width="160" height="65" align="center" valign="middle"><span class="STYLE15">Password</span></td> </tr> <#list userDo as user> <tr> <td width="160" height="65" align="center" valign="middle"><span class="STYLE15">${user.id}</span></td> <td width="160" height="65" align="center" valign="middle"><span class="STYLE15">${user.username}</span></td> <td width="160" height="65" align="center" valign="middle"><span class="STYLE15">${user.password}</span></td> </tr> </#list> </table></td> <td width="500" height="200"> </td> </tr> <tr> <td width="500" height="200"> </td> <td width="500" height="200"> </td> <td width="500" height="200"> </td> </tr> </table> </body> </html>
最后看一下目录结构:
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了