`
zhangjingqiang
  • 浏览: 71696 次
  • 性别: Icon_minigender_1
  • 来自: 东京
社区版块
存档分类
最新评论

导入Velocity

    博客分类:
  • Java
阅读更多

src\main\java\bookstore\action:

InitAction.java

package bookstore.action;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import bookstore.db.BookDB;
import bookstore.pbean.TBook;
import bookstore.vbean.VBook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class InitAction extends Action{
	
	public ActionForward execute(	ActionMapping mapping,
									ActionForm form,
									HttpServletRequest req,
									HttpServletResponse res ){
		VBook insertBook = null;
		TBook currentBook = null;
		
		List baList = new ArrayList();
		
		Iterator iter = BookDB.getBookListAll().iterator();
		
		while( iter.hasNext() ){
			currentBook = (TBook) iter.next();
			insertBook = new VBook( currentBook );
			
			baList.add( insertBook );
		}
		
		req.setAttribute( "BookAllList", baList );
		
		return( mapping.findForward( "bookstore" ) );
	}
}

CartAction.java

package bookstore.action;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.actions.DispatchAction;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import bookstore.db.BookDB;



public class CartAction extends DispatchAction{
	
	public ActionForward addToCart(	ActionMapping mapping,
									ActionForm form,
									HttpServletRequest req,
									HttpServletResponse res ){
		
		HttpSession session = req.getSession();
		session.removeAttribute( "CART" );
		
		AddToCartItems atci = (AddToCartItems)form;
		
		List<String> cart = Arrays.asList( atci.getSelecteditems() );
		
		session.setAttribute( "CART", cart );
		
		return( mapping.findForward( "continue" ) );
	}
	
	
	public ActionForward checkout(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse res) {
		HttpSession httpsession = req.getSession(false);
		List<String> cart = (List<String>) httpsession.getAttribute("CART");

		Session session = BookDB.getHibernateSession();

		Query priceQuery = session
				.createQuery("select sum( book.price ) from TBook book where book.isbn in ( :SELECTED_ITEMS )");
		priceQuery.setParameterList("SELECTED_ITEMS", cart.toArray());
		
		Long total = (Long)priceQuery.uniqueResult();

		req.setAttribute("TOTAL", total);

		return (mapping.findForward("tocheck"));
	}

}

CartVeloAction.java

package bookstore.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.actions.DispatchAction;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import bookstore.db.BookDB;
import bookstore.pbean.TBook;
import bookstore.vbean.VBook;
import bookstore.vbean.VCheckout;

public class CartVeloAction extends DispatchAction{
	
	public ActionForward addToCart(	ActionMapping mapping,
									ActionForm form,
									HttpServletRequest req,
									HttpServletResponse res ){

		HttpSession session = req.getSession();
		
		CartViewItems atci = (CartViewItems)form;
		
		String[] selectedItems = atci.getSelecteditems();
		List cart = Arrays.asList( selectedItems );
		
		session.setAttribute( "CART", cart );

		req.setAttribute( "BookAllList",createVBook( selectedItems ) );
		
		return( mapping.findForward( "continue" ) );
	}

	private List createVBook( String[] inSelectedItems ){
		VBook insertBook = null;
		TBook currentBook = null;
		
		List baList = new ArrayList();
		
		Iterator iter = BookDB.getBookListAll().iterator();
		
		while( iter.hasNext() ){
			currentBook = (TBook) iter.next();
			insertBook = new VBook( currentBook );
			
			for( int counter = 0; counter < inSelectedItems.length;
					counter++ ){
				if( insertBook.getIsbn()
							.equals( inSelectedItems[counter] ) ){
					insertBook.setSelected( true );
				}
			}
		
			baList.add( insertBook );
		}
		
		return( baList );
	}
	
	public ActionForward checkout(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse res) {

		HttpSession httpsession = req.getSession(false);
		List<String> cart = (List<String>) httpsession.getAttribute("CART");

		Session session = BookDB.getHibernateSession();

		Query priceQuery = session
				.createQuery("select sum( book.price ) from TBook book where book.isbn in ( :SELECTED_ITEMS )");
		priceQuery.setParameterList("SELECTED_ITEMS", cart.toArray());
		
		VCheckout vc = new VCheckout();
		vc.setTotal( ((Long)priceQuery.uniqueResult()).intValue() );
		List ba = createVCheckSelected( (String[])cart.toArray( new String[cart.size()] ) );
		vc.setSelecteditems( ba );
		
		req.setAttribute( "ItemsToBuy", vc );
		
		return (mapping.findForward("tocheck"));
	}

	private List createVCheckSelected(String[] inSelectedItems) {
		VBook insertBook = null;
		TBook currentBook = null;

		List baList = new ArrayList();

		for (int counter = 0; counter < inSelectedItems.length; counter++) {

			currentBook = BookDB.findBookByISBN( (String)inSelectedItems[counter]);
			insertBook = new VBook( currentBook );

			insertBook.setSelected(true);

			baList.add(insertBook);
		}
		return (baList);
	}
}

CartViewItems.java

package bookstore.action;

import org.apache.struts.action.ActionForm;


public class CartViewItems extends ActionForm{
	
	private String[] selecteditems = null;
	
	public CartViewItems(){}
	
	public String[] getSelecteditems(){
		return( this.selecteditems );
	}
	public void setSelecteditems( String[] inSelecteditems ){
		this.selecteditems = inSelecteditems;
	}
}

AddToCartItems.java

 

package bookstore.action;

import java.util.List;

import org.apache.struts.action.ActionForm;

import bookstore.db.BookDB;
import bookstore.pbean.TBook;


public class AddToCartItems extends ActionForm{
	
	private List<TBook> items = null;
	private String[] selecteditems = null;
	
	
	public AddToCartItems(){
		items = BookDB.getBookListAll();
	}
	
	public List<TBook> getItems(){
		return( this.items );
	}
	
	public void setItems( List<TBook> inItems ){
		this.items = inItems;
	}
	
	public String[] getSelecteditems(){
		return( this.selecteditems );
	}
	
	public void setSelecteditems( String[] inSelecteditems ){
		this.selecteditems = inSelecteditems;
	}
}

src\main\java\bookstore\db:

BookDB.java

package bookstore.db;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import bookstore.pbean.TBook;

public class BookDB{
	
	private static SessionFactory sf = null;
	
	public static List<TBook> getBookListAll(){
		Session session = BookDB.getHibernateSession();
		
		List<TBook> booklist = (List<TBook>)session.createQuery( "from TBook" ).list();
		
		return( booklist );
	}
	
	public static TBook findBookByISBN( String inISBN ){
		Session session = getHibernateSession();
		if( session == null || session.isOpen() == false ){
			session = getHibernateSession();
		}
		
		Query findquery = session.createQuery( "from TBook book where book.isbn like :ISBN" );
		findquery.setString( "ISBN", inISBN );
		
		TBook returnValue = (TBook)findquery.uniqueResult();
		
		return( returnValue );
	}
	
	public static Session getHibernateSession(){

		if( sf == null ){
			sf = (new AnnotationConfiguration())
							.configure( "../hibernate.cfg.xml" )
							.buildSessionFactory();
		}
		return( sf.openSession() );
	}
}

src\main\java\bookstore\pbean:

TBook.java

package bookstore.pbean;

// Generated 2009/01/30 0:28:39 by Hibernate Tools 3.2.1.GA

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import javax.persistence.GeneratedValue;
import org.hibernate.annotations.GenericGenerator;

/**
 * TBook generated by hbm2java
 */
@Entity
@Table(name = "t_book", schema = "public", uniqueConstraints = @UniqueConstraint(columnNames = "isbn"))
public class TBook implements java.io.Serializable {

	private int id;
	private String isbn;
	private String title;
	private String author;
	private String publisher;
	private int price;
	private Set<TOrderDetail> TOrderDetails = new HashSet<TOrderDetail>(0);

	public TBook() {
	}

	public TBook(int id, String isbn, String title, String author,
			String publisher, int price) {
		this.id = id;
		this.isbn = isbn;
		this.title = title;
		this.author = author;
		this.publisher = publisher;
		this.price = price;
	}

	public TBook(int id, String isbn, String title, String author,
			String publisher, int price, Set<TOrderDetail> TOrderDetails) {
		this.id = id;
		this.isbn = isbn;
		this.title = title;
		this.author = author;
		this.publisher = publisher;
		this.price = price;
		this.TOrderDetails = TOrderDetails;
	}

	@Id
	@GeneratedValue(generator="hibernate_sequence")
	@GenericGenerator(name="hibernate_sequence", strategy="native")
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "isbn", unique = true, nullable = false)
	public String getIsbn() {
		return this.isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	@Column(name = "title", nullable = false)
	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name = "author", nullable = false)
	public String getAuthor() {
		return this.author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	@Column(name = "publisher", nullable = false)
	public String getPublisher() {
		return this.publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	@Column(name = "price", nullable = false)
	public int getPrice() {
		return this.price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TBook")
	public Set<TOrderDetail> getTOrderDetails() {
		return this.TOrderDetails;
	}

	public void setTOrderDetails(Set<TOrderDetail> TOrderDetails) {
		this.TOrderDetails = TOrderDetails;
	}

}

TCustomer.java

package bookstore.pbean;

// Generated 2009/01/30 0:28:39 by Hibernate Tools 3.2.1.GA

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import javax.persistence.GeneratedValue;
import org.hibernate.annotations.GenericGenerator;

/**
 * TCustomer generated by hbm2java
 */
@Entity
@Table(name = "t_customer", schema = "public", uniqueConstraints = @UniqueConstraint(columnNames = "uid"))
public class TCustomer implements java.io.Serializable {

	private int id;
	private String uid;
	private String passwordmd5;
	private String name;
	private String email;
	private Set<TOrder> TOrders = new HashSet<TOrder>(0);

	public TCustomer() {
	}

	public TCustomer(int id, String uid, String passwordmd5, String name,
			String email) {
		this.id = id;
		this.uid = uid;
		this.passwordmd5 = passwordmd5;
		this.name = name;
		this.email = email;
	}

	public TCustomer(int id, String uid, String passwordmd5, String name,
			String email, Set<TOrder> TOrders) {
		this.id = id;
		this.uid = uid;
		this.passwordmd5 = passwordmd5;
		this.name = name;
		this.email = email;
		this.TOrders = TOrders;
	}

	@Id
	@GeneratedValue(generator="hibernate_sequence")
	@GenericGenerator(name="hibernate_sequence", strategy="native")
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "uid", unique = true, nullable = false)
	public String getUid() {
		return this.uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	@Column(name = "passwordmd5", nullable = false)
	public String getPasswordmd5() {
		return this.passwordmd5;
	}

	public void setPasswordmd5(String passwordmd5) {
		this.passwordmd5 = passwordmd5;
	}

	@Column(name = "name", nullable = false)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "email", nullable = false)
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TCustomer")
	public Set<TOrder> getTOrders() {
		return this.TOrders;
	}

	public void setTOrders(Set<TOrder> TOrders) {
		this.TOrders = TOrders;
	}

}

TOrder.java

package bookstore.pbean;

// Generated 2009/01/30 0:28:39 by Hibernate Tools 3.2.1.GA

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import javax.persistence.GeneratedValue;
import org.hibernate.annotations.GenericGenerator;

/**
 * TOrder generated by hbm2java
 */
@Entity
@Table(name = "t_order", schema = "public")
public class TOrder implements java.io.Serializable {

	private int id;
	private TCustomer TCustomer;
	private Date orderday;
	private Set<TOrderDetail> TOrderDetails = new HashSet<TOrderDetail>(0);

	public TOrder() {
	}

	public TOrder(int id, TCustomer TCustomer, Date orderday) {
		this.id = id;
		this.TCustomer = TCustomer;
		this.orderday = orderday;
	}

	public TOrder(int id, TCustomer TCustomer, Date orderday,
			Set<TOrderDetail> TOrderDetails) {
		this.id = id;
		this.TCustomer = TCustomer;
		this.orderday = orderday;
		this.TOrderDetails = TOrderDetails;
	}

	@Id
	@GeneratedValue(generator="hibernate_sequence")
	@GenericGenerator(name="hibernate_sequence", strategy="native")
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "customer_id_fk", nullable = false)
	public TCustomer getTCustomer() {
		return this.TCustomer;
	}

	public void setTCustomer(TCustomer TCustomer) {
		this.TCustomer = TCustomer;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "orderday", nullable = false, length = 35)
	public Date getOrderday() {
		return this.orderday;
	}

	public void setOrderday(Date orderday) {
		this.orderday = orderday;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TOrder")
	public Set<TOrderDetail> getTOrderDetails() {
		return this.TOrderDetails;
	}

	public void setTOrderDetails(Set<TOrderDetail> TOrderDetails) {
		this.TOrderDetails = TOrderDetails;
	}

}

TOrderDetail.java

package bookstore.pbean;

// Generated 2009/01/30 0:28:39 by Hibernate Tools 3.2.1.GA

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import javax.persistence.GeneratedValue;
import org.hibernate.annotations.GenericGenerator;

/**
 * TOrderDetail generated by hbm2java
 */
@Entity
@Table(name = "t_order_detail", schema = "public")
public class TOrderDetail implements java.io.Serializable {

	private int id;
	private TOrder TOrder;
	private TBook TBook;

	public TOrderDetail() {
	}

	public TOrderDetail(int id, TOrder TOrder, TBook TBook) {
		this.id = id;
		this.TOrder = TOrder;
		this.TBook = TBook;
	}

	@Id
	@GeneratedValue(generator="hibernate_sequence")
	@GenericGenerator(name="hibernate_sequence", strategy="native")
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "order_id_fk", nullable = false)
	public TOrder getTOrder() {
		return this.TOrder;
	}

	public void setTOrder(TOrder TOrder) {
		this.TOrder = TOrder;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "book_id_fk", nullable = false)
	public TBook getTBook() {
		return this.TBook;
	}

	public void setTBook(TBook TBook) {
		this.TBook = TBook;
	}

}

src\main\java\bookstore\vbean:

VBook.java

package bookstore.vbean;

import bookstore.pbean.TBook;

public class VBook{
	
	     private String isbn;
	     private String title;
	     private String author;
	     private String publisher;
	     private int price;
	     private boolean selected;

	    public VBook(){}
	    
	    public VBook( TBook book ){
	    	this.isbn = book.getIsbn();
	    	this.title = book.getTitle();
	    	this.author = book.getAuthor();
	    	this.publisher = book.getPublisher();
	    	this.price = book.getPrice();
	    	this.selected = false;
	    }
	    
	    public String getIsbn() {
	        return this.isbn;
	    }	    
	    public void setIsbn(String isbn) {
	        this.isbn = isbn;
	    }

	    public String getTitle() {
	        return this.title;
	    }
	    public void setTitle(String title) {
	        this.title = title;
	    }

	    public String getAuthor() {
	        return this.author;
	    }
	    public void setAuthor(String author) {
	        this.author = author;
	    }

	    public String getPublisher() {
	        return this.publisher;
	    }
	    public void setPublisher(String publisher) {
	        this.publisher = publisher;
	    }

	    public int getPrice() {
	        return this.price;
	    }
	    public void setPrice(int price) {
	        this.price = price;
	    }

		public boolean isSelected() {
			return selected;
		}
		public void setSelected(boolean selected) {
			this.selected = selected;
		}
}

VCheckout.java

package bookstore.vbean;

import java.util.List;


public class VCheckout{
	
	private int total;
	private List selecteditems;

	public int getTotal(){
		return( this.total );
	}
	public void setTotal( int inTotal ){
		this.total = inTotal;
	}
	
	public List getSelecteditems() {
		return selecteditems;
	}
	public void setSelecteditems(List inSelecteditems) {
		this.selecteditems = inSelecteditems;
	}
}

src\main\webapp\WEB-INF:

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">okada</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/BookStore</property>
        <property name="hibernate.connection.username">okada</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <mapping class="bookstore.pbean.TOrderDetail" />
        <mapping class="bookstore.pbean.TCustomer" />
        <mapping class="bookstore.pbean.TOrder" />
        <mapping class="bookstore.pbean.TBook" />
    </session-factory>
</hibernate-configuration>

struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
	"http://struts.apache.org/dtds/struts-config_1_3.dtd">
	
<struts-config>
	<form-beans>
		<form-bean	name="AddToCartActionName"
					type="bookstore.action.AddToCartItems" />
		<form-bean	name="CartViewItemsName"
					type="bookstore.action.CartViewItems" />
	</form-beans>
	<action-mappings>
		<action
			path="/cartAction"
			type="bookstore.action.CartAction"
			name="AddToCartActionName"
			parameter="cartaction"
			scope="request">
			<forward name="continue" path="/BookStore.jsp" />
			<forward name="tocheck" path="/Check.jsp" />
		</action>

		<action
			path="/cartAction2"
			type="bookstore.action.CartVeloAction"
			name="CartViewItemsName"
			parameter="cartaction"
			scope="request">
			<forward name="continue" path="/BookStore.vm" />
			<forward name="tocheck" path="/Check.vm" />
		</action>

		<action
			path="/Init"
			type="bookstore.action.InitAction"
			scope="request">
			<forward name="bookstore" path="/BookStore.vm" />
		</action>
	</action-mappings>

	<message-resources	parameter="ApplicationResources"
						key="org.apache.struts.action.MESSAGE" /> 
</struts-config>

toolbox.xml

<?xml version="1.0"?>
<toolbox>

  <tool>
     <key>math</key>
     <scope>application</scope>
     <class>org.apache.velocity.tools.generic.MathTool</class>
  </tool>

  <tool>
     <key>link</key>
     <scope>request</scope>
     <class>org.apache.velocity.tools.struts.StrutsLinkTool</class>
  </tool>

  <!-- ordinarily the SecureLinkTool would simply replace the StrutsLinkTool
  	if SSL Ext. is in use - in that case the key would be 'link' but it's
        'slink' here to distinguish between the two. -->
  <tool>
     <key>slink</key>
     <scope>request</scope>
     <class>org.apache.velocity.tools.struts.SecureLinkTool</class>
  </tool>

  <tool>
     <key>text</key>
     <scope>request</scope>
     <class>org.apache.velocity.tools.struts.MessageTool</class>
  </tool>

  <tool>
     <key>errors</key>
     <scope>request</scope>
     <class>org.apache.velocity.tools.struts.ErrorsTool</class>
  </tool>

  <tool>
     <key>messages</key>
     <scope>request</scope>
     <class>org.apache.velocity.tools.struts.ActionMessagesTool</class>
  </tool>

  <tool>
     <key>form</key>
     <scope>request</scope>
     <class>org.apache.velocity.tools.struts.FormTool</class>
  </tool>

  <tool>
     <key>tiles</key>
     <scope>request</scope>
     <class>org.apache.velocity.tools.struts.TilesTool</class>
  </tool>

  <tool>
     <key>validator</key>
     <scope>request</scope>
     <class>org.apache.velocity.tools.struts.ValidatorTool</class>
  </tool>

</toolbox>

velocity.properties

default.contentType=text/html; charset=Windows-31J
input.encoding=Windows-31J
output.encoding=Windows-31J

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Chap13 Application</display-name>

  	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>
 
		<init-param>
			<param-name>debug</param-name>
			<param-value>2</param-value>
		</init-param>

		<init-param>
			<param-name>detail</param-name>
			<param-value>2</param-value>
		</init-param>

		<load-on-startup>2</load-on-startup>
	</servlet>
	
	<servlet>
		<servlet-name>velocity</servlet-name>
		<servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
		<init-param>
			<param-name>org.apache.velocity.toolbox</param-name>
			<param-value>/WEB-INF/toolbox.xml</param-value>
		</init-param>
		<init-param>
			<param-name>org.apache.velocity.properties</param-name>
			<param-value>/WEB-INF/velocity.properties</param-value>
		</init-param>
		<load-on-startup>10</load-on-startup>
	</servlet>	
	
 	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
 
 	<servlet-mapping>
		<servlet-name>velocity</servlet-name>
    	<url-pattern>*.vm</url-pattern>
	</servlet-mapping>
</web-app>

src\main\webapp:

BookStore.html

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=Windows-31J">
</head>

<body>
	<center>
	<h2>商品一覧</h2>
	</center>

	<form name="AddToCartActionName" method="post"
					action="$link.setAction( "/cartAction2" )">
 
		<table border="1">
    	#foreach ( $item in $BookAllList )

			<tr>
			<th rowspan="2">
			
				<input type="checkbox" name="selecteditems" value="$item.isbn"
										#if( $item.selected ) checked #end > 
			</th>
			<td colspan="3">
				$item.title
			</td>
			</tr>
			<tr>
			<td>
				$item.author
			</td>
			<td>
				$item.publisher
			</td>
			<td>
				$item.price 円
			</td>
			</tr>
    		#end
			</table>	
			<br>

		<input type="submit" name="cartaction" value="addToCart">
		<input type="submit" name="cartaction" value="checkout">
	</form>

</body>

</html>

BookStore.vm

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=Windows-31J">
</head>

<body>
	<center>
	<h2>商品一覧</h2>
	</center>

	<form name="AddToCartActionName" method="post"
					action="$link.setAction( "/cartAction2" )">
 
		<table border="1">
    	#foreach ( $item in $BookAllList )

			<tr>
			<th rowspan="2">
			
				<input type="checkbox" name="selecteditems" value="$item.isbn"
										#if( $item.selected ) checked #end > 
			</th>
			<td colspan="3">
				$item.title
			</td>
			</tr>
			<tr>
			<td>
				$item.author
			</td>
			<td>
				$item.publisher
			</td>
			<td>
				$item.price 円
			</td>
			</tr>
    		#end
			</table>	
			<br>

		<input type="submit" name="cartaction" value="addToCart">
		<input type="submit" name="cartaction" value="checkout">
	</form>

</body>

</html>

Check.html

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=Windows-31J">
</head>

<body>

	<center>
	<h2>購入商品</h2>
	</center>

	<br><br>
	
	以下が購入する商品と合計です。
	<br>
	<table border="1">
	
	#foreach ( $item in $ItemsToBuy.selecteditems )
		#if( $item.selected )
		<tr>
			<td>
				$item.title
			</td>
			<td>
				$item.author
			</td>
		</tr>
		<tr>
			<td>
				$item.publisher
			</td>
			<td>
				$item.price
			</td>
		</tr>
		#end
	#end
	</table>
	<br>
	<br>
	合計: $ItemsToBuy.total 円
</body>

</html>

Check.vm

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=Windows-31J">
</head>

<body>

	<center>
	<h2>購入商品</h2>
	</center>

	<br><br>
	
	以下が購入する商品と合計です。
	<br>
	<table border="1">
	
	#foreach ( $item in $ItemsToBuy.selecteditems )
		#if( $item.selected )
		<tr>
			<td>
				$item.title
			</td>
			<td>
				$item.author
			</td>
		</tr>
		<tr>
			<td>
				$item.publisher
			</td>
			<td>
				$item.price
			</td>
		</tr>
		#end
	#end
	</table>
	<br>
	<br>
	合計: $ItemsToBuy.total 円
</body>

</html>

index.jsp

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

target:

mvn-eclipse-cache.properties

#Temporary index for unavailable sources and javadocs
#Fri Jan 30 01:42:10 JST 2009
junit\:junit\:3.8.1\:javadoc=true
junit\:junit\:3.8.1\:sources=true

\

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>chap13app</groupId>
  <artifactId>Chap13App</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Chap13App Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    	<groupId>org.apache.struts</groupId>
    	<artifactId>struts-core</artifactId>
    	<version>1.3.9</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.struts</groupId>
    	<artifactId>struts-extras</artifactId>
    	<version>1.3.9</version>
    </dependency>
    <dependency>
    	<groupId>org.hibernate</groupId>
    	<artifactId>hibernate-core</artifactId>
    	<version>3.3.1.GA</version>
    </dependency>
    <dependency>
    	<groupId>org.hibernate</groupId>
    	<artifactId>hibernate-annotations</artifactId>
    	<version>3.4.0.GA</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.velocity</groupId>
    	<artifactId>velocity-tools</artifactId>
    	<version>1.3</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.velocity</groupId>
    	<artifactId>velocity</artifactId>
    	<version>1.6</version>
    </dependency>
    <dependency>
    	<groupId>postgresql</groupId>
    	<artifactId>postgresql</artifactId>
    	<version>8.3-603.jdbc3</version>
    </dependency>
    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-jdk14</artifactId>
    	<version>1.5.2</version>
    </dependency>
    <dependency>
    	<groupId>javassist</groupId>
    	<artifactId>javassist</artifactId>
    	<version>3.8.0.GA</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>Chap13App</finalName>
  </build>
</project>

代码来自日本的技术图书http://www.shuwasystem.co.jp/products/7980html/2197.html

分享到:
评论

相关推荐

    JAVA velocity模板引擎使用实例

    只导入velocity-1.7.jar这个包可能会报错,根据提示再导入velocity自带的其他包。 项目结构如下: 测试Tomcat index.jsp内容如下: 代码如下:&lt;&#37;– Created by IntelliJ IDEA. –%&gt;&lt;&#37;@ page ...

    Velocity所需jar包

    velocity所需jar下载 commons-beanutils.jar commons-collections-3.1.jar commons-digester-1.8.jar velocity-1.7.jar velocity-1.7-dep.jar velocity-tools-2.0.jar

    SpringBoot1.5以上版本兼容velocity,不降级spring

    2.导入本项目中的org.springframework目录下的velocity支持类,原样copy过去或自己打个jar 3.不想写java类配置就直接@ImportResource({"classpath:velocity.xml"})原来的xml配置 4.其他原来是啥就是啥开工

    velocity插件

    安装这个插件通常需要将其导入到Eclipse或MyEclipse的“软件站点”中,然后按照向导步骤进行安装。安装完成后,开发者就能在IDE中享受到上述提到的各种功能,提升Velocity开发的效率和乐趣。 总的来说,Velocity...

    MyEclipse-Velocity-2.0.8插件

    在这个案例中,它可能是指导用户如何将MyEclipse-Velocity插件安装到MyEclipse IDE中的步骤,包括下载、导入、激活等过程,也可能包含了一些基本的使用技巧和常见问题解答。用户在安装和使用插件前应仔细阅读此文件...

    Velocity模板应用案例

    - #import:导入Java类或包,如`#import('java.util.*')`。 - #function:定义函数,类似于宏,但可以接受参数和返回值。 - #stop:停止模板的执行。 - #evaluate:执行Java代码,然后将结果插入模板。 【Velocity...

    velocity document

    2. **灵活性**:Velocity的宏功能相对强大,更易于复用和扩展,而Freemarker的模板继承和导入功能则更灵活。 3. **性能**:两者在性能上各有千秋,具体取决于应用场景和优化程度。 **三、Velocity的上下文和模型**...

    velocity+itext导出pdf

    2. **初始化 Velocity**:在Java代码中,需要导入`org.apache.velocity.app.VelocityEngine`并实例化,配置Velocity的属性,如模板路径、缓存策略等。 3. **设置上下文**:创建一个`VelocityContext`对象,将需要在...

    Velocity简单案例

    7. **模板继承与导入**: Velocity支持模板的继承和导入,这样可以创建可重用的布局和部分。例如,一个基模板定义通用的页眉和页脚,子模板通过`#extend`继承基模板,并在适当位置添加自定义内容。 8. **宏库和...

    Velocity框架学习教程

    - **脚本导入**:`#parse("me.vm")`用于导入外部脚本。 - **停止执行**:`#stop`命令用于提前结束执行并返回。 - **宏定义**:类似于函数,用于封装代码块。 - **无参数宏**:`#macro(d)\n&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;\n#end...

    Velocity_API

    - **`#import`**: 导入其他模板中的宏。 ### 5. 错误处理与调试 Velocity 提供了日志系统和异常处理机制,可以通过配置属性来调整日志级别和错误处理策略。 ### 6. 性能优化 1. **缓存管理**: 可以缓存模板以...

    Velocity入门小实例,纯java工程

    可以通过Maven或Gradle等构建工具导入依赖,或者直接下载jar文件并放置于项目的lib目录。 3. **创建第一个Velocity模板** 在实例中,你可能会看到一个名为`VolecityTest`的文件,这可能是Velocity模板文件。模板...

    flash-animation-with-velocity:与Velocity.js一起使用Flash动画

    2. **Velocity.js的使用**:导入Velocity.js库到你的HTML文件中,可以使用CDN链接或者本地存储的文件。一旦引入,你可以使用`$.velocity`方法来创建动画效果。例如,如果你有一个Flash元素的引用,你可以用`element....

    Velocity模板解析

    - `Velocity.init()`初始化引擎,`Velocity.evaluate()`或`Velocity.mergeTemplate()`用于处理模板并生成输出。 4. **模板继承与导入** - 使用`#include`或`#parse`指令引入其他模板文件,实现模板复用。 - `#...

    Velocity实现模板(详细步奏)

    Velocity 还支持模板继承和导入,允许创建基模板并由子模板覆盖或扩展,以实现模板的复用和定制。此外,Velocity 提供了丰富的工具和库,如 VelocityTools,来增强其功能,如国际化支持、日期/时间处理等。 总之,...

    Velocity实战操作

    在导入的Eclipse项目中,你可能会看到以下组件: - `velocity.properties` - 配置文件,定义了Velocity的行为。 - `templates` 目录 - 存放模板文件。 - `controllers` 或 `services` - 包含处理业务逻辑的Java类,...

    Velocity-windows

    首次使用时,建议导入常用的语言和框架文档,以便于后续的开发工作。 总的来说,Velocity for Windows是一款强大的开发辅助工具,它将帮助开发者高效地管理文档和代码片段,提升开发效率。虽然存在试用期间的注册...

Global site tag (gtag.js) - Google Analytics