ITeye博客

struts使用freemarker的简单实例

prodream · 2010-04-21 22:23 · 编程语言 · Markdown

1.Bean

 

public class User {
	private int userId;
	private String userName;
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}

}

 

2. action

 

import com.opensymphony.xwork2.ActionSupport;

public class IndexAction extends ActionSupport{
	private User user=new User();

	public User getUser(){
		return user;
	}
	public String execute(){
		user.setUserId(1);
		user.setUserName("prodream");
		return "freemarker";
	}
}

 

 

3. struts.xml

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

<package name="test" namespace="/" extends="struts-default">
 <default-action-ref name="index"></default-action-ref>
 <action name="index">
   <result>index.jsp</result>
 </action>
 <action name="freemarker" class="IndexAction">
   <result type="freemarker" name="show">/index.ftl</result>
</action>

</struts>

 

 

4.index.ftl

 

<meta http-equiv="content-type" content="text/html; charset=utf8">
<center>你好 ${user.userName}!Your ID:${user.userId}</center>

 

 

5. 请求  http://localhost:8080/test/freemarker.action

 

    

你好 prodream!Your ID:1

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。