freemarkerDEMO

struts.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<package name="struts" extends="struts-default">
		<action name="LoginAction" class="com.wansha.struts.action.LoginAction">
			<result name="success" type="freemarker">/news/news.ftl</result>
		</action>
	</package>
</struts>    


LoginAction.java

package com.wansha.struts.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.wansha.pojo.Student;

public class LoginAction extends ActionSupport {
	private String username;
	private String password;
	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;
	}
	@Override
	public void validate() {
		System.out.println(" validate invoked !!!");
	}
	@Override
	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Student student = new Student();
		student.setUsername("shasha");
		student.setPassword("123123");
		request.setAttribute("stu", student);
		request.getSession().setAttribute("ss", "hello world");
		List<String> list = new ArrayList<String>();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add("d");
		list.add("e");
		list.add("f");
		list.add("g");
		list.add("h");
		list.add("i");
		request.setAttribute("list",list);
		request.setAttribute("hello","peng");
		request.setAttribute("sharp",14.55555555f);
		request.setAttribute("date",new Date());
		return SUCCESS;
	}
}

new.ftl


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
freemarker<br />
username:${username}<br />
password:${password}<br />
-------------------------------------<br />
username:${stu.username}<br />
password:${stu.password}<br />

------------------------------------<br />

session:${Session.ss}<br />


-----------------------------------<br />
list:<#list list?reverse as a>
	<#if a="b">
	<#else>
		${a}
	</#if>

</#list><br />
------------------------------------<br />
list:<#list list[1..4] as a>
	<#if a="b">
	<#else>
		${a}
	</#if>

</#list><br />
------------------------------------<br />
list:first:  ${list?first} <br /> 

------------------------------------<br />

hello:${hello?exists?string("yes","no")}<br />

------------------------------------<br />

sharp:${(sharp>=15)?string("大于等于15","小于15")}<br />

------------------------------------<br />
sharp: ${sharp?int}
------------------------------------<br />
sharp: #{sharp;m1M3}
------------------------------------<br />
html:${"<table>"?html}<br />
------------------------------------<br />
直接调用方法:${stu.hello("xiaopeng")}
------------------------------------<br />
date:${date?string("yyyy-MM-dd HH:mm:ss")}
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值