SSH integrates three frameworks of Spring Struts Hibernate

Struts, Spring, Hibernate integration

Statement : I just learned the SSH framework, if there are any shortcomings, please forgive me, put forward, thank you

The frame package I use: http://pan.baidu.com/s/1bp1VBx9 The

first step: create Project

Step 2 : Build Struts framework

Step 3: Integrate ss

Step 4: Insert Hibernate framework Step

5 : Complete SSH integration


1. Create a JAVA project
Create a Wwb project with eclipse (development tool), and pay attention to generating the web.xml file.


2. Configure the web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ssh</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
   <!-- Configure spring's listener -->  
	<listener>  
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
	</listener>  
	<context-param>  
		<param-name>contextConfigLocation</param-name>  
		<!-- Add '/' before application to query in the src root directory, otherwise query in the web root directory -->
		<param-value>classpath:/applicationContext*.xml</param-value>  
	</context-param>
		
  	<!-- Configure the core filter of Struts2-->  
	<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>
	
</web-app>


Note here: The '/' in front of applicationContext*.xml in <param-value>classpath:/applicationContext*.xml</param-value> means that
the file is searched in the Java file. If the '/' is removed, it will default to the Web root directory Find below! !

3. Configure struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
	
<struts>
	<!--struts.devMode : Whether to set to development mode -->
	<constant name="struts.devMode" value="true" />
	
	<!-- namespace : corresponds to the "/" after the project name -->
	<package name="front" namespace="/" extends="struts-default">
		<action name="login" class="loginAction" method="login">
			<result name="success">/index.jsp</result>
		</action>		
	</package>
</struts>


4. Integrate SS
First create an applicationContext.xml file with the following contents:
<?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:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
		
		<!--Configure the processor for annotations-->   
	 	<bean id="loginAction" class="com.mwl.action.LoginAction"/>
</beans>


After confirming that the 'bean id' is consistent with the value of class in struts.xml, create the LoginAction file:
package com.mwl.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{	
	public String login() throws Exception {

		return SUCCESS;
	}


Finally, create the index.jsp file and enter the content "Hello word"
to run the JAVA file. If the webpage displays Hello word, the SS integration framework is successfully built.

Note here: the integration of String and Struts must use the struts2-spring-plugin-2.3.4.jar package, otherwise the integration cannot be successful.

5. Configure the hibernate.cfg.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- link database -->  
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sshTest</property>
		<!-- database driver-->  
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<!-- Database account password-->
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<!-- Tool to update data table according to schema-->  
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- Whether to display SQL -->
		<property name="hibernate.show_sql">true</property>
		<!-- Data table mapping configuration file -->  
		<mapping resource="com/mwl/model/Person.hbm.xml"/>
	</session-factory>
</hibernate-configuration>


At the same time, create the Person class and configure the corresponding mapping file Person.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.mwl.model">
	<class name="Person" table="person">
		<id name="id" column="id">
			<!-- auto increment -->
			<generator class="increment"></generator>
		</id>
		
		<property name="name"/>
		<property name="pwd"/>
	</class>
</hibernate-mapping>


6. Improve applicationContext.xml according to the project process Action-Service-Dao:
<?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:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
		
		<!--Configure the processor for annotations-->   
	 	<bean id="loginAction" class="com.mwl.action.LoginAction">
	 		<property name="loginService" ref="loginService"/>
	 	</bean>
	 	<bean id="loginService" class="com.mwl.service.LoginServiceImpl">
	 		<property name="loginDao" ref="loginDao"/>
	 	</bean>
	 	<bean id="loginDao" class="com.mwl.dao.LoginDaoImpl">
	 		<property name="sessionFactory" ref="sessionFactory"/>
	 	</bean>
	 	
	 	 <!-- Configure to use Hibernate-based transaction manager -->
		<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
			<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
		</bean>

</beans>


7. According to the content of applicationContext.xml, create the Class class and its related interfaces in turn, as follows:
package com.mwl.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.mwl.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	String name;
	String pwd;
	
	private LoginService loginService;
	
	public String login() throws Exception {
		//accept data
		String notice =loginService.login(name, pwd);
		HttpServletRequest request=ServletActionContext.getRequest();
		// transfer to page
		request.setAttribute("notice", notice);
		return SUCCESS;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public LoginService getLoginService() {
		return loginService;
	}
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
}


According to the LoginAction class, create the interface LoginService
package com.mwl.service;

import com.mwl.model.Person;

public interface LoginService {
	 public String login(String username,String password);
}


Then create its implementation class:
package com.mwl.service;

import com.mwl.dao.LoginDao;
import com.mwl.model.Person;

public class LoginServiceImpl implements LoginService{
	private LoginDao loginDao;
	@Override
	public String login(String username, String password) {
		Person p =new Person();
		p.setName(username);
		p.setPwd(password);
		loginDao.save(p);
		return "Login successful";
	}
	public LoginDao getLoginDao() {
		return loginDao;
	}
	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}
}


According to the LoginServiceImpl class, create the interface LoginDao
package com.mwl.dao;

import com.mwl.model.Person;

public interface LoginDao {
	public void save(Person p);
}


Then create the implementation class LoginDaoImpl of LoginDao
package com.mwl.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.mwl.model.Person;

public class LoginDaoImpl implements LoginDao{
	SessionFactory  sessionFactory;
	
	@Override
	public void save(Person p) {
		Session session=sessionFactory.openSession();
		session.beginTransaction().begin();

		Person a=new Person();
		a.setName("222");
		a.setPwd("123");
		
		session.save(a);
		
		session.beginTransaction().commit();
	}
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}	
}


Finally, create the database sshTest, and create the detection class to check the code.
Run Tomcat and enter localhost.
If the table person is automatically generated in the database sshTest, and the name and pwd are assigned values ​​of 222 and 123 respectively, the SSH framework integration is successful! ! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326208071&siteId=291194637