s2sh integration (detailed version)

For details, please click: http://www.verydemo.com/demo_c143_i24269.html

 

Create project s2sh

 

 

Add Hibernate 3.2



By default, don't forget to add the jar package to /WebRoot/WEB-INF/lib and

then next

 

In fact, the hibernate.cfg.xml here does not work, because the content of the configuration file is managed by the subsequent spring configuration file, we can delete it after creation.
By default


, we don't need hibernate.cfg.xml at all, so there is no need for hibernate.cfg.xml here. No database configuration is required, and of course the default does not matter.
continue next


We will also use the sessionFactory provided by spring directly in the future, so there is no need to create it here.
finish 

Add Spring2.0

 



Don't forget to check these 5 libraries:
Spring 2.0 AOP Libraries
Spring 2.0 Core Libraries
Spring 2.0 Persistence Core Libraries
Spring 2.0 Persistence JDBC Libraries
Spring 2.0 Web Libraries
Also don't forget to add the jar package to /WebRoot/WEB-INF/lib.
Next



, we don't need Enable AOP, so we don't need to select it.
Don't forget to specify the configuration file applicationContext.xml in the /WEB-INF directory, otherwise the sessionFactory in
next cannot be loaded when the server starts,



and we don't need it, and we will configure it manually later.
finish

 

Add Struts2

Since MyEclipse does not support Struts2, we need to manually import the relevant jar packages.
The required related jar packages are as follows:
freemarker-2.3.8.jar ognl
-2.6.11.jar
struts2-core-2.0.11.jar
xwork-2.0.4.jar
struts2-spring-plugin-2.0.11.jar

struts.xml

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<?xmlversion="1.0" encoding="UTF-8" ?>

<!DOCTYPEstruts PUBLIC

    "-//Apache Software Foundation//DTDStruts Configuration 2.0//EN"

   "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="s2sh"extends="struts-default">

</package>

</struts>

web.xml

 

 

   

   

   

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

org.springframework.web.context.ContextLoaderListener  

 

 

<?xml version="1.0"encoding="UTF-8"?>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

So far our integration work is complete. We can package the project for direct use next time.
There is a problem here, that is, if myeclipse is used to load the lib package of hibernate and spring to publish the project, there will be an exception, we can do it.


test

sql

(1) Use mysql,
create an s2sh database, and create a table person

CREATE TABLE `person` (  

`id` int(11)  ,  

`name` varchar(20)  ,  

`age` int(11)  ,  

PRIMARY KEY  (`id`)  

);  

--Table "person" DDL

CREATETABLE `person` (

  `id` int(11) NOT NULL,

  `name` varchar(20) NOT NULL,

  `age` int(11) NOT NULL,

  PRIMARY KEY (`id`)

)ENGINE=InnoDB DEFAULT CHARSET=utf8;


(2) Create a package, create the Entity Bean corresponding to the person table and its mapping file

Person.java

 

 

 

 

 

 

 

}

 

 

}

 

 

}

 

 

}

 

 

}

 

 

}

 

 

 

packagetest.s2sh.bean;

 

publicclass Person {

private Integer id;

private String name;

private int age;

public Integer getId() {

  return id;

}

public void setId(Integer id) {

  this.id = id;

}

public String getName() {

  return name;

}

public void setName(String name) {

  this.name = name;

}

public int getAge() {

  return age;

}

public void setAge(int age) {

  this.age = age;

}

}

 

 

Person.hbm.xml

 

  

   

 

 

 

 

 

 

 

          

 

 

 

 

sessionFactory creation method<?xml version="1.0"encoding="utf-8"?>

<!DOCTYPEhibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping>

<classname="test.s2sh.bean.Person" table="person">

  <id name="id"type="java.lang.Integer" column="id">

    <generatorclass="increment"></generator>

  </id>

  <property name="name"type="string" column="name"

   length="20"></property>

   <property name="age"type="java.lang.Integer" column="age"></property>

</class>

</hibernate-mapping>

 

(3) Configure hibernate's connection database information and sessionFactory creation method in the spring configuration file applicationContext.xml

 

 

   

   

 

 

      

 

 

          

 

 

   

 

   

 

   

 

   

 

 

 

 

      

 

   

 

 

 

 

 

 

 

 

   

 

 

 

 

 

 

 

<?xmlversion="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<!--apache.dbcp connection pool configuration-->

<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">

  <property name="driverClassName"value="com.mysql.jdbc.Driver"></property>

  <property name="url"value="jdbc:mysql://localhost:3306/s2sh?useUnicode=true&characterEncoding=UTF-8"></property>

  <property name="username"value="root"></property>

  <property name="password"value=""></property>

  <!-- Maximum Active Connections-->

  <property name="maxActive"value="100"></property>

  <!-- Maximum number of idle connections-->

  <property name="maxIdle"value="30"></property>

  <!-- Maximum waitable connections-->

  <property name="maxWait"value="500"></property>

  <!-- The default submission method (if the transaction is not required, it can be set to true, in practical applications, it is generally set to false, and the default is false) -->

  <property name="defaultAutoCommit"value="true"></property>

</bean>

<!--The sessionFactory supported by spring for hibernate3 is directly used here -->

<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  <property name="dataSource"ref="dataSource"></property>

  <!-- hibernate dialect and other related configuration-->

  <propertyname="hibernateProperties">

   <props>

     <propkey="connection.useUnicode">true</prop>

     <propkey="connection.characterEncoding">utf-8</prop>

     <propkey="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

     <propkey="hibernate.show_sql">true</prop>

   </props>

  </property>

  <!-- The mapping file of hbm.xml -->

  <propertyname="mappingResources">

   <list>

     <value>test/s2sh/bean/Person.hbm.xml</value>

   </list>

  </property>

</bean>

 

</beans>

Garbled problem solution

1、

 

 

 

 

 

 

 

  

<propertyname="hibernateProperties">

  <props>

    <propkey="connection.useUnicode">true</prop>

    <propkey="connection.characterEncoding">utf-8</prop>

    <propkey="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

    <propkey="hibernate.show_sql">true</prop>

  </props>

</property>


2、

 

useUnicode=true&characterEncoding=UTF-8"<propertyname="url"value="jdbc:mysql://localhost:3306/s2sh?useUnicode=true&characterEncoding=UTF-8"></property>




(4) Next, we start to write the DAO layer. First, create the com.s2sh.dao package to place the interface of the Dao class, and create the com.s2sh.dao.impl package to place the implementation of the Dao class.

PersonDAO.java

 

 

 

 

 

 

Person findPersonById(Integer id);

List<Person> findAllPerson();

 

 

packagetest.s2sh.dao;

 

importjava.util.List;

 

importtest.s2sh.bean.Person;

 

publicinterface PersonDAO {

void savePerson(Person p);

void removePerson(Person p);

Person findPersonById(Integer id);

List<Person> findAllPersons();

void updatePerson(Person p);

}


Since spring is used, the implementation class of DAO can inherit HibernateDaoSupport for implementation

PersonDAOImpl.java

 

 

 

 

 

 

 

String sql =

 

}

 

Person p = (Person)

 

}

 

 

}

 

 

}

 

 

}

 

 

(5) Let's start the design of the service layer. The service layer here is only a method of simply calling the DAO layer.
The same is to create an interface package (com.s2sh.service), an implementation package and an interface class (com.s2sh.service.impl), and an implementation class

PersonService.java

 

 

 

 

List<Person> findAll();

 

 

Person findById(Integer id);

 

 

packagetest.s2sh.service;

 

importjava.util.List;

 

importtest.s2sh.bean.Person;

 

publicinterface PersonService {

List<Person> findAll();

void save(Person p);

void delete(Person p);

Person findById(Integer id);

void update(Person p);

}

 

 

Here you need to use spring's ioc to add DAO to the service, so you need to add DAO as an attribute to the service implementation class.

PersonServiceImpl.java

 

 

 

 

 

 

 

 

 

}

 

 

}

 

 

}

 

 

}

 

 

}

 

 

}

 

 

}

 


(6) Next, start to set the action layer. There may be many actions in the actual project, so the action needs to be subcontracted. Here, the operation is performed on the Person, and com.s2sh.action.person is created.
Here, the save operation is performed first .

savePersonAction.java

 

 

 

 

 

 

 

 

 

}

 

 

}

 

 

}

 

 

}

 

 

 

}

 

 

 

}

}

 

 

save.jsp

 

 

 

 

 

 

 

 

 

""

 

 

 

 

 

 

 

 

 

       

       

       

   

 

 

<%@page language="java" import="java.util.*"pageEncoding="utf-8"%>

<%@taglib prefix="s" uri="/struts-tags" %>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

   

    <title>Save Person</title>

 

  </head>

 

  <body>

    <s:formaction="savePerson">

      <s:textfieldname="p.name" label="name"></s:textfield>

      <s:textfieldname="p.age" label="age"></s:textfield>

      <s:submit></s:submit>

    </s:form>

  </body>

</html>

 

Operation success page

saveSuccess.jsp

 

>

>

>

>

>

 

>

 

>

<%@page language="java" import="java.util.*"pageEncoding="utf-8"%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>Save OK</title>

  </head>

 

  <body>

    Saved successfully<br>

  </body>

</html>

Configure struts.xml

 

 

 

 

 

<actionname="savePerson" class="savePersonAction">

....................................................................

  <resultname="input">/save.jsp</result>

</action>

 

 

 

Since the creation of the current action has been taken over by spring, the alias used in the class here corresponds to the id in applicationContext.xml.

Configure applicationContext's ioc

 

      

   

 

 

 

 

 

 

 

 

      

 

 

 

<beanid="personDAO" class="test.s2sh.dao.impl.PersonDAOImpl"scope="singleton">

  <!-- Since this class inherits from HibernateDaoSupport, sessionFactory needs to be injected -->

  <property name="sessionFactory">

   <ref bean="sessionFactory"/>

  </property>

</bean>

<beanid="personService" class="test.s2sh.service.impl.PersonServiceImpl">

  <property name="personDAO"ref="personDAO"></property>

</bean>

<beanid="savePersonAction"class="test.s2sh.action.person.SavePersonAction"scope="prototype">

  <property name="service"ref="personService"></property>

</bean>

 

 

Here is a description of the difference between the singleton in spring and the singleton in the design pattern. The singleton in the design pattern generates only one instance in the jvm, while the singleton in spring
is for each ioc container. If there are two ioc container, then each ioc container will generate a unique class
instance and inject sessionFactory into Dao. Dao here is stateless and can be set as a singleton. The action here is created using spring.
The action of struts2 is different from that of struts1, because there may be attributes in the action in struts2, here is Person, so the action of struts2 may be stateful.
If it is stateful, we need to set the scope to prototype mode, that is, create a new action for each request.
Otherwise, it may occur that if the form is submitted with an error for the first time, and then it cannot be submitted again. The value should be the default singleton.
When the first time, an error is added to FieldError, which is always this. action always carries this error, so it cannot be submitted.

So far this integration has been completed.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326560817&siteId=291194637