Use eclipse to build spring + hibernate + struts framework (learning)

[color=blue]Building spring + hibernate + struts framework with eclipse (learning)[/color]


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

each of Struts + Spring + Hibernate What are the characteristics of?



The MVC design pattern of Struts can make our logic very clear, mainly responsible for the display of the presentation layer.

Spring's IOC and AOP can decouple our project to the greatest extent possible.

Hibernate is the persistence of entity objects and the encapsulation of the database.
Presentation layer, middle layer (business logic layer) and data service layer. The three-tier system places business rules, data access and legality verification in the middle tier. The client does not directly interact with the database, but establishes a connection with the middle layer through components, and then the middle layer interacts with the database.



The presentation layer is the traditional JSP technology.

The middle layer adopts the popular Spring+Hibernate. In order to separate the control layer from the business logic layer, it is subdivided into the following types.



The Web layer, which is the "C" (controller) in the MVC model, is responsible for controlling the interaction between the business logic layer and the presentation layer, calling the business logic layer, and returning business data to the presentation layer for organizational performance. The MVC framework of the system uses Struts .



The Service layer (that is, the business logic layer) is responsible for implementing business logic. The business logic layer is based on the DAO layer, and completes the business logic required by the system through the positive mode packaging of the DAO components.



The DAO layer is responsible for interacting with persistent objects. This layer encapsulates the operations of adding, deleting, checking, and modifying data.



PO, persistent object. The data of the relational database is mapped into objects through the entity-relationship mapping tool, which is very convenient to operate the database in an object-oriented manner.



The role of Spring runs through the entire middle layer, seamlessly integrating the Web layer, Service layer, DAO layer and PO, and its data service layer is used to store data.



A good framework relieves developers of the burden and effort of rebuilding solutions to complex problems; it can be extended for internal customization; and it has a strong user community to support it. A framework is usually a good solution to a problem. However, your application is layered, and each layer may require its own framework.


Now start to build SSH:
Step 1:
       Create a Wwb project in eclipse (development tool), and pay attention to check that the web.xml file can be generated.

Step 2:
      Import the jar package
     My Baidu network disk sharing link http://pan.baidu.com/s/1bp1VBx9  is the jar package required by SSH.

Step 3:
      Configure the filters and listeners of [/url]web.xml

<?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>cssh</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 Filters-->
  <filter>
	  <filter-name>hello</filter-name>
	  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
     <filter-name>hello</filter-name>
	 <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- configure listener -->
  <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:/applicationContext.xml</param-value>  
    </context-param>  
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>



Step 4:
   Create a new struts.xml header file in the src folder of this project as

<?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>
   <!--Define a package-->
   
    	<package name="user" namespace="/" extends="struts-default">
    	
    	<!-- Define an action with configuration jump information similar to
             Servlet@WebServlet("/IndexServlet")
    		class corresponds to the action class written by yourself. When the method attribute is not written, execute is called by default.
    	 -->
    		<action name="login" class="userAction" method="login">
    			<result name="success">
    				/success.jsp
    			</result>
    		
    		</action>
    	
    	</package>
    
    </struts>


Step 5: Create a UserAction      in
    com.action     to inherit ActionSupport




  

    package com.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.userservice.UserService;

public class UserAction extends ActionSupport {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	String name;
	String pwd;
	
	
	private UserService userService;
	public String login() throws Exception {
		
		String message=userService.login(name, pwd);
		HttpServletRequest request= ServletActionContext.getRequest();
		 request.setAttribute("message", message);
		return SUCCESS;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	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;
	}
}



Step 6: Create hibernate.cfg.xml in the src directory 

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- After the link database, chengs is the name of the database -->
<property name="hibernate.connection.url">jdbc:mysql://192.168.99.100:3306/chengs</property>
<!-- database driver com.mysql.jdbc.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>
<!-- show SQL -->

<property name="hibernate.show_sql">true</property>
<!-- Data table mapping configuration file -->
<mapping resource="com/model/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Step 7: The
  applicationContext.xml configuration file is also created in the src directory. The

  specific 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"   
    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">
        <!-- id="userAction" The name of the instantiated object
			class="com.action.UserAction" The path of the instantiated object (package name class name)
			scope="prototype" non-singleton
			name="userService" The name of the property in the instantiated object
			ref="userService" 引用 -->
       <bean id="userAction" class="com.action.UserAction">
       
       	<property name="userService" ref="userService"/>
       </bean>
       <bean id="userService" class="com.userservice.UserServiceImpl">
       
       	<property name="userDao" ref="userDao"/>
       </bean>
        <bean id="userDao" class="com.dao.UserDaoImpl">
       
       	<property name="sessionFactory" ref="sessionFactory"/>
       </bean>
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
       </bean>
        
        </beans>

 

Guess you like

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