分享

Struts2框架中为什么要继承ActionSupport类,以及实现过程

 一本正经地胡闹 2019-07-12

   struts可以继承ActionSupport类,也可以不继承,继承的好处简单来说就是更方便实现验证,国际化等功能,与struts2的功能结合紧密,方便我们开发。

ActionSupport类的作用:

     此类实现了很多实用的接口,提供了很多默认的方法,这些默认方法包括国际化信息,默认的处理用户请求的方法等,可以大大简化action的开发,在继承ActionSupport的情况下,必须有无参构造函数

下面用在struts2框架搭建完成的基础上,用 用户请求的例子来实现ActionSupport类:

1.创建视图层两个页面index.jsp和welcome.jsp页面,下面只展示index.jsp页面,welcome.jsp页面的代码自己简单写一下看一下效果就行。这个jsp页面的<s:fielderror/>标签会在相应的字段处输出错误信息。

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@ taglib prefix="s" uri="/struts-tags"%>
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; utf-8">
  7. <title>Insert title here</title>
  8. <style type="text/css">
  9. ul,li {
  10. list-style-type:none;
  11. margin:0px;
  12. float:left;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <form action="helloworldAction" method="post">
  18. <input type="hidden" name="submitFlag" value="login"/>
  19. <div>
  20. <font color=red><s:fielderror fieldName="account"/></font>
  21. <br/>
  22. 账号:<input type="text" name="account">
  23. </div>
  24. <div>
  25. <font color=red><s:fielderror fieldName="password"/></font>
  26. <br/>
  27. 密码:<input type="password" name="password">
  28. </div>
  29. <input type="submit" value="提交">
  30. </form>
  31. </body>
  32. </html>

2.实现action类封装HTTP请求参数,类里面应该包含与请求参数对应的属性,并为属性提供get,set方法,再说一次,在继承ActionSupport的情况下,必须有无参构造函数。

validate方法内部,对请求传递过来的数据进行校验,而且我们也能看出来同一个数据可以进行多方面的验证,如果不满足要求,内容将会在页面上直接显示。里面重写了 execute() throws Exception方法,返回字符串。

  1. package com.hnpi.action;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class RegisterAction extends ActionSupport{
  4. private String account;
  5. private String password;
  6. private String submitFlag;
  7. public String execute() throws Exception {
  8. this.businessExecute();
  9. return "toWelcome";
  10. }
  11. public void validate(){
  12. if(account==null || account.trim().length()==0){
  13. this.addFieldError("account", "账号不可以为空");
  14. }
  15. if(password==null || password.trim().length()==0){
  16. this.addFieldError("password", "密码不可以为空");
  17. }
  18. if(password!=null && !"".equals(password.trim()) && password.trim().length()<6){
  19. this.addFieldError("password", "密码长度至少为6位");
  20. }
  21. }
  22. public void businessExecute(){
  23. System.out.println("用户输入的参数为==="+"account="+account+",password="+password+",submitFlag="+submitFlag);
  24. }
  25. public String getAccount() {
  26. return account;
  27. }
  28. public void setAccount(String account) {
  29. this.account = account;
  30. }
  31. public String getPassword() {
  32. return password;
  33. }
  34. public void setPassword(String password) {
  35. this.password = password;
  36. }
  37. public String getSubmitFlag() {
  38. return submitFlag;
  39. }
  40. public void setSubmitFlag(String submitFlag) {
  41. this.submitFlag = submitFlag;
  42. }
  43. }

 

3.从上面我们也可以看出来,validate方法是没有返回值的,如果验证不成功的话,错误信息该怎么在页面上显示出来呢?我们需要在struts.xml中的Action配置里面,添加一个名称为input的result配置,没有通过验证,那么会自动跳转回到该action中名称为input的result所配置的页面。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts./dtds/struts-2.0.dtd">
  5. <struts>
  6. <package name="helloworld" extends="struts-default">
  7. <action name="helloworldAction" class="com.hnpi.action.RegisterAction">
  8. <result name="toWelcome">/welcome.jsp</result>
  9. <result name="input">/index.jsp</result>
  10. </action>
  11. </package>
  12. </struts>

下面我们来看一下代码效果图:

这个例子可以看出来,validate方法会先于execute方法被执行,只有validate方法执行后,又没有发现验证错误的时候,才会运行execute方法,否则会自动跳转到你所配置的input所对应的页面。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多