文章正文

JAVA基础: 学习struts的一个小例子

来源:    2007-4-12 15:39:31 网友评论 0 条 字体:[ ] ~我要投稿!
一、学习目的:
通过这个小例子的学习,初步掌握jsp+struts编程技巧。

二、功能简介:

显示一个登录页面,用以下登录:
Username = jbuilder
Password = borland
成功的话,显示登录成功的提示,否则显示输入有误的提示。
环境:JB9+tomcat4.0+struts1.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,其中内容为:

字串5



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的空框架就搭好了,但是它只是个空架子而已,还什么功能也没有。下面就正式添加点功能给它:

<ccid_page/>

3.编写struts源代码:

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

<CENTER><ccid_nobr>
<table width="400" border="1" cellspacing="0" cellpadding="2" 字串1
bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center">
<tr>
  <td bgcolor="e6e6e6" class="code" style="font-size:9pt">
  <pre><ccid_code>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;

}</ccid_code></pre>
  </td>
</tr>
</table>

字串7


</ccid_nobr></CENTER>

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

当然也可以直接修改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">

字串3



  <B><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></B>



  </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, 字串9

                      HttpServletRequest httpServletRequest,

                      HttpServletResponse httpServletResponse) {

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

字串8





    } else {



        errors.add(ActionErrors.GLOBAL_ERROR,



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



    }



    if (!errors.empty()) {



        saveErrors(httpServletRequest, errors);



    }



    return actionMapping.findForward("failure");</B>



  }

}

<ccid_page/>

4.     编写jsp源代码:

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

logon.jsp文件:





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

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

<html:html>

字串3



<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">密 码</td>

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

</tr>

<tr>
字串9

    <td colspan="2" align=center><input type="submit" value="登录"> <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提供一种基于MVC(Model-View-Control)设计模式的开发框架,就有点好比VC中的MFC中的框架一样,使用这一框架可以有效的实现显示逻辑,业务逻辑,控制过程的分离,减弱了耦合性。 字串3

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



上一篇:浅析Java语言中两种异常使用间的差别
下一篇:让spring帮助你在MVC层解决JPA的缓迟加载问题
用户名:新注册) 密码: 匿名评论 [所有评论]
评论内容:不能超过250字,请自觉遵守互联网相关政策法规。
本栏搜索

  • Google Chinesedocument.com
推荐文章
     
 网站首页 -  网站地图 -  技术论坛 -  网站投稿 -  广告服务 -  手机游戏
©2007 分享文档 Chinesedocument.com [京ICP备06000384号]