6.spring+struts+hibernat Annotation Method Integration

Source: Internet
Author: User

SSH Annotation Method implementation

1. Create the Db.properties file, mainly the connection data of the database

Jdbc.driverclassname=com.mysql.jdbc.driver
Jdbc.url=jdbc\:mysql\://localhost\:3306/test
Jdbc.username=root
jdbc.password=076634

2. Create a struts configuration file that primarily defines the general configuration of struts

<?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" >

<!--the general configuration of struts-
<struts>

<constant name= "Struts.devmode" value= "true"/>
<constant name= "Struts.enable.DynamicMethodInvocation" value= "true"/>
<constant name= "struts.objectfactory" value= "Spring"/>

</struts>


3. Defined configuration file

<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx"
xmlns:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd "default-autowire=" ByName ">

<!--auto-Scan Package--
<context:component-scan base-package= "Cn.yunhe"/>

<!--allow things to be annotated-
<tx:annotation-driven/>

<!--read properties--> of definitions
<bean class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "Locations" >
<value>classpath:db.properties</value>
</property>
</bean>

<!--payees configuration file to build a connection pool--
<bean id= "DataSource" destroy-method= "Close" class= "Org.apache.commons.dbcp.BasicDataSource" >
<property name= "Driverclassname" value= "${jdbc.driverclassname}"/>
<property name= "url" value= "${jdbc.url}"/>
<property name= "username" value= "${jdbc.username}"/>
<property name= "Password" value= "${jdbc.password}"/>
</bean>

<!--Configure sessionfactory, scan entity, and configure Hibernate-related configuration, be sure to use the Class-annotation package
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name= "DataSource" ref= "DataSource"/>
<!--scan entity class--
<property name= "Packagestoscan" >
<value>cn.yunhe.model</value>
</property>

<!--hibernate parameter configuration--
<property name= "Hibernateproperties" >
<value>
Hibernate.dialect=org.hibernate.dialect.mysqldialect
Hibernate.hbm2ddl.auto=update
Hibernate.show_sql=true
</value>
</property>
</bean>

<!--initialize Things--

<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Sessionfactory"/>
</bean>
</beans>


5.web.xml Layer


<! DOCTYPE Web-app Public
"-//sun Microsystems, INC.//DTD Web application 2.3//en"
"Http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>archetype Created Web application</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>

<!--struts Filters--
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<!--spring Listener to create a spring container when the system is started--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

6. Operation of the in-package class

1: Annotation of entity classes
@Entity
@Table (name = "User")
public class User {....


Primary key:
@Id
@GeneratedValue
@Column (name = "id")
public int getId () {
return ID;
}
Other:
@Column (name = "PWD")


2:dao Layer:


@Repository ("Userdao")
public class Userdaoimpl extends Hibernatedaosupport implements Userdao {

3:biz Layer:

@Service ("Userbiz")
public class Userbizimpl implements Userbiz {
Introducing DAO
@Resource (name = "Userdao")
Private Userdao Userdao;

4:struts Controller


Create a controller for struts

@Controller ("User")
@Scope (value = "Prototype")
@ParentPackage (value = "Struts-default")
public class Usercontroller extends Actionsupport {

Create Biz
@Resource (name = "Userbiz")
Private Userbiz userbiz;

@Action (value = "user", results = {
@Result (name = "Success", location = "/list.jsp")
})
Public String Execute () throws Exception {


list<user> list = Userbiz.searchall ();
Create a stored Actioncontext
Map request = (map) actioncontext.getcontext (). Get ("request");
Request.put ("list", list);
return SUCCESS;
}

Public userbiz getuserbiz () {
return userbiz;
}

public void Setuserbiz (Userbiz userbiz) {
This.userbiz = userbiz;
}
}

6.spring+struts+hibernat Annotation Method Integration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.