本节主讲将struts与spring进行整合:
1,新建一个WEB工程:工程名为strutsspring,在其src目录上新建struts.xml配置文件
将struts相关的7个JAR包,拷至工程的WebRoot/Web-inf/lib目录下
2,配置 Web.xml文件,加上struts的过滤
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
经过以上两步配置,此工程已是标准的struts工程。
修改Tomcat/conf/server.xml配置文件,加上
<Context path="/strutsspring" docBase="E:\workspace\strutsspring\WebRoot" reloadable="true"/>
3,将spring相关的JAR包导入工程,将applicationContext.xml文件放入工程的web-inf目录下
4,将struts与spring整合:
将struts lib目录下的struts2-spring-plugin-2.0.14.jar包复制到工程中,此包中只有一个class文件。
修改web.xml文件,加入监听器,如下:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
启动tomcat服务器,启用完成。
例子:下面用一个登录的例子,结合struts与spring开发
登录页面login.jsp, 要求输入用户名与密码
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix ="s" uri="/struts-tags"%>
<html>
<head>
</head>
<body>
<s:form action="login">
<s:textfield name="username" size="20" label="username"></s:textfield>
<s:password name="password" size="20" label="password"></s:password>
<s:submit></s:submit>
</s:form>
</body>
</html>
定义一个接口,isLogin方法用于判断用户名与密码是否正确
package com.test.service;
public interface LoginService {
public boolean isLogin(String username,String password);
}
实现类,当username为hello,password为world时,返回true,否则返回false
package com.test.service.impl;
import com.test.service.LoginService;
/*
* 该类完成业务处理,向外暴露的是LoginService接口
*/
public class LoginServiceImpl implements LoginService {
public boolean isLogin(String username, String password) {
if("hello".equals(username) && "world".equals(password)){
return true;
}
return false;
}
}
action类中不处理业务逻辑,逻辑由LoginService 接口负责,其对象由spring负责生成
package com.test.action;
import com.opensymphony.xwork2.ActionSupport;
import com.test.service.LoginService;
public class LoginAction extends ActionSupport {
private String username;
private String password;
private LoginService loginService;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public LoginService getLoginService() {
return loginService;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
public void setPassword(String password) {
this.password = password;
}
//下面这个loginService由spring生成
public String execute() throws Exception {
if(loginService.isLogin(username, password)){
return "success";
}else{
return "error";
}
}
}
struts配置文件,此文件中不再负责生成action,改为由spring配置文件生成,class对应的值,必须对应spring配置文件中的bean id
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="strutsspring" extends="struts-default">
<!--下面这个class中的loginAction对应的是spring配置文件applicationContext.xml文件中声明的loginAction bean -->
<!--现在由spring负责生成action-->
<action name="login" class="loginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
spring配置文件,负责生成action及loginService对象
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="loginService" class="com.test.service.impl.LoginServiceImpl" scope="singleton"></bean>
<bean id="loginAction" class="com.test.action.LoginAction" scope="prototype">
<property name="loginService" ref="loginService"></property>
</bean>
</beans>
其中scope配置是重点!其范围如下图所示
一般无状态类应使用singleton,而action则建议使用request或prototype
无状态类是指只有方法,没有属性的类。
分享到:
相关推荐
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...
JAVA WEB整合开发实例精通:Struts+Hibernate+Spring
以国内使用最广泛的Struts为例,其新版本Struts2与前一版Struts1.x已经有了天壤之别。Hibernate也从2.0升级为3.X,近期Spring也最新推出了2.5版。读者如果再参考以前的版本书籍学习,无异于南辕北辙,而《JavaWeb...
以下章节源码[03] [04] [05] [08] [10] [11] [12] [13] [14] [15] [16]
希望此套框架整合对深入了解和学校Struts2+Spring4+Hibernate4三大框架整合技术有帮助
介绍了Java EE领域的三个开源框架:Struts 2、Spring和Hibernate。 这是第三部分,我上传的上限是60MB,所以不得不分包额!
比较流行的一本书,在公司内很受欢迎 比较实用 下半部分,需与上半部分放在一起解压。
1. 导入的包 <br>l struts2的五个基础包:commons-logging-1.1.jar; <br>freemarker-2.3.8.jar; <br>ognl-2.6.9.jar; <br>struts-core-2.0.6.jar; <br>xwork-2.0.0.jar. <br>l spring的核心包:...
spring与struts2整合的小示例,适合初学者,具体说明在博客里
学习Spring Struts Hibernate 整合开发必看的书
轻量级 J2EE 企业应用实战:Struts+Spring+Hibernate 整合开发
MyClipse6.0\Struts2,Spring与Hibernate整合应用,学生成绩管理系统
J2EE 企业应用实战:Struts Spring Hibernate 整合开发.part2
J2EE 企业应用实战:Struts Spring Hibernate 整合开发
J2EE 企业应用实战:Struts Spring Hibernate 整合开发 文件过大``分2部分 此为Part2
收集的Java Web整合开发实战:基于Struts 2+Hibernate+Spring-源代码,看到其他人下载币要的太多,给大家分享一下。 不是很全,但可以拿来参考了。 由于大小限制,还有另外一个包······
只使用spring和struts进行整合
Spring整合集成Struts1.2最简单例子