惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - .net

软件项目获取用户需求的沟通技巧(摘自IT168技术频道) 软件项目进度控制要处理好四个问题(摘自IT168技术频道) 软件外包项目实施过程中的关键因素(摘自IT168技术频道) Struts学习心得之Struts流程篇(3) -示例 转载 Struts学习心得之Struts流程篇(2) 转载 Struts学习心得之Struts流程篇(1)转载 初学Java Applet程序设计基础 将一个数据库添加到服务器中 datagrid中使用dropdownlist编辑模版时遇到问题 开发中的临时内容 开发个人财务管理系统(一)建数据库 SQL查询语句使用方法参考二 SQL查询语句使用方法参考一 用RichTextBox控件来做一个文本编辑器 微软的一项Agent技术(动画小精灵) - .net .NET中如何获得IP地址和主机名 response.bufferoutput cookie的使用 测试cookie关键字是否存在
Struts学习心得之Struts流程篇(3) -示例 续 转载
.net · 2005-12-20 · via 博客园 - .net
    

第二步:实现controller。在Struts中继承自Action。调用Model,实现数据的深层次检验(email是否存在)和数据的插入,程序的跳转等。代码如下:

SignAction.java

 1/**
 2 * @author han
 3 * soochow university
 4 * 实现Action,controller
 5 */

 6public class SignAction extends Action {
 7    
 8     public ActionForward execute(ActionMapping mapping, ActionForm form,
 9                HttpServletRequest request,HttpServletResponse response
10            ) throws Exception{    
11         
12         /*  参数说明:
13          *  ActionMapping 实现跳转,控制页面
14          *  ActionForm 在viewer中实现的bean,继承自ActionForm,获得form中的数据
15          *  HttpServletRequest   request
16          *  HttpServeletRsponse  response
17          */

18         
19         ActionErrors errors = new ActionErrors();  //错误处理,详细信息见本blog的《Struts错误处理》
20         
21         /*
22          *  得到form中的数据,因为form中的数据在SignForm中,做一类型转换即可;
23          *  这个要在struts-config.xml中进行说明
24          */

25         SignForm sf = (SignForm) form;
26         
27         /*
28          *  调用Model业务逻辑。
29          */

30         SignModel sign = new SignModel();
31         
32         sign.setEmail(sf.getEmail());
33         sign.setUserid(sf.getUserid());
34         
35         /*
36          *  调用Model的findUserByEmail 和 findUserByUserid 判断
37          *  email和userid是否存在。
38          *  如果存在,则添加出错信息,页面跳转到mapping.getInputforward(),需要在
39          *  struts-config.xml中定义。
40          *  关于错误的详细信息请参看本blog的《struts错误处理》文章。
41          */

42         //email existed
43         if (sign.findUserByEmail()){
44             errors.add("email.existed",new ActionError("email.existed", sign.getEmail()));
45             this.saveErrors(request, errors);
46             return mapping.getInputForward();             
47         }

48     
49         //userid existed
50         if (sign.findUserByUserid()){
51             errors.add("userid.existed"new ActionError("userid.existed",sf.getUserid()));
52             this.saveErrors(request, errors);
53             return mapping.getInputForward();
54         }

55         
56         /*
57          * 调用Model的sendPassword将系统生成的密码发送到用户信箱。
58          * 如果发生错误转到mapping.getInputForward(),这个需要在struts-config.xml中定义
59          * 详细信息见后面的struts-config.xml文件说明
60          */

61         if (sign.sendPassword()){
62             sign.saveNewUser();
63         }
else{  //邮件发送错误
64             errors.add("email.send.error"new ActionError("email.send.error", sf.getEmail()));
65             this.saveErrors(request, errors);
66             return mapping.getInputForward();
67         }

68                  
69        /*
70         *     如果注册成功,页面跳转到mapping.findForward("home")
71         *  这个需要在struts-config.xml中定义
72         *  详细信息见后面的struts-config.xml文件说明
73         */

74         return mapping.findForward("home");
75         
76         
77     }

78}

说明:
           1、SignAction实现MVC中的controller,通过mapping参数来实现跳转,在controller中调用了Model中的一些操作,根据操作结果来实现跳转。
            2、程序中包括了Struts的错误处理(请见本blog的《struts错误处理》)、资源文件的使用,并且好多处代码都和struts的配置文件struts-config.xml有联系
            3、关键代码处都有详细的注释,若还有疑问,请您留言,我们共同探讨。
     第三步:实现Model,业务逻辑。用户注册程序比较简单,主要实现findUserByUserid()、findUserByEmail()、sendPassword()、saveNewUser()等功能。代码如下:
    
SignForm.java

说明:
        1、saveNewUser()使用了Hibernate作为持久化工具,关于Hibernate请参阅相关资料,也可以留言我们共同讨论。
       2、sendPassword()使用JavaMail发送Email,本文件通过SendMail工具类实现。
       3、密码生成由RandStringGenerator()工具类生成。
       4、工具类可以点击这里下载。

    第四步:配置struts-config.xml。

     第五步:调试程序。经过上面的说明和代码示例是不是对Struts中的MVC架构有了比较清晰的了解,我们知道在java特别是j2ee的软件中,需要设置很多的配置文件,刚开始的时候非常烦,特别是频频出错的时候,那种感觉学java的人应该都尝过哦!但是当你彻底了解了配置文件的确切含义,并且能和代码中的内容进行联系时,就会豁然开朗了,就不会觉得象走进死胡同似的。

     有了上面struts-config.xml中的说明,相信你已经和代码联系起来了,如果将这个程序调试成功,那么你就可以说我已经对struts设计MVC Web程序入门了,一旦跨进了这个门槛,你会觉得学习起来是那么的轻松,一些来得是那么自然。

      好了,希望以上三篇文章能带你走进Struts,理解Struts,特别是熟悉Struts的基本流程,当然要想对一种模式由深入的了解,必须要多加实践,从实践中来体验到它的好处。

      最后,希望你能彻底了解Struts,能为你所用。如果有什么意见和评论,请留言,我们共同讨论,共同进步。
      谢谢你的阅读!