捷凌网安JAVA频道
捷凌IT教育
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME 开源技术 其他技术 论坛
 您现在的位置: 捷凌网安 >> Java >> 开源技术 >> Spring >> 正文
Spring与struts整合开发实例(一)

作者:佚名 责任编辑:左决 点击数: 更新时间:2008-4-30 4:02:13

   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");

    }

}

[1] [2] [3] 下一页

  • 上一篇文章:

  • 下一篇文章:
  •  
     最进更新
    普通文章J2SE实现windows读取网卡的物05-11
    普通文章Scala—Java的避难所之main(05-11
    普通文章Spring数据源的灵活配置巧应05-11
    普通文章Spring 与 Log4J 进行动态日05-11
    普通文章Heritrix的多线程ToeThread和05-11
    普通文章应用spring示例开发网站构思05-11
    普通文章Spring 数据源配置与应用05-11
    普通文章Spring中的四种声明式事务的05-11
    普通文章Hibernate+Spring搞定Clob、05-11
    普通文章Hibernate的映射关联关系05-11
     
     推荐文章
    推荐文章Java技术开源搜索引擎04-30
    推荐文章Eclipse中建立自己的JUnit测04-30
    推荐文章Eclipse 3.3上安装jadclipse04-30
    推荐文章spring 编程入门十大问题解答04-30
    推荐文章Java编程中Spring的一些负面04-30
    推荐文章Java应用中Hibernate对多表关04-30
    推荐文章关于hibernate的缓存和CRUD04-30
    推荐文章基于Struts1.2的动态多文件上04-30
    推荐文章演示Struts2实现简单上传代码04-30
    推荐文章J2EE学习笔记--Struts初步认04-30
     
     热点文章
    普通文章J2SE实现windows读取网卡的物05-11
    普通文章Scala—Java的避难所之main(05-11
    普通文章Spring数据源的灵活配置巧应05-11
    普通文章Spring 与 Log4J 进行动态日05-11
    普通文章Heritrix的多线程ToeThread和05-11
    普通文章应用spring示例开发网站构思05-11
    普通文章Spring 数据源配置与应用05-11
    普通文章Spring中的四种声明式事务的05-11
    普通文章Hibernate+Spring搞定Clob、05-11
    普通文章Hibernate的映射关联关系05-11

    | 设为首页 | 加入收藏 | 联系站长 | 广告服务 | 友情链接 | 版权申明 | 网站地图 |

    在线交流 捷凌网安主群:51649627
    Copyright 2007-2008 © 捷凌网安. All rights reserved.
    备案序号:蜀ICP备08001812号