由于最近项目里面要求要使用freemarker技术来做展现层,所以在网上搜索了好多资料,基础知识是看了李刚原来写的那本《struts2权威指南》。一直想在网上找一个很基础的例子来入门,但是不知道是我的原因还是怎么的,找到的都不是很理想,而且网上基本上是一篇文章到处贴。所以郁闷之下,就自己慢慢摸索写出了一个入门的例子,可以作为自己的总结,也可以让更多的初学者快速入门。
闲话少述,看例子。
*****例子的整个目录结构如下图:
说明:1、struts-tags.tld文件是由struts2的jar中拷贝出来的。
2、工程全部使用utf-8编码。
关键的几个文件:
1、index.ftl:
- <#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"]/>
- <!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>freemarkerDemo</title>
- </head>
- <body>
- <@s.form action="users/users_login.action">
- ${"用户名"}:<@s.textfield name="username"/><br/>
- ${"密码"}:<@s.password name="password"/><br/>
- <@s.submit value="sign on"/>
- </@s.form>
- </body>
- </html>
2、profile.ftl:
- <#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"]/>
- <!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>freemarkerDemo</title>
- </head>
- <body>
- 姓名:${username}
- <br/>
- 密码:${password}
- <br/>
- 年龄:${userInfo.age}
- <br/>
- 登录时间:${userInfo.signinTime?string("yyyy-MM-dd HH:mm:ss")}
- <br/>
- QQ:${userInfo.qq}
- <br/>
- 地址:${userInfo.addr}
- <hr/>
- 我的书籍:
- <#list userInfo.books as book>
- ${book}
- </#list>
- </body>
- </html>
3、web.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <servlet>
- <servlet-name>JspSupportServlet</servlet-name>
- <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
4、sturts.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>
- <constant name="struts.ui.theme" value="simple" />
- <package name="users" extends="struts-default" namespace="/users">
- <action name="users_*" class="com.pop.project.myftl.test.action.UserAction" method="{1}">
- <result name="index" type="freemarker">/WEB-INF/ftl/index.ftl</result>
- <result name="profile" type="freemarker">/WEB-INF/ftl/profile.ftl</result>
- </action>
- </package>
- </struts>
5、UserAction:
- package com.pop.project.myftl.test.action;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import com.opensymphony.xwork2.ActionSupport;
- import com.pop.project.myftl.test.bean.UserInfo;
- public class UserAction extends ActionSupport {
- private static final long serialVersionUID = 1L;
- private String username;
- private String password;
- private UserInfo userInfo;
- /**
- * 首页
- * @return
- */
- public String index() {
- return "index";
- }
- /**
- * 登录
- * @return
- */
- public String login()
- {
- userInfo = new UserInfo() ;
- userInfo.setAddr("shaanxi baoji") ;
- userInfo.setAge(25) ;
- userInfo.setSigninTime(new Date()) ;
- userInfo.setQq("77276575") ;
- List<String> books = new ArrayList<String>() ;
- books.add("Thinking in Java") ;
- books.add("禅意花园") ;
- books.add("敏捷编程") ;
- books.add("代码大全") ;
- userInfo.setBooks(books) ;
- return "profile" ;
- }
- // setters and getters
- 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 UserInfo getUserInfo() {
- return userInfo;
- }
- public void setUserInfo(UserInfo userInfo) {
- this.userInfo = userInfo;
- }
- }
UserInfo类是一个有几个属性的javabean,这里就不用列了。
自己跑一下例子慢慢来吧。
运行结果: