Spring整合struts的示例
一、 准备工作
1、 创建工程
创建一个名为spring_struts_demo的Web工程,注意图1.1中,Context root URL为“/”。
图1.1
2、 引入struts框架
选中项目名称,右键—MyEeclipse—Add Struts Capabilities…具体如图1.2

图1.2
完成一起操作过后,就会弹出如图1.3的向导框。在Struts specification处,选择Struts1.2。将Base package for new classes处修改为com.wuya.sturts.。其实不变,单击完成按钮,完成Struts框架的引入工作。

图1.3
3、 增加需要的JAR包
当我们完成Struts框架的引入后,已经将一定的JAR包保存在了/WebRoot/WEB-INF/lib下如图1.4,我们仅仅有Struts的JAR还不够,因为我们的项目为Spring和Struts的整合,所以Spring的JAR包必不可少;还有就是我们在编写JSP页面的时候为了方便,我们使用JSTL标签。所以我们要增加JSTL必须的JAR包。当我们完成增加JAR的工作后的工程目录如图1.5
图1.4 图1.5
4、创建spring容器的ApplicationContext实例
创建spring容器的ApplicationContext实例方法较多,详细参见我的博客的一篇文章http://blog.csdn.net/wuxiaohong520/archive/2007/10/04/1811325.aspx中“spring整合struts时创建ApplicationContext的方法”。这里我就使用了其中一种较为简便的方法,就是在Struts-config.xml配置文件中,增加如下代码:
<plug-in
className="org.springframework.web.context.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in>
其中需要创建一个applicationContext.xml文件,存放路径/WEB-INF/下
4、 方便管理,新建文件夹
我们为了方便管理我们的项目,我在原有的目录中,增加了如下一些文件夹,蓝色选中的文件夹为我新建的,如图1.6。
图1.6
二、 创建页面
1、/tags/tag.jsp
创建此页面主要是为了方便其他页面引用标签库,所以创建了一个公共的标签库页面,如果其他页面需要引用标签库,那么只需通过%@include file=”/tags/tag.jsp”%此命令就可以了。
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@taglib uri="/WEB-INF/struts-nested.tld" prefix="nested"%>
<%@taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<%@taglib uri="/WEB-INF/c.tld" prefix="c"%>
2、/pages/login.jsp
创建的登录页面很简单,主要就是实现用户登录。其中使用了Struts标签库。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="/tags/tag.jsp"%>
<!DOCTYPE html PUBLIC "-//W 3C //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=UTF-8">
<title>login page</title>
</head>
<body>
<html:form action="/login.do" method="post">
<table>
<tr>
<td> 用户: </td><td><html:text property="name" /></td>
</tr>
<tr>
<td>密码: </td><td><html:password property="password" /></td>
</tr>
<tr>
<td><html:submit value="登录" /></td>
<td><html:reset value="重置" /></td>
</tr>
</table>
</html:form>
</body>
</html>
3、/pages/success.jsp
创建的登录成功页面也非常简单,主要显示用户登录成功后,显示用户姓名,显示模拟的图书信息。其中主要使用了JSTL标签库和EL表达式。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/tags/tag.jsp"%>
<!DOCTYPE html PUBLIC "-//W 3C //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=UTF-8">
<title>success page</title>
</head>
<body>
<center>
<h3>
登录成功,
<font color="red"><c:out value="${user.name }" /></font>欢迎你!
</h3>
</center>
<table align="center" border="1">
<tr>
<td> 书名</td><td>作者</td><td>出版社</td><td>价格</td>
</tr>
<c:forEach items="${bookList }" var="book">
<tr>
<td>${book.name }</td>
<td>${book.author }</td>
<td>${book.publisher }</td>
<td>${book.price }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
4、/pages/error.jsp
创建的失败页面相对简单,主要实现的功能就是用户登录失败后,返回此页面,此页面在自动刷新又重新回到登录页面,其主要代码就是:<meta http-equiv="refresh" content="2;URL=pages/login.jsp">
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W 3C //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=UTF-8">
<meta http-equiv="refresh" content="2;URL=pages/login.jsp">
<title>error page</title>
</head>
<body>
<center>
<h1>
登录失败!<font>2秒后自动返回到登录页面</font>
</h1>
</center>
</body>
</html>
三、 相关类的创建与编码
1、 com.wuya.struts.model.Book.java
此类是一个图书域对象,其他的也没有什么好多说的。
package com.wuya.struts.model;
public class Book {
private int id;
private String name;
private String author;
private String publisher;
private double price;
// setter and getter
}
2、 com.wuya.struts.service.BookManager.java接口
此类为业务层接口,本文重在Spring和Struts整合的方法和步骤,所以在业务接口中,只定义了一个方法。
package com.wuya.struts.service;
public interface BookManager {
public List getBook();
}
3、 com.wuya.struts.service.BookManagerImp实现接口类
此类是业务接口的实现类,其中的数据对象都是自己创建,其实在实际的项目中,数据对象都是都是从数据库中取出来,或者业务层只是调用Dao层。此处为了方便,所以自己创建了对象。
package com.wuya.struts.service;
public class BookManagerImp implements BookManager {
private List list = new ArrayList();
private Book book1 = new Book();
private Book book2 = new Book();
public List getBook() {
book1.setName("java");
book1.setAuthor("孙鑫");
book1.setPublisher("清华大学出版社");
book1.setPrice(100);
book2.setName("c++");
book2.setAuthor("潭浩强");
book2.setPublisher("电子工业出版社");
book2.setPrice(88);
list.add(book1);
list.add(book2);
return list;
}
}
4、 com.wuya.struts.form.LoginForm类
此类是一个ActionForm类,继承了ActionForm类。主要是用户数据的封装。
package com.wuya.struts.form;
public class LoginForm extends ActionForm {
private int id;
private String name;
private String password;
// setter and getter
}
5、 com.wuya.struts.action.LoginAction.java类
此类是Struts中的核心,也是与业务层打交道的地方。这里主要是对用户输入的信息进行判断,如果用户输入的信息正确,那么就到登录成功页面,并显示用户姓名和图书的信息。如果失败就到失败页面,失败页面自动刷新又返回到登录页面重新登录。
package com.wuya.struts.action;
public class LoginAction extends Action {
private BookManager manager = null;
private List list = new ArrayList();
public void setManager(BookManager manager) {
this.manager = manager;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginForm user = (LoginForm) form;
if (user.getName().equals("wuya") && user.getPassword().equals("wuya")) {
request.setAttribute("user", user);
list = manager.getBook();
request.setAttribute("bookList", list);
return mapping.findForward("success");
} else {
return mapping.findForward("error");
}
}
}
四、 修改web.xml配置文件
<welcome-file-list>
<welcome-file>pages/login.jsp</welcome-file>
</welcome-file-list>
五、 修改struts-config.xml配置文件
对于此配置文件其他的没有多说的,主要说说<action>中的type属性的值:org.springframework.web.struts.DelegatingActionProxy。这是spring提供的一个类。它在这里只是完成一个转发工作。其实struts的业务逻辑被分成了两部分,一部分是spring的DelegatingActionProxy,另一部分就是用户的Action实现类。DelegatingActionProxy接受ActionServlet转发过来的请求,然后转发给ApplicationContext管理的bean。用户实现的Action就在ApplicationContext管理的bean中定义的。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="loginForm"
type="com.wuya.struts.form.LoginForm">
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/login"
type="org.springframework.web.struts.DelegatingActionProxy"
name="loginForm">
<forward name="success" path="/pages/success.jsp" />
<forward name="error" path="/pages/error.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.wuya.struts.ApplicationResources" />
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in>
</struts-config>
六、 修改applicationContext.xml配置文件
将Action的实现类在这里注册,注意bean 中 name属性是与struts-config.xml文件中action中的path的值对应。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="/login" class="com.wuya.struts.action.LoginAction">
<property name="manager">
<ref bean="manager" />
</property>
</bean>
<bean name="manager" class="com.wuya.struts.service.BookManagerImp" />
</beans>
七、 运行
在地址栏输入127.0.0.1,显示登录页面,如图7.1。当输入wuya和wuya后,成功登录显示登录成功页面,如图7.2。如果登录失败,则显示失败页面,如图7.3。
图7.1
图7.2
图7.3
总结:由于我才接触spring不久,所以对此也不是很熟悉,如果在本文中有什么不妥或者错误之处,希望前辈指出。
Email:kevin_hong@yahoo.cn
QQ:16044171
本文详细介绍了如何在MyEclipse环境下,通过一系列步骤完成Spring与Struts的整合。包括创建Web工程,引入Struts框架,增加所需JAR包,配置ApplicationContext,创建页面如login.jsp、success.jsp和error.jsp,编写相关类如LoginForm、LoginAction等,以及修改web.xml、struts-config.xml和applicationContext.xml配置文件。最终实现用户登录验证并展示图书信息的功能。

116

被折叠的 条评论
为什么被折叠?



