免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1361 | 回复: 0
打印 上一主题 下一主题

Eclipse使用StrutsIDE开发struts [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-06 13:19 |只看该作者 |倒序浏览

Eclipse使用StrutsIDE开发struts
    在没有Struts经验之前,最好的办法是先建立一个Struts开发平台,先做出一个Hello world的小程序,然后再来研究它实现的原理。
功能说明
在一个jsp页面(HelloWorld.jsp)中,输入你的名字,通过struts 将你的名字加上Helloword字样,然后在另外一个jsp页面(ShowHelloWorld.jsp)显示出来。

一、安装StrutsIDE 1.1.8
同样,将tk.eclipse.plugin.struts_1.1.8.zip解压缩,然后拷贝到D:\eclipse目录下,覆盖所有的现有文件夹。
二、安装Eclipse HTML Editor 1.6.9
同样,将tk.eclipse.plugin.htmleditor_1.6.9.zip解压缩,然后拷贝到D:\eclipse目录下,覆盖所有的现有文件夹。

八、创建测试工程

如果已经完成了上面所有步骤,现在可以重新启动eclipse,使新安装的插件生效,开始正式开发了。
1、创建Dynamic Web Project工程:
File->new->others,打开新建向导对话框,在树中找到web->Dynamic Web Project,选中,点击next按钮。在projects name中输入HelloWorldStruts,点击finished。这时,我们在eclipse的package explorer中会看到新建的工程HelloWorldStruts,创建完成。

2.加入struts框架
File->new->others,打开新建向导对话框,找到Amateras->Struts->Add Struts Support,选中点击next按钮,选择 Web Application Root的路径为 /HelloStruts/WebContent 点击Finish按钮。这时,在eclipse的package explorer中会看到增加了很多struts的库文件,在WEB-INF下也增加了很多struts的配置文件。到此我们已经在项目加入了Struts框架。

3.写Helloword jsp页面
右击Hellowordstruts工程中的WebContent目录上->new ->other->jsp添加一个HelloWorld.jsp文件,在这个文件中,用户输入自己的用户名,然后提交,源代码清单如下:
Struts Hello world Demo
输入你的用户名,提交显示结果:

用户名称:
   

注意到第一行的:
这里到我们添加了struts的taglib的引用,struts标签库的使用,在后续的文章中将陆续介绍。

4.写struts 中hello word Action实现
在HelloWorldStruts/src目录下,在包中添加HelloWorldAction类,它继承自org.apache.struts.action.Action,并实现其execute方法。
其代码清单如下:
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.*;
import org.apache.struts.action.*;

public class HelloWorldAction extends Action {
    public ActionForward execute(ActionMapping actionMapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
     
        HelloWorldForm form = (HelloWorldForm) actionForm;
        ActionErrors errors = new ActionErrors();
        String username = form.getUsername();
        username += ",Hello world!";
        request.setAttribute("hello", username);      
        return actionMapping.findForward("success");
    }
}
5.写struts 中hello word ActionForm实现
在HelloWorldStruts/src目录下,添加HelloWorldForm类,它继承自org.apache.struts.action.ActionForm,在里面只有一个username属性,其代码清单如下:
import javax.servlet.http.*;
import org.apache.struts.action.*;

public class HelloWorldForm extends ActionForm {
    private static final long serialVersionUID = 3256445798169261619L;
    private String username;
    public HelloWorldForm() {
        username = null;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getUsername() {
        return this.username;
    }
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        username = null;
    }
}
6、写ShowHelloWorld.jsp页面
右击Hellowordstruts工程中的WebContent目录上->new ->other->jsp添加一个ShowHelloWorld.jsp文件,在这个文件中,显示输出结果,代码清单如下:
Show Hellow world
   
7、配置struts_config.xml文件
在WebContent/WEB-INF目录下修改struts-config.xml文件,添加HelloWorldAction和HelloWorldForm的映射,代码清单如下:

"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
   
        
   
   
        
            name="HelloWorldForm" scope="request" input="">
            
        
   
        

8、运行:
好了,所有的代码都已经编好了,在HelloWorld.jsp文件上->右键->run as -> run on server



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/29032/showart_227322.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP