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>