Preface
This document describes how to use Tomcat as the j2ee container and Sqlserver2005 as the database. Struts version 2.3.15.3 and Spring version 3.2.5
Spring Introduction
Spring is also an open-source project under appache. The powerful configuration management based on the Inversion of Control (IoC) Principle of JavaBeans makes the components of the application more convenient and easy. Of course, its functions include Aspect-Oriented Programming, JDBC support, and transaction management.
Obtain Spring
Spring official website http://www.springsource.org/, because of the revision of the official website, find it may be more troublesome, you can download from this website the required package: http://repo.springsource.org/release/org/springframework/spring/
Create StrutsSpringDemo
Introduce lib package
Decompress the spring-framework-3.2.5.release-dist.zip package downloaded from the official website, select the required jar package from lib, and introduce the struts jar package (for details about Struts construction, refer to the "StrutsDemo build" document ). Shows the required packages:
Web. xml configuration
Add the following content to web. xml:
ContextConfigLocation
/WEB-INF/applicationContext. xml/WEB-INF/spring-service.xml
Org. springframework. web. context. ContextLoaderListener
Struts2
Org. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter
Create applicationContext. xml
Create the applicationContext. xml file under the WEB-INF in the project. Pay attention to the JNDI configuration used in JDBC configuration here, you can change according to the specific situation, the content is as follows:
& Quot; 1.0 & quot; encoding =UTF-8"?>
Http://www.springframework.org/schema/beans"
Xmlns: xsi =Http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation =Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd"Default-autowire ="ByName">
"DataSource" class = "Org. springframework. jndi. JndiObjectFactoryBean">
"JndiName">
Java: comp/env/jdbc/ehrdb
"JdbcTemplate" class = "Org. springframework. jdbc. core. JdbcTemplate">
"DataSource">
"DataSource"/>
"SpringDao" class = "Org. apache. struts. helloworld. dao. SpringDao">
"JdbcTemplate">
"JdbcTemplate"/>
New spring-service.xml
Create a WEB-INF file under the spring-service.xml in the project with the following content:
& Quot; 1.0 & quot; encoding =UTF-8"?>
Http://www.springframework.org/schema/beans"
Xmlns: xsi =Http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation =Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd"Default-autowire ="ByName">
"SpringService" class = "Org. apache. struts. helloworld. service. SpringService"/>
Create struts. xml
Create a struts. xml file in the SRC directory. Note: To implement Spring-managed Struts, you must add The Code details are as follows:
& Quot; 1.0 & quot; encoding =UTF-8"?>
"-// ApacheSoftware Foundation // DTD Struts Configuration 2.0 // EN"
Http://struts.apache.org/dtds/struts-2.0.dtd>
"Struts. objectFactory" value = "Spring"/>
"Struts. devMode" value = "False"/>
"Basicstruts2" extends = "Struts-default">
"Index">
/Index. jsp
"Hello"
Class ="Org. apache. struts. helloworld. action. HelloWorldAction"Method ="Execute">
"Success"> /HelloWorld. jsp
Create SpringDao
Package org.Apache.Struts.Helloworld.Dao;
ImportOrg.Springframework.Jdbc.Core.JdbcTemplate;
Publicclass SpringDao{
Private JdbcTemplate jdbcTemplate;
Publicvoid query(){
String value=JdbcTemplate.QueryForObject("Select password from tb_manager where id = 1",String.Class);
System.Out.Println(Value);
}
Publicvoid setJdbcTemplate(JdbcTemplate jdbcTemplate){
This.JdbcTemplate=JdbcTemplate;
}
}
Create SpringService
Package org.Apache.Struts.Helloworld.Service;
ImportOrg.Apache.Struts.Helloworld.Dao.SpringDao;
Publicclass SpringService{
Private SpringDao springDao;
Publicvoid doSomething(){
System.Out.Println("SpringService doSomething ...");
SpringDao.Query();
}
Publicvoid setSpringDao(SpringDao springDao){
This.SpringDao=SpringDao;
}
}
Use Spring
Define private SpringService springService in action; and add the set Method to directly use service in action.
Deployment and running
Deploy the program to tomcat and access http: // localhost: 8080/StrutsSpringDemo to run
Demo
The above code is part of the core configuration, the complete demo is as follows: http://download.csdn.net/detail/zfz1214/6679927