- 浏览: 71696 次
- 性别:
- 来自: 东京
最新评论
-
JavaStudyEye:
讲解的很详细,呵呵
Ruby on Rails入门例子 -
greatwqs:
提供一个下载的例子岂不是更好?
Struts2.1+Spring3.0+JPA1.0(Hibernate3.3实现)例子 -
einsteinm:
老套路!
Struts+Spring+Hibernate添加用户例子 -
processlife:
个人感觉有些陈旧...
现在比较流行 appfuse 的ss ...
Struts+Spring+Hibernate添加用户例子 -
dhxyu:
看看你是怎么使用hibernate的
Struts+Spring+Hibernate添加用户例子
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
发表评论
-
Struts2.1+Spring3.0+JPA1.0(Hibernate3.3实现)例子
2010-01-28 14:03 7650本文代码是传智播客黎活明老师所讲的《Spring2.5视频教程 ... -
Struts+Spring+iBATIS做的XML文件操作例子
2010-01-21 10:08 1066去年圣诞节前的一个例子,初版,给别人说明用的。 Str ... -
Struts1的版本差异——Action!
2009-12-25 13:44 883近来的项目要用Struts1.0.2框架,真够老的! 昨天模 ... -
中规模应用的开发(二)
2009-07-20 22:13 993BookStore\src\main\webapp\WEB-I ... -
中规模应用的开发(一)
2009-07-20 21:50 1205BookStore pom.xml <proje ... -
导入Spring
2009-07-20 17:38 1223src\main\java\chap12app: Mai ... -
导入Maven
2009-07-20 17:27 1005一.Maven的展开 1.在 ... -
小规模应用的开发
2009-07-20 16:30 1121init: initdb dropdb BookSto ... -
导入Struts2
2009-07-20 15:54 871src: struts.xml <?xml ver ... -
导入Hibernate
2009-07-20 15:44 1176src\chap06app: Main.java pa ... -
JDBC登录
2009-07-20 15:31 1239LoginCheckServlet.java package ... -
JDBC
2009-07-20 15:18 705Main.java package chap05app; ... -
JSP/Servlet编程
2009-07-20 15:06 573PersonalInfoCheckServlet.java ... -
Spring Live-Spring Quick Start Tutorial
2007-03-02 11:35 1345http://sourcebeat.com/downloads ... -
Sysdeo Eclipse Tomcat Launcher plugin备份
2007-02-26 09:48 3053http://www.sysdeo.com/eclipse/t ... -
Struts+Spring+Hibernate添加用户例子
2007-02-02 12:41 4136由于jar太大,抓成图片以供下载. 图片在lib目录下. 数据 ... -
关于Struts+Hibernate分页的例子
2007-01-08 08:37 3018这个程序很久了,当时就查询分页这没有成功,一点遗憾:( ... -
测试 Enterprise Beans
2006-12-29 11:45 1713这个测试企业Beans的教程内容不多,却花费我很长时间。主要是 ... -
创建 J2EE 应用程序客户端
2006-12-29 10:24 1769确切地说,这个例子很失败! 从http://www.netbe ... -
J2EE 应用程序快速入门指南
2006-12-29 08:28 2247近来想系统地学习一下Java,NetBeas有较丰富的在线教程 ...
相关推荐
只导入velocity-1.7.jar这个包可能会报错,根据提示再导入velocity自带的其他包。 项目结构如下: 测试Tomcat index.jsp内容如下: 代码如下:<%– Created by IntelliJ IDEA. –%><%@ page ...
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
2.导入本项目中的org.springframework目录下的velocity支持类,原样copy过去或自己打个jar 3.不想写java类配置就直接@ImportResource({"classpath:velocity.xml"})原来的xml配置 4.其他原来是啥就是啥开工
安装这个插件通常需要将其导入到Eclipse或MyEclipse的“软件站点”中,然后按照向导步骤进行安装。安装完成后,开发者就能在IDE中享受到上述提到的各种功能,提升Velocity开发的效率和乐趣。 总的来说,Velocity...
在这个案例中,它可能是指导用户如何将MyEclipse-Velocity插件安装到MyEclipse IDE中的步骤,包括下载、导入、激活等过程,也可能包含了一些基本的使用技巧和常见问题解答。用户在安装和使用插件前应仔细阅读此文件...
- #import:导入Java类或包,如`#import('java.util.*')`。 - #function:定义函数,类似于宏,但可以接受参数和返回值。 - #stop:停止模板的执行。 - #evaluate:执行Java代码,然后将结果插入模板。 【Velocity...
2. **灵活性**:Velocity的宏功能相对强大,更易于复用和扩展,而Freemarker的模板继承和导入功能则更灵活。 3. **性能**:两者在性能上各有千秋,具体取决于应用场景和优化程度。 **三、Velocity的上下文和模型**...
2. **初始化 Velocity**:在Java代码中,需要导入`org.apache.velocity.app.VelocityEngine`并实例化,配置Velocity的属性,如模板路径、缓存策略等。 3. **设置上下文**:创建一个`VelocityContext`对象,将需要在...
7. **模板继承与导入**: Velocity支持模板的继承和导入,这样可以创建可重用的布局和部分。例如,一个基模板定义通用的页眉和页脚,子模板通过`#extend`继承基模板,并在适当位置添加自定义内容。 8. **宏库和...
- **脚本导入**:`#parse("me.vm")`用于导入外部脚本。 - **停止执行**:`#stop`命令用于提前结束执行并返回。 - **宏定义**:类似于函数,用于封装代码块。 - **无参数宏**:`#macro(d)\n<tr><td></td></tr>\n#end...
- **`#import`**: 导入其他模板中的宏。 ### 5. 错误处理与调试 Velocity 提供了日志系统和异常处理机制,可以通过配置属性来调整日志级别和错误处理策略。 ### 6. 性能优化 1. **缓存管理**: 可以缓存模板以...
可以通过Maven或Gradle等构建工具导入依赖,或者直接下载jar文件并放置于项目的lib目录。 3. **创建第一个Velocity模板** 在实例中,你可能会看到一个名为`VolecityTest`的文件,这可能是Velocity模板文件。模板...
2. **Velocity.js的使用**:导入Velocity.js库到你的HTML文件中,可以使用CDN链接或者本地存储的文件。一旦引入,你可以使用`$.velocity`方法来创建动画效果。例如,如果你有一个Flash元素的引用,你可以用`element....
- `Velocity.init()`初始化引擎,`Velocity.evaluate()`或`Velocity.mergeTemplate()`用于处理模板并生成输出。 4. **模板继承与导入** - 使用`#include`或`#parse`指令引入其他模板文件,实现模板复用。 - `#...
Velocity 还支持模板继承和导入,允许创建基模板并由子模板覆盖或扩展,以实现模板的复用和定制。此外,Velocity 提供了丰富的工具和库,如 VelocityTools,来增强其功能,如国际化支持、日期/时间处理等。 总之,...
在导入的Eclipse项目中,你可能会看到以下组件: - `velocity.properties` - 配置文件,定义了Velocity的行为。 - `templates` 目录 - 存放模板文件。 - `controllers` 或 `services` - 包含处理业务逻辑的Java类,...
首次使用时,建议导入常用的语言和框架文档,以便于后续的开发工作。 总的来说,Velocity for Windows是一款强大的开发辅助工具,它将帮助开发者高效地管理文档和代码片段,提升开发效率。虽然存在试用期间的注册...