This article mainly describes how to use spring MVC + Hibernate framework for web development in eclipse. Installation of Eclipse, JDK, and myeclipse is not included here, nor is myeclipse used to build them, it's just a manual setup.
1. Version
Jdk1.50; eclipse3.2; myeclipse 5.0; Tomcat 5.0; spring2.0; hibernate3.2.
2. Prepare
Install the eclipse and JDK environments. Because myeclipse is not used, you need to prepare two main packages: Spring. jar packages, hibernate3.jar, and packages such as commons-dbcp.jar, commons-pool.jar, and spring-orm.jar; these tables can be downloaded from the corresponding project site or searched online.
After installation, the Lib package contains:
Activation. Jar
Antlr-2.7.6.jar
ASM. Jar
Cglib-2.1.3.jar
Commons-beanutils.jar
Commons-collections.jar
Commons-collections-2.1.1.jar
Commons-digester.jar
Commons-fileupload.jar
Commons-logging.jar
Commons-logging-1.0.4.jar
Commons-validator.jar
Dom4j-1.6.1.jar
Ehcache-1.2.jar
Jakarta-oro.jar
Jstl. Jar
JTA. Jar
Log4j-1.2.11.jar
Mail. Jar
Ojdbc14.jar
Standard. Jar
The following are important:
Commons-dbcp.jar
Commons-pool.jar
Hibernate3.jar
Spring. Jar
Spring-orm.jar
If you want to use struts, there will be a struts. jar package.
3. Build:
First, under the "file" menu "new" A "project", select "Web project" under myeclipse under "Other ", put the above package to webroot/WEB-INF/LIB;
After the project is created, we create two new projects under the project's webroot/WEB-INF. XML file, the name can be casually started (you can also create a new, content and the content of the two files can be the same), here named as web-config.xml and model-config.xml, as the name suggests, web-config.xml configuration and Servlet and control logic and other web-related beans, model-config.xml configuration and data model layer related beans, such as data access bean.
Here is the model-config.xml content:
<? XML version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd>
<! -- Set parameters related to the database connection pool. Here we use the mongoel database -->
<Bean id = "datasource"
Class = "org. Apache. commons. DBCP. basicdatasource"
Destroy-method = "close">
<Property name = "driverclassname">
<Value> oracle. JDBC. Driver. oracledriver </value>
</Property>
<Property name = "url">
<Value> JDBC: oracle: thin: @ 192.168.6.4: 1521: database </value>
</Property>
<Property name = "username">
<Value> username </value>
</Property>
<Property name = "password">
<Value> password </value>
</Property>
</Bean>
<! -- Defines the hibernate sessionfactory and hibernate ing files. All hibernate ing files are defined here. -->
<Bean id = "sessionfactory"
Class = "org. springframework. Orm. hibernate3.localsessionfactorybean"
Destroy-method = "Destroy">
<Property name = "datasource" ref = "datasource"/>
<Property name = "mappingresources">
<List>
<! -- The Hibernate ing file maps the hibernate configuration file here, which defines a persistence class ing file for the uuser class -->
<Value> COM/yondor/yuejiao/ORM/uuser. HBM. xml </value>
</List>
</Property>
<Property name = "hibernateproperties">
<Props>
<Prop key = "hibernate. dialect">
Org. hibernate. dialect. oracle9dialect
</Prop>
</Props>
</Property>
</Bean>
<! -- Spring hibernatetemplate template definition -->
<Bean id = "hibernatetemplate"
Class = "org. springframework. Orm. hibernate3.hibernatetemplate">
<Property name = "sessionfactory" ref = "sessionfactory"/>
</Bean>
<! -- Hibernate Data Model bean definition -->
<Bean id = "dbdao"
Class = "com. yondor. yuejiao. model. Common. dbdao">
<Property name = "hibernatetemplate" ref = "hibernatetemplate"/>
</Bean>
<! -- The following is the definition of the business logic Bean -->
<! -- The bean defined by the user module can be used in the web-config.xml configuration file -->
<Bean id = "userdao"
Class = "com. yondor. yuejiao. model. example. userdao">
<Property name = "dbdao" ref = "dbdao"/>
</Bean>
<! -- End of User Module -->
</Beans>
The contents of the web-config.xml are as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd>
<! -- Controller method call rule definition -->
<Bean id = "paramethodresolver"
Class = "org. springframework. Web. servlet. MVC. multiaction. parametermethodnameresolver">
<Property name = "paramname" value = "action"/>
<Property name = "defaultmethodname" value = "list"/>
</Bean>
<! -- Page view layer basic information settings -->
<Bean id = "viewresolver"
Class = "org. springframework. Web. servlet. View. internalresourceviewresolver">
<Property name = "viewclass"
Value = "org. springframework. Web. servlet. View. jstlview"/>
<Property name = "prefix" value = "/yuejiao/"/>
<Property name = "suffix" value = ". jsp"/>
</Bean>
<! -- Servlet ing list, which is defined here by the servlet of all control layer controllers -->
<Bean id = "urlmapping"
Class = "org. springframework. Web. servlet. handler. simpleurlhandlermapping">
<Property name = "mappings">
<Props>
<Prop key = "example. Do"> usercontroller </prop>
</Props>
</Property>
</Bean>
<! -- The following control layer controller bean definition starts -->
<! -- Start the user module -->
<! -- A total of seven attributes (bean) are injected, where userdao is the business logic bean, which is defined in the corresponding module of the model-config.xml -->
<Bean id = "usercontroller"
Class = "com. yondor. yuejiao. Controller. example. usercontroller">
<Property name = "methodnameresolver" ref = "paramethodresolver"/>
<! -- Used to define userdao in the model-config.xml file -->
<Property name = "userdao" ref = "userdao"/>
<! -- The following attributes are related to the redirection after processing -->
<Property name = "login_success" value = "example/login_success"/>
<Property name = "login_failure" value = "example/login"/>
<Property name = "register_success" value = "example/register_success"/>
<Property name = "register_failure" value = "example/register"/>
<Property name = "userinfolist" value = "example/List"/>
</Bean>
<! -- End of User Module -->
</Beans>
Finally, you need to configure the Web. xml file as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
Version = "2.4">
<Session-config>
<Session-Timeout>
30
</Session-Timeout>
</Session-config>
<Servlet>
<Servlet-Name> dispatcherservlet </servlet-Name>
<Servlet-class>
Org. springframework. Web. servlet. dispatcherservlet
</Servlet-class>
<Init-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>/WEB-INF/model-config.xml,/WEB-INF/web-config.xml </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-Name> dispatcherservlet </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>
</Web-app>
At this point, the development environment has been basically set up. Finally,
Right-click the project name -- properties -- Java bulid path -- libraries, add jars, and introduce the added package to the project.