学习struts的一个初等的小例子

学习struts的一个小例子

一、             学习目的:

通过这个小例子的学习,初步掌握jsp+struts编程技巧。

二、             功能简介:

显示一个登录页面,用以下登录:

Username = jbuilder

Password = borland

成功的话,显示登录成功的提示,否则显示输入有误的提示。

环境:JB9tomcat4.0struts1.0

三、             操作步骤:

1.      建立一个新web工程:

先新建一个工程,命名为:mystruts

再新建一个web应用,命名亦为:mystruts

 

选中Struts1.0

 

点击OK,一个基于struts框架的web应用就建好了。

2.      建立一个struts构架:

File菜单—>New,然后选中ActionForm

 

新加两个属性:username, password

 

点击Next,直到Finish。可以看到Jb9自动为你生成了一个LogonForm.java文件。

 

同时可以看到在struts-config.xml这个配置文件中,多了如下面所示的几行代码。

接下来,用Action向导创建一个LogonAction类,并将FormBean Name属性值设置成上一步配置的logonForm

 

 

 

 

 

Jb9自动为你生成了LogonAction.java文件。

 

struts-config.xml文件中则会生成如下几行:

mystruts/WEB-INF/classes目录下,添加一个资源配置文件,将其命名为:ApplicationResources.properties,其中内容为:

error.login.username=<li>用户不存在!

error.login.nullusername=<li>用户名不能为空!

error.login.password=<li>密码不正确!

errors.footer=</font></ul>

errors.header=<ul><font color="red">

这个文件有什么用呢?它是为了使你的程序能够更好地国际化。

同时注意在web.xml中添加以下配置语句:

    <init-param>

      <param-name>application</param-name>

      <param-value>ApplicationResources</param-value>

</init-param>

如下图:

到此为止,一个基于struts的空框架就搭好了,但是它只是个空架子而已,还什么功能也没有。下面就正式添加点功能给它:

3.      编写struts源代码:

实现LogonForm类的validate()reset()方法。修改后文件如下:

代码如下:

public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {

    ActionErrors errors = new ActionErrors();

    if (username == null || username.equals("")) {

        errors.add(ActionErrors.GLOBAL_ERROR,

                   new ActionError("error.login.nullusername"));

    }

    return errors;

  }

  public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {

    this.username = null;

    this.password = null; 

  }

下面配置LogonActionForward页面,运用如下所示的可视化编辑器可以方便的进行配置。

 

当然也可以直接修改struts-config.xml文件。

修改后struts-config.xml文件大致是这样的,其中黑体部分为修改后自动生成的:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">

<struts-config>

  <form-beans>

    <form-bean name="logonForm" type="mystruts.LogonForm" />

  </form-beans>

  <action-mappings>

    <action name="logonForm" validate="true" input="/logon.jsp" scope="request" path="/logon">

      <forward name="success" path="/index.jsp">

        <set-property value="value" property="property" />

      </forward>

      <forward name="failure" path="/logon.jsp">

        <set-property value="value" property="property" />

      </forward>

    </action>

  </action-mappings>

</struts-config>

下面来实现LognonAction类的perform方法,实现程序的控制逻辑。黑体部分为修改后代码:

package mystruts;

 

import org.apache.struts.action.*;

import javax.servlet.http.*;

 

public class LogonAction extends Action {

    public ActionForward perform(ActionMapping actionMapping,

                                 ActionForm actionForm,

                                 HttpServletRequest httpServletRequest,

                                 HttpServletResponse httpServletResponse) {

        LogonForm theForm = (LogonForm) actionForm;

        ActionErrors errors = new ActionErrors();

        if (theForm.getUsername().equals("jbuilder") &&

            theForm.getPassword().equals("borland")) {

            return actionMapping.findForward("success");

        } else if (theForm.getUsername().equals("jbuilder")) {

            errors.add(ActionErrors.GLOBAL_ERROR,

                       new ActionError("error.login.password"));

        } else {

            errors.add(ActionErrors.GLOBAL_ERROR,

                       new ActionError("error.login.username"));

        }

        if (!errors.empty()) {

            saveErrors(httpServletRequest, errors);

        }

        return actionMapping.findForward("failure");

    }

}

4.      编写jsp源代码:

最后来编写logon.jspindex.jsp页面。代码分别如下:

logon.jsp文件:

 

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<html:html>

<head>

<title>

logon

</title>

</head>

<body bgcolor="#ffffff">

<h1>

登陆系统

</h1>

<center>

<html:errors />

</center>

<br>

<html:form method="post" action="/logon">

<table align=center width="40%">

<tr>

       <td width="40%" align="center">用户名</td>

       <td><html:text name="logonForm" property="username" /></td>

</tr>

<tr>

       <td align="center">&nbsp;&nbsp;</td>

       <td><html:password name="logonForm" property="password" /></td>

</tr>

<tr>

       <td colspan="2" align=center><input type="submit" value="登录">&nbsp;&nbsp;<input type="reset" value="恢复"></td>

</tr>

</table>

</html:form>

</body>

</html:html>

 

 

index.jsp文件如下:

 

<%@ page contentType="text/html; charset=GBK" %>

<html>

<head>

<title>

index

</title>

</head>

<body bgcolor="#ffffff">

<h1>

登录成功!

</h1>

</body>

</html>

运行效果:

四、             小结:

Struts是一个开源开发包,在Jb9中已自动集成好了,免去了许多配置工作。Struts提供一种基于MVCModel-View-Control)设计模式的开发框架,就有点好比VC中的MFC中的框架一样,使用这一框架可以有效的实现显示逻辑,业务逻辑,控制过程的分离,减弱了耦合性。

  本文档只是很浅显的讲解了一个小例子的全过程,但还没有完全讲清楚struts的原理,故只能使人“知其然,而不知其所以然”,所以还应该钻研struts的原理,那才是最本质的东西,本文档若能起一个抛砖引玉的作用,即已足矣。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值