In my previous blog, I introduced the Tomcat drip configuration and the Struts2 drip build, assuming that this will not drip children's shoes to see my drip blog "Java based on Eclipse build SSH framework (above)." Today we go on to the content of the blog post. Continue to build our drop SSH framework.
(i) Integrating spring on the basis of the previous blog:
First we copy the jar that spring needs (the previous blog has) to the Lib under the Web-inf under the webcontent.
Next, under SRC, create a file named: Applicationcontext.xml. (Some people are prompted to create under Web-inf) Personal Advice : created under SRC .
Spring configuration files are available in two formats: DTD format. Schema format.
The configuration file format based on the DTD format is as follows:
<?xml version="1.0" encoding="UTF-8"?
> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <beans> <!-- Service配置 --> <bean id="loginService" class="com.hy.service.impl.LoginServiceImpl" /> </beans>
The configuration file in the schema format has its own namespace. The format is 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" XMLNS:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" Xmlns:context="Http://www.springframework.org/schema/context" Xmlns:tx="Http://www.springframework.org/schema/tx" xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.5. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema /context/spring-context-2.5.xsd Http://www.springframework.org/schema/tx HTTP://WWW.SPR Ingframework.org/schema/tx/spring-tx-2.5.xsd "> <!--service configuration -- <bean id= "loginservice" class=" Com.hy.service.impl.LoginServiceImpl " /> </Beans>
Here I am using a different kind of configuration method. Applicationcontext.xml content is:
<?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.or G/schema/beans/spring-beans-3.0.xsd "> <!--service configuration -- <bean id= "loginservice" class=" Com.hy.service.impl.LoginServiceImpl " /> <!--action configuration - <bean id="Loginserver" class="Com.hy.action.LoginAction " Scope="Prototype"> < property name="Loginservice" ref="Loginservice"> </Property > </Bean> </Beans>
In struts, this configuration is possible:
< package name="struts2" extends= "struts-default"> <!--The content of the class here is the same as the Bean's ID in the spring configuration file -- <action name="Login" class="Loginserver"> <result name="Success">/result.jsp</result> <result name="Input">/login.jsp</result> </Action> </Package >
Note Here is the action configuration in the Struts.xml file. class= "" is not the same as the struts we talked about in our last blog post. The class content here is the same as the ID of the action config bean inside the applicationcontext.xml!
。!
Next in Web. XML we need to add the following code:
<!--used to locate the spring frame profile -- <context-param> <param-name>Contextconfiglocation</param-name> <param-value>/web-inf/applicationcontext*.xml,classpath*:applicationcontext*.xml</param-value> </context-param> <!--Configure Spring listening -- <listener> <listener-class>Org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
This integration of spring to configure the file is finished, another point to emphasize that the integration of struts and spring need a jar (Struts2-spring-plugin-2.3.8.jar). This jar I put into the jar of the struts2 needed , adding this jar to the integration of struts and spring.
Before integrating hibernate, say something about the context configuration of the spring XML file.
Applicationcontext.xml In fact this file can be saved to the Classpath or Web-inf file. As the project grows, spring's configuration files become large and can be divided into several configuration files based on the established principles. This makes the configuration more clear. Improved maintainability. The notation in the above code is to find all the configuration files under the Classpath and Web-inf files (a lot of people say one of them.) Suppose the write lookup is not the same as the file save location. will be an error, OH ~).
Test and integrate the situation, such as the following:
Mo-Ha ~~demo I will be in the following to everyone, please see the URL inside, because this demo also contains the final SSH build test.
(ii) Integrated hibernate
The first is to put the jar that Hibernate needs (on the previous blog). Copy it into Lib under the Web-inf under the webcontent. Then add the following configuration to the Applicationcontext.xml:
<!--configuring data sources - <bean id= "dataSource" class=" Org.apache.commons.dbcp.BasicDataSource "destroy-method=" Close "> <!--Specify the drive to connect to the database < property name= "driverclassname" value=" Com.mysql.jdbc.Driver " /> <!--Specify the URL of the connection database-- < property name="url" value="Jdbc:mysql://localhost:3306/test" /> <!--Specify the user name to connect to the database-- < property name="username" value="root" /> <!--Specify a password to connect to the database-- < property name="password" value="" /> </Bean> <!--configuration Sessionfactory -- <bean id= "sessionfactory"class=" Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> < property name="DataSource"> <ref Bean="DataSource" /> </Property > < property name="Hibernateproperties"> <props> <prop key="Hibernate.dialect">Org.hibernate.dialect.MySQLDialect</prop> <prop key="Hibernate.hbm2ddl.auto">Update</prop> <prop key="Hibernate.connection.autocommit">True</prop> <prop key="Hibernate.show_sql">True</prop> <prop key="Sql_format">True</prop> </props> </Property > < property name="Mappingresources"> <!--specify hibernate mapping file -- <list> <value>Com/hy/entity/user.hbm.xml</value> </list> </Property > </Bean>
The framework is complete even if it is built here.
Some people will have doubts. Not to create a hibernate.cfg.xml or hibernate.properties configuration. In fact, in the above configuration file, did you find that there is a file in a bean that is particularly like the contents of these two files? In fact, that's it, no more two files to be created.
Here is a description of the assumption that your database is configured according to the MySQL configuration, do not forget to add the appropriate jar (I was told that the jar package should also be corresponding to their own data version number, otherwise not even on). Assuming that your database is Oracle, the configuration is configured in accordance with.
Finally, we're talking about the hibernate mapping file (the relationship mapping between classes and tables)
<hibernate-mapping> < Classes name ="class name" table ="table name"> <!--primary key -- <ID name="PRIMARY Key Name"> <column name="PRIMARY key column" /> <!--primary key generator -- <generator class="Build Policy" /> </ID> < name="property name" type="data type"> < column name = "Length" = " not-null" = "not Empty" /> </Property > </class></hibernate-mapping>
Test it:
Building SSH is over here. If you have any questions. Please leave a message for me ~ ~
Recently a small partner has reflected. Read my blog and then follow the error, summed up their mistakes here I briefly explain:
1. If the pro has not configured spring, do not put (Struts2-spring-plugin-2.3.8.jar). This jar package was imported into the project in advance (this is my mistake.) Put the jar in the package required by struts). Let's say there was an error validating struts on my blog. Then remove the package and you can. (Integrate spring don't forget to import oh ~)
2. Is the configuration problem: (, there is a picture of the truth ~)
The name of the configuration file and code should be corresponding, otherwise it will be wrong oh ~
3. Is our framework for Hibernate, children's shoes are known to write the mapping table, but do not forget the configuration file specified.
Finally attached:
Demo Address
Java-based Eclipse building SSH framework (bottom)