接上篇文章(Struts2.x+Spring3.x+Hibernate4.x 整合(一)Struts环境搭建)中的代码,向项目中整合Spring3.0.0框架。
注:项目源码附在文章下方。
1、下载Spring
到官方网站下载Spring3.0框架, 这里我已压缩一份:http://download.csdn.net/detail/xz2001/4844494。
另外, 还需要下载两个包:com.springsource.org.apache.commons.logging-1.1.1.jar 和 struts2-spring-plugin-2.3.7.jar,地址在:http://download.csdn.net/detail/xz2001/4844534
最后lib目录包含如下jar包:
2、修改web.xml配置文件,增加spring配置,修改后内容如下:
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
3、在WEB-INF目录中增加applicationContext.xml配置文件,内容如下:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
4、修改struts配置文件,修改后内容如下:
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
5、修改Action文件,为spring注入准备:
package com.manage.enter.action;
import com.manage.enter.service.EnterService;
import com.manage.enter.service.impl.EnterServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
public class EnterAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private EnterService service;
private String username;
private String password;
public EnterService getService() {
return service;
}
public void setService(EnterService service) {
this.service = service;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception {
return "login";
}
public String login() throws Exception {
service = new EnterServiceImpl();
return service.load(this.username, this.password) ? "success" : "error";
}
}
6、测试访问
启动Tomcat,访问:http://localhost:8080/ssh/manage/enter.do
最后附上完成后的代码:http://download.csdn.net/detail/xz2001/4844476