SSH simple integration

Understand:

struts to implement the interaction between browser and server;

spring completes object creation, object dependency and transaction processing

hibernate completes data access

Difficult points:

1. By accessing the browser, spring can create an action without creating it by itself

2. Set c3p0 in spring to improve performance.

3. The XML file reference file contains error causes tomcat to fail to start

Solution: in Preferences > XML file > validation > XML, the check mark before "hour all XML schema locations" is removed. It will disable validation that points to the same namespace reference at different schema locations, only as a result of the first found verifiable XML file.

(from: https://blog.csdn.net/u013087513/article/details/70991422)

web.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
	<!-- To configure spring Of OpenSessionInView Mode [Purpose: JSp Page access lazy loading data] -->
	<filter>
		<filter-name>OpenSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>OpenSessionInView</filter-name>
		<url-pattern>*/</url-pattern>
	</filter-mapping>
	
	<!-- struts2 To configure -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- Spring To configure -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:bean*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

Public bean.xml configuration:

<?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"
	xmlns:context="http://www.springframework.org/schema/context"
	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.xsd
     	 http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
     	 http://www.springframework.org/schema/tx/spring-tx.xsd">


	
	<!-- 1) Connection pool instance -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///day16"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
		<property name="initialPoolSize" value="3"></property>
		<property name="maxPoolSize" value="6"></property>
	</bean>

	<!-- 2) SessionFactory Instance creation -->
	<!-- All configurations are configured by spring Maintain(Not required in project hibernate.cfg.xml La) -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- a. Connection pool -->
		<property name="dataSource" ref="dataSource"></property>
		
		<!-- b. hibernate Common configuration: dialect, display sql,Automatic table building, etc -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		
		<!-- c. Mapping configuration -->
		<property name="mappingLocations">
			<list>
				<value>classpath:SSH/*.hbm.xml</value>
			</list>
		</property>
	</bean>
	
	<!-- 3) Transaction configuration -->
	<!-- # Transaction manager -- >
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- # Transaction enhancement -- >
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="*" read-only="false"/>
		</tx:attributes>
	</tx:advice>
	<!-- # AOP configuration -- >
	<aop:config>
		<aop:pointcut expression="execution(* SSH.*.*(..))" id="pt"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
	</aop:config>
	
</beans>     

bean-dao.xml:

    <bean id = "adminDao" class="SSH.AdminDao">
       <property name="sf" ref="sessionFactory"></property>
    </bean>

dao layer code:

package SSH;

import org.hibernate.SessionFactory;

public class AdminDao {
    private SessionFactory sf;
    public void setSf(SessionFactory sf) {
		this.sf = sf;
	}
    public Admin query(int id){
    	Admin admin = (Admin) sf.getCurrentSession().get(Admin.class, id);
    	return admin;
    }
}

Action code:

package SSH;

import org.apache.struts2.ServletActionContext;

public class AdminAction {
   private AdminService service;
   public void setService(AdminService service) {
	this.service = service;
   }
   public String query(){
	   int id = 2;
	   Admin admin = service.query(id);
	   ServletActionContext.getRequest().setAttribute("admin", admin);
	   return "success";
   }
}

 

Tags: xml Spring Hibernate ssh

Posted on Fri, 10 Jan 2020 11:07:04 -0500 by bfinucan