打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
struts-2.1.8.1与spring2.5.6的整合
一 说明
1)开发环境
eclipse _javaEE_3.6(Helios)
jdk1.6.21
tomcat6.0.29
2)整合原因
通过spring来组织管理struts2,是结构更清晰,配置更简单。在整合了2个框架以后,用法与独立使用稍微有些不同,但原理是一样的。笔者会在下面的文章中给出来。
3)笔者的项目结构,其中log4j为可选配置,不会影响spring和struts2的整合

二 struts2.1.8.1的环境搭建
1)导入包
此时就是搭建个纯的struts2环境,与spring无关。创建个WEB项目,拷贝以下包到 /WEB-INF/lib下:以下部分包,笔者进行了相关更新,可能版本号跟你的不太一样。
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.1.1.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
2)配置struts.xml
复制在Struts目录的例子程序中WEB-INF\classes\struts.xml文件,粘贴到项目的src目录下,主要保留其文件头:
<?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="default" namespace="/" extends="struts-default">
</package>
</struts>
3)配置web.xml
在<web-app>节点下,加入如下配置
<filter>
  <filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
三 Spring2.5.6环境搭建
1)导入包
可能有些包会与struts2中的重复,下面只是为了说明如何配置纯spring环境
spring.jar
log4j-1.2.16.jar
commons-logging-1.1.1.jar
2)配置applicationContext-*.xml
在WEB-INF目录下建立三个文件
   applicationContext-actions.xml
   applicationContext-beans.xml
   applicationContext-common.xml
   这三个文件其实是applicationContext.xml的分解,为的是避免所有配置放在同一文件,造成混乱。3个文件结构均如下:
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
</beans>
3)配置web.xml
在<web-app>节点下,加入如下配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
4)配置log4j(可选)
此处配置与否,不会影响spring的正常使用。要知道如何配置,请查看笔者另一篇博客文章。
四 整合spring2.5.6和struts2.1.8.1
至此,spring和struts2可以独立的使用。下面我们将2个部分整合到一起,让spring来组织管理struts2.在导入下面的包以后,整合工作至此完成。
struts2-spring-plugin-2.1.8.1.jar
五 测试spring和sturts2框架是否可用
在导入了上述包以后,spring已经就可以管理struts了。下面我们来测试一下。逻辑很简单,就是验证数据的密码是否正确。当用户名和密码均为phl时正确,其他情况下错误。配置完下面的信息以后,访问index.jsp亲自体验一下吧。
1)编写3个JSP文件。error.jsp,hello.jsp,index.jsp
error.jsp
<%@ page language="java" contentType="text/html; charset=utf8"
    pageEncoding="utf8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>Insert title here</title>
</head>
<body>
输入内容有错误.
</body>
</html>

hello.jsp
<%@ page language="java" contentType="text/html; charset=utf8"
    pageEncoding="utf8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>Insert title here</title>
</head>
<body>
消息:<s:property value="msg"/><br>
用户名:${username}<br>
密码:${password}<br>
</body>
</html>

index.jsp
<%@ page language="java" contentType="text/html; charset=utf8"
    pageEncoding="utf8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>Insert title here</title>
</head>
<body>
<a href="hello.action">跳转-实验struts2,显示msg默认值</a>
<form action="hello.action">
<!-- 下面的元素中,name要与action中的相同,这样他才能自动为我们注入 -->
信息<input type="text" name="msg"><br/>
用户名<input type="text"  name="username" /><br/>
密码<input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
2)编写2个类,一个为action,一个为业务逻辑类。HelloAction.java,Manager.java
HelloAction.java
package com.huaxin.phl;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {

private String msg = "Hello World! this is my first struts2!";
private Manager loginManager;
private String username;
private String password;

public String execute() throws Exception {
if (loginManager!=null && loginManager.isLogin(username, password)) {
return "success";
} else {
return "error";
}
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public Manager getLoginManager() {
return loginManager;
}

public void setLoginManager(Manager loginManager) {
this.loginManager = loginManager;
}

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;
}

}

Manager.java
package com.huaxin.phl;

public class Manager {
public boolean isLogin(String username, String password) {
if (null != username && null != password) {
if ("phl".equals(username.trim()) && "phl".equals(password.trim())) {
return true;
}
}
return false;
}
}

3)配置属性文件
A:在applicationContext-beans.xml配置业务逻辑类
<bean id="lm" class="com.huaxin.phl.Manager"></bean>
B:在applicationContext-actions.xml配置action,让spring来管理action,并为我们自动注入业务逻辑类
<bean id="ha" class="com.huaxin.phl.HelloAction" scope="prototype">
<property name="loginManager" ref="lm"></property>
</bean>
C:配制struts.xml
<?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="default" namespace="/" extends="struts-default">
<!--
注意看此处配置的class,其中并没有包含类的包路径com.huaxin.phl.HelloAction
而是直接使用了spring中,配置的bean做为了class
这就说明spring与struts整合成功,action中的类,由spring成功接管
-->

<action name="hello" class="ha">
<result name="success">hello.jsp</result>
<result name="error">error.jsp</result>
</action>
<!--  下面是没有整合spring,纯struts环境下,使用的配置,此时不需要配置applicationContext-*.xml,因为那是spring的配置文件
<action name="hello" class="com.huaxin.phl.HelloAction">
<result name="success">
/hello.jsp
</result>
<result name="error">
/error.jsp
</result>
</action>
-->
</package>
</struts>  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
struts2.0简单的例子
SpringMVC+Hibernate+Spring 简单的一个整合实例
Struts2.1.6与Spring2.5.6框架整合
struts2整合spring应用实例
SSH框架搭建详细图文教程
Eclipse开发struts完全指南
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服