zoukankan      html  css  js  c++  java
  • Struts2 Action(1)

    通过第二节中的helloworld实例,会发现Struts2中的Action会比Struts1.x中的Action简练了许多,只需要在熟悉的JavaBean中加入execute方法即可,这样做的好处是省掉了Struts1.x中的FormBean,并且可以方便使用Junit进行单元测试。

    Struts2中将Struts1.x中的FormBean和Action合二为一,进行接受参数、调用程序逻辑、将数据返回到页面的工作。

    虽然使用现在的Action可以进行一系列的工作,但是想要更快更方便的进行开发,可以使Action类继承com.opensymphony.xwork2.ActionSupport类,这个类可以使Action方便的进行数据验证、国际化等工作,在实际开发中大多数情况下是要让Action类继承自ActionSupport类的。

    通过查看源代码或API的方式,知道ActionSupport类实现了一个名为Action的接口,该接口源代码如下:

       1: package com.opensymphony.xwork2;
       2: public interface Action {
       3:     public static final String SUCCESS = "success";
       4:     public static final String NONE = "none";
       5:     public static final String ERROR = "error";
       6:     public static final String INPUT = "input";
       7:     public static final String LOGIN = "login";
       8:     public String execute() throws Exception;
       9: }

    Action接口的源代码很简单,接口中定义了五个常量,一个execute方法,这五个常量就是在日常开发中业务逻辑方法中返回的字符串,execute方法则是Action接口定义的一个默认的业务逻辑方法,该方法在ActionSupport类中的实现如下:

       1: public String execute() throws Exception {
       2:         return SUCCESS;
       3: }

    那么在我们自定义的Action类中只需要重写该方法即可。Struts2会默认的调用execute方法。

    那么如何去定义自己的业务逻辑方法,只需要遵守Struts2中对业务逻辑方法的约定即可(必须返回String类型,并且要无参数列表,还要抛出Exception异常)。

    来看一个登陆的实例,该实例需要以下元素:

    1. Action类:LoginAction.java

    2. jsp页面:login.jsp suc.jsp error.jsp

    3. 在struts.xml中配置

    创建LoginAction并继承自ActionSupport类:

       1: package com.kay.action;
       2:  
       3: import com.opensymphony.xwork2.ActionSupport;
       4:  
       5: public class LoginAction extends ActionSupport {
       6:  
       7:     private String userName;
       8:     private String userPwd;
       9:     public String getUserName() {
      10:         return userName;
      11:     }
      12:     public void setUserName(String userName) {
      13:         this.userName = userName;
      14:     }
      15:     public String getUserPwd() {
      16:         return userPwd;
      17:     }
      18:     public void setUserPwd(String userPwd) {
      19:         this.userPwd = userPwd;
      20:     }
      21:     /**
      22:      * 登陆的业务逻辑方法
      23:      * @return 
      24:      * @throws Exception
      25:      */
      26:     public String login()throws Exception{
      27:         if(userName.equals("Tom") && userPwd.equals("123456")){
      28:             return SUCCESS;
      29:         }
      30:         return LOGIN;
      31:     }
      32: }

    login页面:

       1: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
       2: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
       3: <html>
       4:   <head>
       5:     <title>登陆页面</title>
       6:   </head>
       7:   <body>
       8:         <form action="loginAction.action" method="post">
       9:             用户名:<input type="text" name="userName"/><br>
      10:             密码:<input type="password" name="userPwd"/>
      11:             <input type="submit" value="登录"/>
      12:         </form>
      13:   </body>
      14: </html>

    suc.jsp

       1: <body>
       2:     用户名:${userName }<br>
       3:     密码:${userPwd }
       4:   </body>

    error.jsp:

       1: <body>
       2:        对不起,出错了!
       3: </body>

    在struts.xml中配置LoginAction:

       1: <action name="loginAction" class="com.kay.action.LoginAction" method="login">
       2:             <result name="success">/suc.jsp</result>
       3:             <result name="login">/error.jsp</result>
       4: </action>

    在action节点中这次多了个method属性,该属性用于指定调用Action中的哪个方法。

    在Tomcat中部署程序,浏览器的地址栏中输入http://localhost:8080/s2/login.jsp,:

    clip_image002

    成功后:

    clip_image004

    失败:

    clip_image006

    Struts2也支持在代码中指定调用Action中的哪个方法:

       1: <form action="loginAction!login.action" method="post">
       2: </form>

    通过!号后面跟方法名称也可以,当然这样就不用在struts.xml中添加method属性了。

  • 相关阅读:
    浅析影响一个网站的因素
    23种常用设计模式的UML类图
    各版本IE兼容问题,IE6,IE7,IE8,IE9,IE10,IE11
    47种常见的浏览器兼容性问题大汇总
    5个Sublime Text 的插件推荐
    网页设计师神器,快速生成网站配色、字型等风格的工具——Stylify Me
    免费的高分辨率图库——re:splashed 可用做网页背景、设计或桌面壁纸
    MySQL(18):Select- subquery子查询
    MySQL(17):Select-union(联合查询)使用注意事项
    MySQL(16):Select-union(联合查询)
  • 原文地址:https://www.cnblogs.com/kay/p/1385794.html
Copyright © 2011-2022 走看看