发布.net版的strutsnstruts1.0

本文示例源代码或素材下载

  如今,NHibernate有了,NSpring有了,唯独少了个NStruts。也许是因为.netWebform开发模式吧,NStruts对于.net来说没有多大的实用价值,webform已经很struts了。不过,我还是觉得struts的开发模式用起来顺手一点。

  当一个页面的数据项过多的时候,假如有几十个的文本输入框,在后台获取它们的数据实在是件相当痛苦的事情。如果能像struts那样,直接发送个请求就OK了,那该有多好。Webform和struts各有优点,如果能在做项目中,将起到一个互补的作用。

  基于以上原因,我设计了一个.net版的struts,名字就叫NStruts。这是一个开源的项目,支持的朋友希望也加进这个项目来,共同完善它。我设计的思路是,将webform的优点和struts的优点相结合,形成一个独特的struts。

发布.net版的struts---nstruts1.0

  现在以上功能除了依赖注入拦截器没有做好,其它功能都已经做好了。

  NStruts由三个核心类构造,Configuration, Dispatcher, ActionSupport。

  Configuration:Configuration类负责获取获取配置文件的信息

  Dispatcher:Dispatcher类负责请求Action,拦截Action,并执行Action内的方法等。

  ActionSupport:ActionSupport类是抽象类,提供了一些方法的封装,Action类可以继承该类以获得更多的功能。

  首先看一下使用示例吧

  页面的代码如下:

Default.ASPx
<body>
  <form id="form1" runat="server">
  <div>
  用户名:<input name="UserName" type="text"></input><br />
  密码:<asp:TextBox ID="Password" runat="server"></asp:TextBox><br />
  <asp:Button ID="Login" runat="server" Text="登录" onclick="Login_Click" />
    <asp:Button ID="Other" runat="server" Text="Other" onclick="Other_Click" />
    <asp:Button ID="TypeButton" runat="server" Text="Type"
      onclick="TypeButton_Click" />
    <asp:Label ID="Label1" runat="server"></asp:Label>
  </div>
  </form>
</body>

 

表单控件可以是普通的html标签,而且不需要设置runat="server"哦。

 

  先定义一个Action类:

 

TypeAction
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
///TypeAction 的摘要说明
/// </summary>
public class TypeAction
{
  public TypeAction()
  {
  }
  public string Execute()
  {
    if (UserName == "demo" && Password == "demo")
    {
      Age = 22;
      return "success";
    }
    return "error";
  }
  public string UserName { get; set; }
  public string Password { get; set; }
  public int Age { get; set; }
}

 

  TypeButton在代码:

 

TypeButton_Click
  /// <summary>
  /// 使用Type方法请求Action,不需要配置文件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  protected void TypeButton_Click(object sender, EventArgs e)
  {
    string result = Dispatcher.Send(this, typeof(TypeAction));
    if (result == "success")
    {
      Page.Response.Redirect("Default4.ASPx");
    }
  }

 

通过Type传值可以不需要配置文件,使用起来比较简单。

 

  NStruts是支持配置文件的,而且还支持多个配置文件,可以避免了单个文件带来的代码过长问题,也能起到团队协作的作用。使用配置文件的好处是很多的,能够更充分地发挥NStruts的功能,虽然会麻烦了点。NStruts的配置文件格式如下:

 

nstruts
<?XML version="1.0" encoding="utf-8" ?>
<nstruts>
 <!--设置是否自动刷新配置文件信息-->
 <reflesh auto="true"/>
 <!--包含其它配置文件-->
 <includes>
  <include file="pages/nstruts.XML"/>
 </includes>
 <!--定义Action映射-->
 <actions>
  <action name="Other" assembly="TestAction" class="TestAction.OtherAction">
   <result name="success">Default3.ASPx</result>
  </action>
 </actions>
 <!--定义全局跳转-->
 <global-results>
  <result name="error">Default2.aspx</result>
 </global-results>
</nstruts>

 

  如果使用配置文件,则需要创建一个默认的配置文件,并把它保存在Web应用程序的根目录下,配置文件的名字为:nstruts.xml。

 

  如果有多个配置文件,可在include结点添加。在执行Action的Execute后,如果在result结点中没有相应的信息,则返回到方法调用处。

 

  如下演示:

 

LoginAction
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestAction
{
  /// <summary>
  /// 普通的Action类
  /// </summary>
  public class LoginAction
  {
    public string Execute()
    {
      if (UserName == "demo" && Password == "demo")
      {
        Age = 22;
        return "success";
      }
      return "error";
    }
    public string UserName { get; set; }
    public string Password { get; set; }
    public int Age { get; set; }
  }
}
  
Login_Click
  /// <summary>
  /// 使用普通的类作为Action
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  protected void Login_Click(object sender, EventArgs e)
  {
    string result = Dispatcher.Send(this, "Login");
    if (result == "success")
    {
      ValueTable valueTable = Page.Items["ValueTable"] as ValueTable;
      Label1.Text = "UserName:" + valueTable["UserName"] + ",Password:" + valueTable["Password"] + ",Age:" + valueTable["Age"];

 

 

由上面的代码可以看到,Action可以只是一个普通的C#类。

 

  继承自ActionSupport类能获得更多的功能,如表单数据验证:

 

OtherAction
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NStruts.Pride;
namespace TestAction
{
  public class OtherAction : ActionSupport
  {
    public string UserName { get; set; }
    public string Password { get; set; }
    public int Age { get; set; }
    public override string Execute()
    {
      if (UserName == "demo" && Password == "demo")
      {
        Age = 22;
        AddSession("UserName", "demo");
        return Success;
      }
      return Error;
    }
    public override void Validate()
    {
      if (String.IsNullOrEmpty(UserName) || String.IsNullOrEmpty(Password))
      {
        AddFieldError("LoginInfo", "LoginInfoRequired");
      }
    }
  }
}
  
Other_Click
  /// <summary>
  /// 使用继承ActionSupport的类
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  protected void Other_Click(object sender, EventArgs e)
  {
    string result = Dispatcher.Send(this, "Other");
    if (result == "input")
    {
      ValueTable valueTable = Page.Items["ValueTable"] as ValueTable;
      FieldErrorTable fieldErrorTable = valueTable["FieldErrorTable"] as FieldErrorTable;
      Label1.Text = fieldErrorTable["LoginInfo"];
    }
  }

 

 

 

  可以看到,Action的属性值将会保存在Page.Items的ValueTable中返回到方法调用页面,表单验证的错误信息保存在ValueTable的FieldErrorTable中。

 

  注释是非常的详细的,希望喜欢的朋友支持的朋友,一起来完善。如果有朋友对源码有了修改,希望能给我发一份。

 

  我的邮箱地址是reallypride@163.com

 

  我的QQ是:571398367

 

  欢迎大家提意见,也希望大家的参与。

 

  项目会不定期地更新,下一个版本打算加入简单的依赖注入功能。

 

原文地址:https://www.cnblogs.com/weihengblogs/p/2880518.html