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




	JspSupportServlet
	org.apache.struts2.views.JspSupportServlet
	1


	struts2
	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter


	struts2
	/*

  
    index.jsp
  




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



 
   
   
   		
   			/templater/addBook.ftl
   		
   		
   			/templater/showBook.ftl
   		
   



6.webroot\templater
  addBook.ftl
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"]/>


  
    addBook.ftl
  
  
  
    

添加图书


<@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="提交"/>


showBook.ftl
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"]/>


  
    showBook.ftl
  

	

图书信息


图书名称:${book.bookName}
图书作者:${book.author}
图书价格:${book.price}


7.在 web-int 下 添加  struts-tags.tld  (struts2-core-2.3.4.1.jar\META-INF)


8.如果 ftl 有乱码  点击文件 右键 -->properties  --text file encoding

9.访问
  http://localhost:8083/StrutsAndFreeMarker/books/addBook.action

你可能感兴趣的:(struts,freemarker)