分类:
2008-10-17 14:26:40
Spring与struts整合主要有三种方式,通过Spring的ActionSupport类,通过Spring的DelegatingRequestProcessor类、通过Spring的DelegatingActionProxy类。
第一中方式: 通过Spring的ActionSupport类。方法是Action不在继承Struts提供的Action,而是继承于Spring提供的ActionSupport类。然后在Action中获得Spring的ApplicationContext。
1.新建一个登陆页面:loginActionSupport.jsp,代码如下
< contentType="text/html;charset=GBK" isELIgnored="false"%>
${msg}
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");
}
}
[1]