Spring与struts整合主要有三种方式,通过Spring的ActionSupport类,通过Spring的DelegatingRequestProcessor类、通过Spring的DelegatingActionProxy类。
第一中方式: 通过Spring的ActionSupport类。方法是Action不在继承Struts提供的Action,而是继承于Spring提供的ActionSupport类。然后在Action中获得Spring的ApplicationContext。
1. 新建一个登陆页面:loginActionSupport.jsp,代码如下
<%@page contentType="text/html;charset=GBK" isELIgnored="false"%>
<html>
<head><title>实现用户登录实例,struts和Spring整合</title></head>
<body>
<font size=’22’> ${msg}<br> </font>
<form name="form1" action="/myLogin/loginActionSupport.do" method="post">
用户名:<input type="text" name="username" value="${user.username}"/><br>
密码:<input type="password" name="password" value="${user.password}"/><br>
<input type="submit" name=”method” value="提交"/>
</form>
</body>
</html>
2. 创建一个存储登陆用户信息的类:User.java该类继承于ActionForm,代码如下:
package com.zhaosoft.bean;
import org.apache.struts.action.ActionForm;
public class User extends ActionForm {
private String username=null;
private String password=null;
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;
}
}
3. Com.zhaosoft.action中新建一个LoginActionSupport.java,该类不继承于struts的Action,而是继承于Spring的ActionSupport,代码示例如下:
package com.zhaosoft.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.web.struts.ActionSupport;
import com.zhaosoft.bean.User;
import com.zhaosoft.domain.Login;
public class LoginActionSupport extends ActionSupport {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 通过ApplicationContext获取配置文件
ApplicationContext ctx = getWebApplicationContext();
Login login = (Login) ctx.getBean("login");
login.login((User) form);
request.setAttribute("msg", login.getMsg());
request.setAttribute("user", (User) form);
return mapping.findForward("login");
}
}
4. 新建包:com.zhaosoft.domain和com.zhaosoft.domain.impl在包com.zhaosoft.domain中新建一个接口:Login.java用来定义登陆的逻辑处理,示例代码如下:
package com.zhaosoft.domain;
import com.zhaosoft.bean.User;
public interface Login {
public abstract void login(User user);
public abstract String getMsg();
}
5. 在包com.zhaosoft.domain.impl中建立LoginImpl.java用来实现登陆的逻辑处理
package com.zhaosoft.domain.impl;
import com.zhaosoft.bean.User;
import com.zhaosoft.domain.Login;
public class LoginImpl implements Login {
private String msg;
public String getMsg() {
return this.msg;
}
public void login(User user) {
String username = user.getUsername();
String password = user.getPassword();
if (username == null && password == null) {
msg = "请输入用户名和密码";
} else if ("".equals(username) || "".equals(password)) {
msg = "请输入用户名和密码";
} else if ("zhaosoft".equals(username) && "zhaosoft".equals(password)) {
msg = "登陆成功";
} else if (!"zhaosoft".equals(username)) {
msg = "用户名填写错误";
}
}
}
6.修改struts的配置文件struts-config.xml,注册ContextLoaderPlugIn插件,实例代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!--定义formbean-->
<form-beans>
<form-bean name="user" type="com.zhaosoft.bean.User"></form-bean>
</form-beans>
<action-mappings>
<action path="/loginActionSupport"type="com.zhaosoft.action.LoginActionSupport" name="user">
<forward name="login" path="/jsp/loginActionSupport.jsp"></forward>
</action>
<action path="/loginActionSupportTemp"type="org.apache.struts.actions.ForwardAction"parameter="/jsp/loginActionSupport.jsp"></action>
</action-mappings>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/spring-config.xml" />
</plug-in>
</struts-config>
7. 编写Spring的配置文件spring-config.xml。放在WEB-INF目录中,代码如下:
<?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="login" class="com.zhaosoft.domain.impl.LoginImpl">
</bean>
</beans>
8.web.xml中的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>actionServlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<!--初始参数-->
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--处理所有后缀为do的请求-->
<servlet-mapping>
<servlet-name>actionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
完整的代码实例下载:http://d.download.csdn.net/down/422185/zxl315