1. Database:
Column |
Type |
Comment |
Id |
Int (one) Auto Increment |
|
Name |
varchar (+) NULL |
|
wr. |
varchar (255) NULL |
|
2. Overall structure:
3.model Layer Product.java
Package Com.zhoumo.model;
public class Product {
Private Integer ID;
private String name;
Private String URL;
constructor with no parameters
Public Product ()
{
}
Setter and getter methods for ID attributes
public void SetId (Integer ID)
{
This.id = ID;
}
Public Integer getId ()
{
return this.id;
}
Setter and Getter methods for the Name property
public void SetName (String name)
{
THIS.name = name;
}
Public String GetName ()
{
return this.name;
Setter and Getter methods for}//age properties
public void SetUrl (String URL)
{
This.url = URL;
}
Public String GETURL ()
{
return this.url;
}
}
4. Database mapping File Product.hbm.xml
<?xml version= "1.0" encoding= "GBK"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<class name= "Product" table= "Product" >
<!--mapping Identity Properties--
<id name= "id" column= "id"
type= "int" >
<!--Specify the primary key generator policy--
<generator class= "Increment"/>
</id>
<!--Map Normal Properties--
<property name= "Name" type= "string"/>
<property name= "url" type= "string"/>
</class>
5.DAO Layer
Public interface Productdao {
/**
* Save Instance
* @param instances that need to be saved
* @return The IDENTITY property value of the instance you just saved
*/
Integer Save (product product);
}
6.DAO Implementation Layer
Package Com.zhoumo.dao.impl;
Import com.zhoumo.model.Product;
Import com.zhoumo.dao.*;
Import Org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class Productdaoimpl extends Hibernatedaosupport implements Productdao {
/**
* Save Person instance
* @param person instance that person needs to save
* @return The IDENTITY property value of the person instance just saved
*/
Public Integer Save (product product)
{
Return (Integer) gethibernatetemplate ()
. Save (product);
}
7. Service Layer
Package com.zhoumo.service;
Import com.zhoumo.model.Product;
public interface productservice{
Integer Save (product product);
}
8. Service Implementation Layer
Package Com.zhoumo.service.impl;
Import Com.zhoumo.dao.ProductDao;
Import com.zhoumo.model.Product;
Import Com.zhoumo.service.ProductService;
public class Productserviceimpl implements productservice{
Private Productdao Productdao;
Relies on setter methods required to inject DAO components
public void Setproductdao (Productdao Productdao)
{
This.productdao = Productdao;
}
Public Integer Save (product product) {
TODO auto-generated Method Stub
return Productdao.save (product);
}
}
9. Action Layer
Package com.zhoumo.action;
Import com.opensymphony.xwork2.Action;
Import com.zhoumo.model.Product;
Import Com.zhoumo.service.ProductService;
public class Addproductaction implements Action
{
The following is a property for encapsulating user request parameters
Private product product;
Properties for encapsulating processing results
Private String tip;
Business logic components used by the system
Private Productservice Productservice;
Set setter methods necessary to inject business logic components
public void Setproductservice (Productservice productservice)
{
This.productservice = Productservice;
}
Setter and Getter methods for the person property
public void Setproduct (product product)
{
This.product = product;
}
Public Product getproduct ()
{
return this.product;
}
Setter and Getter methods for tip properties
public void Settip (String tip)
{
This.tip = tip;
}
Public String Gettip ()
{
return this.tip;
}
Public String Execute () throws Exception {
TODO auto-generated Method Stub
if (Productservice.save (product) >0)
{
Settip ("Haha, registered successfully!") ");
return SUCCESS;
}
Else
{
Settip ("Haha, registration failed! ");
return ERROR;
}
}
}
10. Configuration file:
Structs.xml
<?xml version= "1.0" encoding= "GBK"?
<!--Specify the DTD information for the Struts 2 configuration file-->
<! DOCTYPE struts public
-//apache software foundation//dtd struts Configuration 2.1.7//en "
"/http STRUTS.APACHE.ORG/DTDS/STRUTS-2.1.7.DTD
<!--The root element of the Struts 2 configuration file-->
<struts>
<!-- The series constants are configured-->
<constant name= "struts.i18n.encoding" value= "GBK"/>
<constant name= "Struts.devmode" Value= "true"/>
<package name= "XQH" extends= "Struts-default";
<!--define the action to handle the user request, Specifies the implementation class--> for the action;
<action name= "addproduct"
class= "com.zhoumo.action.AddProductAction";
<!- -Configure View page--> for two logical views;
<result name= "error" >/ERROR.JSP</RESULT>
<result name= "Success" >/ Welcome.jsp</result>
</action>
<!--to list all view pages--> when the user accesses the app directly;
<action name= "*" >
<result>/{1}.jsp</result>
</action>
</package>
</struts>
One. applicationcontext.xml file
<?xml version= "1.0" encoding= "GBK"?>
<!--Specify the schema information for the spring configuration file--
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<!--define the data source bean, using the C3P0 data source to implement-
<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method= "Close" >
<!--Specify the drive to connect to the database
<property name= "Driverclass" value= "Com.mysql.jdbc.Driver"/>
<!--Specify the URL of the connection database--
<property name= "Jdbcurl" value= "jdbc:mysql://10.0.7.99:3306/auction?useunicode=true&characterencoding =utf-8 "></property>
<!--Specify the user name to connect to the database--
<property name= "User" value= "root"/>
<!--specify a password to connect to the database--
<property name= "password" value= "/>"
<!--Specify the maximum number of connections to connect to a database connection pool--
<property name= "maxpoolsize" value= "/>"
<!--Specify the minimum number of connections to connect to the database connection pool--
<property name= "Minpoolsize" value= "1"/>
<!--Specify the number of initialization connections to connect to the database connection pool-
<property name= "Initialpoolsize" value= "1"/>
<!--Specify the maximum idle time for connections that connect to a database connection pool-
<property name= "maxidletime" value= "/>"
</bean>
<!--define Hibernate sessionfactory--
<bean id= "Sessionfactory"
class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<!--Dependency Injection data source, injected exactly as defined above datasource--
<property name= "DataSource" ref= "DataSource"/>
<!--The Mappingresouces property is used to list all mapping files--
<property name= "Mappingresources" >
<list>
<!--the following to list hibernate mapping files--
<value>com/zhoumo/model/Product.hbm.xml</value>
</list>
</property>
<!--defines the properties of Hibernate sessionfactory--
<property name= "Hibernateproperties" >
<!--Configure Hibernate Properties-
<value>
Hibernate.dialect=org.hibernate.dialect.mysqlinnodbdialect
Hibernate.hbm2ddl.auto=update
Hibernate.show_sql=true
Hibernate.format_sql=true;
</value>
</property>
</bean>
<!--defining DAO bean-->
<bean id= "Productdao" class=
"Com.zhoumo.dao.impl.ProductDaoImpl" >
<!--sessionfactory required to inject persistent operations
<property name= "Sessionfactory" ref= "Sessionfactory"/>
</bean>
<!--Configure a business logic component--
<bean id= "Productservice" class=
"Com.zhoumo.service.impl.ProductServiceImpl" >
<!--injection of the DAO components required for persistent access--
<property name= "Productdao" ref= "Productdao"/>
</bean>
<!--configuring Hibernate's local transaction manager, using the Hibernatetransactionmanager class--
<!--This class implements the Platformtransactionmanager interface, which is a specific implementation for hibernate-
<bean id= "TransactionManager"
class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<!--need to inject sessionfactory reference when configuring Hibernatetransactionmanager--
<property name= "Sessionfactory" ref= "Sessionfactory"/>
</bean>
<!--configuring transaction enhancement processing, specifying transaction manager--
<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" >
<!--for configuring verbose transaction semantics--
<tx:attributes>
<!--all methods that start with ' get ' are read-only--
<tx:method name= "get*" read-only= "true"/>
<!--Other methods use the default transaction settings--
<tx:method name= "*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--Configure a pointcut--
<aop:pointcut id= "Pointcut"
expression= "Bean (Service)"/>
<!--specifies that the Txadvice transaction enhancement process is applied at the Pointcut pointcut
<aop:advisor advice-ref= "Txadvice"
pointcut-ref= "Pointcut"/>
</aop:config>
</beans>
12.web.xml file
<?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 ">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--Initialize Spring container with Contextloaderlistener--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--define the filterdispathcer of Struts 2
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!--Filterdispatcher is used to initialize Struts 2 and handle all Web requests. -
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
13.JSP page
Product New Page
<%--
Website: <a href= "http://www.crazyit.org" > Crazy Java Alliance </a>
Author Yeeku. H.lee [email protected]
Version 1.0
Copyright (C), 2001-2012, Yeeku. H.lee
protected by copyright laws.
Program Name:
Date:
--%>
<%@ page contenttype= "text/html; CHARSET=GBK "language=" java "errorpage=" "%>
<% @taglib prefix= "s" uri= "/struts-tags"%>
<title> Products Added </title>
<body>
<s:form action= "Addproduct" >
<s:textfield name= "Product.name" label= "User name" Value= "/>
<s:textfield name= "product.url" label= "link" value= "/>
<tr>
<TD colspan= "2" >
<s:submit value= "Register" theme= "simple"/>
<s:reset value= "Refill" theme= "simple"/></td>
</tr>
</s:form>
</body>
Success Prompt Page
<%--
Website: <a href= "http://www.crazyit.org" > Crazy Java Alliance </a>
Author Yeeku. H.lee [email protected]
Version 1.0
Copyright (C), 2001-2012, Yeeku. H.lee
protected by copyright laws.
Program Name:
Date:
--%>
<%@ page contenttype= "text/html; CHARSET=GBK "language=" java "errorpage=" "%>
<% @taglib prefix= "s" uri= "/struts-tags"%>
<title> Success Page </title>
<body>
You are already logged in!
<s:property value= "Tip"/>
</body>
Error Prompt page
<%--
Website: <a href= "http://www.crazyit.org" > Crazy Java Alliance </a>
Author Yeeku. H.lee [email protected]
Version 1.0
Copyright (C), 2001-2012, Yeeku. H.lee
protected by copyright laws.
Program Name:
Date:
--%>
<%@ page contenttype= "text/html; CHARSET=GBK "language=" java "errorpage=" "%>
<% @taglib prefix= "s" uri= "/struts-tags"%>
<title> Error Pages </title>
<body>
Registration failed!
</body>
The first structs+spring+hibernate Web program