struts2整合Freemarker
1.解压struts2-core-X.X.X.jar文件,把在META-INF文件夹下面的struts-tags.tld文件复制到WEB-INF文件夹下。
将freemark的jar导入到工程中
2.在web.xml文件中配置freemark同时启动JSPSupportServlet.代码如下:
<servlet> <servlet-name>freemarker</servlet-name> <servlet-class> freemarker.ext.servlet.FreemarkerServlet </servlet-class> <!--下面的配置freemarke的ftl文件的位置 --> <init-param> <param-name>TemplatePath</param-name> <param-value>/</param-value> </init-param> <!-- 是否和服务器(tommcat)一起启动。0为不。1为是--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>freemarker</servlet-name> <url-pattern>*.ftl</url-pattern> </servlet-mapping><servlet> <!-- define a JspSupportServlet Object --> <servlet-name>JspSupportServlet</servlet-name> <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class> <!-- setting JspSupportServlet auto start --> <load-on-startup>1</load-on-startup> </servlet>
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"] /><@s.form action="login"> <@s.textfield name="username" label="username"/> <@s.submit value="login" />/@s.form
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"] /><html><head><title>Login Form</title></head><body>please input username and password login<br><@s.form action="Login.action"> <@s.textfield name="username" label="username"/> <@s.password name="password" label="password" /> <@s.submit value="submit" /></@s.form></body></html>
<html><head><title>Success</title></haed><body>WelCome,${username},you logined!<br><a href="ShowInfo.action">Show Information</a></body></html>
<html><head><title>User Information</title><body><table border="1" width="360"><caption>User Information</caption><tr><td>username:</td><td>${username}</td></tr><tr><td>sex:</td><td>${sex}</td></tr><tr><td>age:</td><td>${age}</td></tr></table></body></html>