Struts2整合FreeMarker
1.jar重要的freemarker.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.4.jar
xwork-core-2.3.4.jar
2.web.xml中配置freemarker和struts2
<?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"> <servlet> <servlet-name>JspSupportServlet</servlet-name> <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3.Book.java
package com.sh.entity; public class Book { private String bookName; private String author; private double price; //get set }
4.action.java
package com.sh.action; import com.opensymphony.xwork2.ActionSupport; import com.sh.entity.Book; public class ShowBookAction extends ActionSupport { private Book book; public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } }
5.struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.i18n.encoding" value="utf-8"/> <package name="default" extends="struts-default" namespace="/books"> <action name="addBook"> <result name="success" type="freemarker">/templater/addBook.ftl</result> </action> <action name="showBook" class="com.sh.action.ShowBookAction"> <result name="success" type="freemarker">/templater/showBook.ftl</result> </action> </package> </struts>
6.webroot\templater
addBook.ftl
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"]/> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>addBook.ftl</title> </head> <body> <div> <h3>添加图书</h3> <hr/> <@s.form id="id" action="showBook"> <@s.textfield name="book.bookName" label="图书名称"/> <@s.textfield name="book.author" label="图书作者"/> <@s.textfield name="book.price" label="图书价格"/> <@s.submit value="提交"/> </@s.form> </div> </body> </html>
showBook.ftl
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"]/> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>showBook.ftl</title> </head> <body> <center> <div> <h3>图书信息</h3> <hr/> 图书名称:${book.bookName}<br/> 图书作者:${book.author}<br/> 图书价格:${book.price}<br/> </div> </center> </body> </html>
7.在web-int下添加struts-tags.tld(struts2-core-2.3.4.1.jar\META-INF)
8.如果ftl有乱码点击文件右键-->properties--textfileencoding
9.访问
http://localhost:8083/StrutsAndFreeMarker/books/addBook.action
相关推荐
hgzhang 2020-05-06
白净垃圾桶 2020-05-04
playis 2020-04-18
郭大路路 2020-04-16
lenchio 2020-04-08
yixiaoqi00 2020-04-08
melonjj 2020-01-05
mmywcoco 2019-12-28
wangruiling 2014-06-13
yaoyao0 2014-06-01
YangHuiLiang 2014-05-20
方志朋 2019-12-22
xcguoyu 2014-01-18
方志朋 2015-04-30
殷龙飞 2014-06-24
zmysna 2016-12-16
89264255 2011-04-23
wuddny的blog 2014-12-04