项目开始前准备
此项目是在ssm进行,直接对SSM项目进行修改,因此略去其他步骤,只对修改的内容进行描述
- 首先,pom文件需要添加的内容有
<!--spring 多出来的依赖文件 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!--freemarker依赖文件 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker.version}</version>
</dependency>
- springmvc-servlet.xml需要添加的设置
<!-- freemarker的配置 -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="WEB-INF/views/" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="number_format">0.##########</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="classic_compatible">true</prop>
<prop key="template_exception_handler">ignore</prop>
</props>
</property>
</bean>
<!--视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property>
<property name="suffix" value=".html" />
<property name="contentType" value="text/html;charset=utf-8" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="order" value="1" />
</bean>
目前测试 没有对原有的jsp 视图解析器设置造成不兼容
JSP视图解析器原有配置为
<!-- 配置内部资源视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
先看一个demo
- 我们先定义一个entity
import java.util.Date;
public class SysUser {
private Integer userid;
private String usercode;
private String username;
private String password;
private Integer status;
private String phonenumber;
private String email;
private String creator;
private Date createtime;
private String description;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsercode() {
return usercode;
}
public void setUsercode(String usercode) {
this.usercode = usercode == null ? null : usercode.trim();
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber == null ? null : phonenumber.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator == null ? null : creator.trim();
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description == null ? null : description.trim();
}
}
· 然后定义一个Controller
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.xqchen.ssm.domain.SysUser;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Controller
public class DemoController {
@RequestMapping("/demo")
public ModelAndView demo() {
ModelAndView mv = new ModelAndView("demo");
mv.addObject("username", "demo ceshi");
mv.addObject("loglist", this.logList());
return mv;
}
public List<SysUser> logList() {
List<SysUser> list = new ArrayList<SysUser>();
for (int i = 0; i < 5; i++) {
SysUser sysUser = new SysUser();
sysUser.setCreatetime(new Date());
sysUser.setUsername("laobiao" + i);
list.add(sysUser);
}
return list;
}
}
以上 为了从简 我把数据库操作省略了 直接给对象赋值
我在ModelAndView 传了两个对象,分别是username
和loglist
我需要新建一个demo.html文件 通过Freemarker引擎取值
- demo.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
${username!}
<ul>
<#list loglist as loglist>
<li> ${loglist.username}</li>
<li>${loglist.createtime?string('yyyy-MM-dd')}</li>
</#list>
</ul>
</body>
</html>
我们可以从下图看到 我们取到了 username 单个值 以及loglist这个集合。
后面,我们再来讲解Freemarker的语法。