FreeMarker & Struts2配置及实例

81314797 2011-02-28

Struts2应用中使用FreeMarker模板技术,只需要在struts.xml文件中进行简单配置即可。

1.要在FreeMarker模板使用标签库,只要四步,如下:

(1)解压struts2-core-X.X.X.jar文件,把在META-INF文件夹下面的struts-tags.tld文件复制到WEB-INF文件夹下。

(2)在web.xml文件中启动JSPSupportServlet.代码如下:

<servlet>

<!--defineaJspSupportServletObject-->

<servlet-name>JspSupportServlet</servlet-name>

<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>

<!--settingJspSupportServletautostart-->

<load-on-startup>1</load-on-startup>

</servlet>

(3)在FreeMarker模板中使用assign指令导入标签库。代码如下

<#assigns=JspTaglibs["/WEB-INF/struts-tags.tld"]/>注:这里我把struts-tags.tld放在WEB-INF下面

(4)现在我们可以在FreeMarker模板中使用标签了,示例代码如下:

<#assigns=JspTaglibs["/WEB-INF/struts-tags.tld"]/>

<@s.formaction="login">

<@s.textfieldname="username"label="username"/>

<@s.submitvalue="login"/>

</@s.form>

2.登录示例:

(1)在WEB-INF文件夹下面建立一个名为ftl的文件夹,其实就是FreeMarker容器,我们把所有的ftl文件都放在里面。

现在我们先在下面建立名字分别为login.ftl,welcome.ftl的文件。

login.ftl代码如下

<#assigns=JspTaglibs["/WEB-INF/struts-tags.tld"]/>

<html>

<head>

<title>LoginForm</title>

</head>

<body>

pleaseinputusernameandpasswordlogin<br>

<@s.formaction="Login.action">

<@s.textfieldname="username"label="username"/>

<@s.passwordname="password"label="password"/>

<@s.submitvalue="submit"/>

</@s.form>

</body>

</html>

welcome.ftl代码如下:

<html>

<head>

<title>Success</title>

</haed>

<body>

WelCome,${username},youlogined!<br>

<ahref="ShowInfo.action">ShowInformation</a>

</body>

</html>

(2)建立Action

packageAction;

importcom.struts2.sunny.SunnyAction;

publicclassLoginActionextendsActionSupport{

privateStringusername;

privateStringpassword;

publicvoidsetPassword(Stringpassword)

{

this.password=password;

}

publicStringgetPassword()

{

returnthis.password;

}

publicvoidsetUsername(Stringusername)

{

this.username=username;

}

publicStringgetUsername()

{

returnthis.username;

}

publicStringexecute()throwsException

{

if(username==null)return"error";

else

returnSUCCESS;

}

}

(3)配置文件struts.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<packagename="manager"namespace="/manager"extends="struts-default">

<actionname="*">

<resulttype="freemarker">/WEB-INF/ftl/{1}.ftl</result>

</action>

<actionname="Login"class="Action.LoginAction">

<resultname="error"type="freemarker">/WEB-INF/ftl/login.ftl</result>

<resultname="success"type="freemarker">/WEB-INF/ftl/welcome.ftl</result>

</action>

</package>

</struts>

(4)web.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="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">

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<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>

</web-app>

相关推荐

sunh / 0评论 2013-06-19