SSH instance (2), ssh instance
Create two files under WebContent \ WEB-INF \: applicationContext. xml and web. xml.
Web. xml:
<?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"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
The filter and listener are specified in web. xml.
ApplicationContext. xml:
<? 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: p = "http://www.springframework.org/schema/p" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <! -- Configure SessionFactory --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean "> <property name =" dataSource "> <ref local =" dataSource "/> </property> <! -- Configure the attributes of Hibernate --> <property name = "hibernateProperties"> <props> <prop key = "hibernate. dialect "> org. hibernate. dialect. mySQLDialect </prop> <prop key = "hibernate. show_ SQL "> true </prop> </props> </property> <! -- Specify the path of the Hibernate ing file --> <property name = "mappingResources"> <list> <value> com/school/entity/Clas. hbm. xml </value> </list> </property> </bean> <bean id = "dataSource" class = "org. springframework. jdbc. datasource. driverManagerDataSource "> <property name =" driverClassName "> <value> com. mysql. jdbc. driver </value> </property> <property name = "url"> <value> jdbc: mysql: // localhost: 3306/myssh </value> </property> <property name = "username"> <value> root </value> </property> <property name = "password"> <value> root </value> </property> </bean> <bean id = "clasDAO" class = "com. school. dao. clasDAOImpl "abstract =" false "lazy-init =" default "autowire =" default "> <property name =" sessionFactory "> <ref bean =" sessionFactory "/> </property> </bean> <bean id = "clasService" class = "com. school. service. clasServiceImpl "> <property name =" clasDAO "ref =" clasDAO "> </property> </bean> <bean id =" clasQueryAction "class =" com. school. action. clasQueryAction "> <property name =" clasService "ref =" clasService "> </property> </bean> <bean id =" clasAction "class =" com. school. action. clasAction "> <property name =" clasService "ref =" clasService "> </property> </bean> </beans>
ApplicationContext. xml defines multiple beans. dataSource defines the url, user name, password, and other attributes of the database to be connected. SessionFactory configures Hibernate attributes and the path of the ing file. The mapped com/school/entity/Clas. hbm. xml file is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
This file corresponds to the clas table in the MySQL database. The table structure is as follows:
The Clas file is as follows:
Package com. school. entity; public class Clas {// course id private int id; // Course name private String name; // course description private String comment; // default constructor public Clas () {}// constructor that contains all attributes public Clas (int id, String name, String comment) {super (); this. id = id; this. name = name; this. comment = comment;} public int getId () {return id;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getComment () {return comment;} public void setComment (String comment) {this. comment = comment ;}}
In applicationContext. xml, there is a reference relationship between beans:
The class corresponding to clasDAO is com. school. dao. ClasDAOImpl, with sessionFactory as the parameter;
The class corresponding to clasService is com. school. service. ClasServiceImpl, with clasDAO as the parameter;
The class corresponding to clasQueryAction is com. school. action. ClasQueryAction, with clasService as the parameter;
The class corresponding to clasAction is com. school. action. ClasAction, and clasService is used as the parameter.