zoukankan      html  css  js  c++  java
  • Struts(五)Action的访问

      在struts开发中,Action作为框架的核心类,实现对用户的请求的处理,Action被称为业务逻辑控制器。一个Action类代表一次请求或调用。Action就是用来处理一次用户请求的对象

    Action的编写方式

      Action是一个POJO的类 ,在struts2中,Action可以不继承特殊的类或不实现任何特殊的接口。POJO  :Plain  Ordinary    Java  Object  (简单的java对象)  具有一部分getter  /setter方法的那种类  就被称为POJO类

         Action 类 ,要有公共的无参构造方法,采用默认的就可以 和一个execute()方法。

    定义格式如下:

        

    public   class  ActionDemo1{
    
             public  String  execute(){
    
                        System.out.println("ActionDemo1执行了。。。。");
                         return  null;
             }
    
    }

    execute()方法的要求如下:

    • 方法的权限修饰符为public
    • 返回一个字符串,就是指示的下一个页面的Result
    • 方法没有参数

    开发者自己编写Action类或者实现Action接口或者继承ActionSupport类

    【Action类实现一个Action接口】

           struts2提供一个Action 接口,使开发更加规范

    public class   ActionDemo2   implements Action{
    
            @override
            public  String  execute () throws Exception{
    
    
                    System.out.println("ActionDemo2 执行了......");
    
                    return  null;
    
    
                    }

    Action 接口为位于com.opensymphony.xwork  包中。一个execute()  方法,  该接口的规范规定了Action 处理类应该包含一个execute方法,该方法返回一个字符串。

      Action接口定义了5个字符串常量,它们的作用是统一execute方法的返回值

    • SUCCESS       :success 代表成功
    • NONE             : none  代表页面不跳转
    • ERROR          :error   代表跳转到错误页面
    • INPUT            :input  数据校验的时候跳转的路径
    • LOGIN           :login   用来跳转到登录页面

    Action的接口简单,帮助较小,  开发中都是从ActionSupport类继承

    [Action   类继承ActionSupport类]  (推荐)

    public  class ActionDemo3 extends  ActionSupport{
    
                @Override
                public  String   execute()  throws   Exception{
    
                        System.out.println("ActionDemo3 执行了。。。。。");
                        return  NONE;
    
                   }
      }

      Action 的访问:

    .利用<action>标签中的 method属性 ,配置method属性值

    点击页面  click.jsp:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>点击事件</title>
    </head>
    <body>
        <a href="${pageContext.request.contextPath }/SaveUserAction.action">保存</a>
        <a href="${pageContext.request.contextPath }/UpdateUserAction.action">修改</a>
        <a href="${pageContext.request.contextPath }/DeleteUserAction.action">删除</a>
        <a href="${pageContext.request.contextPath }/FindUserAction.action">查询</a>
    </body>
    </html>

     Action 文件  配置  StrutsDemo1.java

    package com.study.Hello;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class StrutsDemo1 extends ActionSupport{
        private static final long serialVersionUID = 1L;
    
        public String save(){
            return NONE;
         }
        
        public String update(){
            return NONE;
        }
        
        public String delete(){
            return NONE;
        }
        public String find(){
            return NONE;
        }
    }

      

    Struts.xml文件  :

    action标签的method属性值与  Action 配置文件(即StrutsDemo1) 中的  方法对应

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
    
        <constant name="struts.i18n.encoding" value="UTF-8"></constant>
        <constant name="struts.devMode " value="true"></constant>
        <constant name="struts.action.extension" value="action,,"></constant>    
        <package name="hello"  namespace="/" extends="struts-default">
            <action name="SaveUserAction" class="com.study.Hello.StrutsDemo1"  method="save"></action>
            <action name="UpdateUserAction" class="com.study.Hello.StrutsDemo1" method="update"></action>
            <action name="DeleteUserAction" class="com.study.Hello.StrutsDemo1"  method="delete"></action>
            <action name="FindUserAction" class="com.study.Hello.StrutsDemo1"  method="find"></action>
        </package>
    </struts>

    Web.xml   文件保持不变

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>Struts2StudyDay01</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <!-- <context-param>
              <param-name>struts.i18n.encoding</param-name>
              <param-value>UTF-8</param-value>
      </context-param> -->
      <filter>
              <filter-name>Action</filter-name>
              <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
              <filter-name>Action</filter-name>
              <url-pattern>/*</url-pattern>
      </filter-mapping>
    
    </web-app>

    但是从Action配置 中可以看出一个Action被配置很多次 ,只是修改了method的值

    如果要只配置一次,就要用到  通配符的配置方式

    2.通配符的配置

     jsp 、Action类 页面保持不变;

    Struts.xml 页面    

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        <!-- 2.变量设置 在Struts.xml文件里面配置 -->
        <!--i18n :国际化  解决post提交乱码。输出中文乱码  省去过滤器字节编码-->
        <constant name="struts.i18n.encoding" value="UTF-8"></constant>
        <constant name="struts.devMode " value="true"></constant>
        <constant name="struts.action.extension" value="action,,"></constant>
        <!-- package :将Action配置封装,就是可以在Package中配置很多action
        
             name 属性:给包起名字,不能 和其他包名重复
             namespace属性:给action的访问路径中定义一个空间 
             extends  :继承一个指定   包 
        
         -->
            
        <package name="hello"  namespace="/" extends="struts-default">
    
        <!--     <action name="SaveUserAction" class="com.study.Hello.StrutsDemo1"  method="save"></action>
            <action name="UpdateUserAction" class="com.study.Hello.StrutsDemo1" method="update"></action>
            <action name="DeleteUserAction" class="com.study.Hello.StrutsDemo1"  method="delete"></action>
            <action name="FindUserAction" class="com.study.Hello.StrutsDemo1"  method="find"></action> -->
            
            
            <action name="*UserAction" class="com.study.Hello.StrutsDemo1" method="{1}"></action>
        </package>
    </struts>

    使用{1}取出于第一次星号统配的 内容 ;

    3.动态方法访问

    动态访问在Struts2默认是关闭的,使用要先开启一个常量

    <constant  name="struts.enable.DynamicMethodInvocation" value="true"></constant>

    action标签配置

     <!-- 动态方法配置   访问路径很重要  -->
             <action  name="StrutsDemo1"  class="com.study.Hello.StrutsDemo1"></action>

    要保障action的访问路径:

    下面就是 代码演示:

    Action类 代码:

    package com.study.Hello;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class StrutsDemo1 extends ActionSupport{
    
        public String save(){
            System.out.println("save");
            return NONE;
         }
        
        public String update(){
            System.out.println("update");
            return NONE;
        }
        
        public String delete(){
            System.out.println("delete");
            return NONE;
        }
        public String find(){
            System.out.println("find");
            return NONE;
        }
    }

    Action  配置 即   Struts.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        <!-- 2.变量设置 在Struts.xml文件里面配置 -->
        <!--i18n :国际化  解决post提交乱码。输出中文乱码  省去过滤器字节编码-->
        <constant name="struts.i18n.encoding" value="UTF-8"></constant>
        <constant name="struts.devMode " value="true"></constant>
        <constant name="struts.action.extension" value="action,,"></constant>
        
        <!-- 动态访问在Struts2默认是关闭的,使用要先开启一个常量 -->
        <constant  name="struts.enable.DynamicMethodInvocation" value="true"></constant>
        <!-- package :将Action配置封装,就是可以在Package中配置很多action
        
             name 属性:给包起名字,不能 和其他包名重复
             namespace属性:给action的访问路径中定义一个空间 
             extends  :继承一个指定   包 
        
         -->
            
        <package name="hello"  namespace="/" extends="struts-default">
            <action name="StrutsDemo1" class="com.study.Hello.StrutsDemo1">
                <result name="success" >/hello.jsp</result>
            </action>
            
        <!--     
          method 属性配置
        <action name="SaveUserAction" class="com.study.Hello.StrutsDemo1"  method="save"></action>
            <action name="UpdateUserAction" class="com.study.Hello.StrutsDemo1" method="update"></action>
            <action name="DeleteUserAction" class="com.study.Hello.StrutsDemo1"  method="delete"></action>
            <action name="FindUserAction" class="com.study.Hello.StrutsDemo1"  method="find"></action> -->
            
            <!-- 通配符配置
            <action name="*UserAction" class="com.study.Hello.StrutsDemo1" method="{1}"></action>
             -->
             
             <!-- 3动态方法配置   访问路径很重要  -->
             <action  name="StrutsDemo1"  class="com.study.Hello.StrutsDemo1"></action>
        </package>
    </struts>

    接下来就是  jsp页面的  访问action 的URL   

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>点击事件</title>
    </head>
    <body>                                <!--  这里要输入正确的链接 才可以访问到action   -->                    
        <a href="${pageContext.request.contextPath }/StrutsDemo1!save.action">保存</a>
        <a href="${pageContext.request.contextPath }/StrutsDemo1!update.action">修改</a>
        <a href="${pageContext.request.contextPath }/StrutsDemo1!delete.action">删除</a>
        <a href="${pageContext.request.contextPath }/StrutsDemo1!find.action">查询</a>
    </body>
    </html>

    打开jsp页面点击保存 出现页面   注意URl

    http://127.0.0.1:8080/News/newsAction!findAll.action

    上面链接中News是工程名,newsAction是类名,findAll是newsAction类中的方法。

    即:   action标签中的name属性值: 需要时Action的类名

    否则会出现 以下错误:

  • 相关阅读:
    Open live Writer
    python两则99乘法表
    更改jupyter-notebook启动时的默认目录
    在内容中插入代码,返回进行编辑时,有无法跳出代码区的情况
    关于jupyter notebook密码设置
    nginx+zuul
    spring-cloud 服务优雅下线
    java.util.ConcurrentModificationException异常排查
    使用bitset实现毫秒级查询(二)
    使用bitset实现毫秒级查询
  • 原文地址:https://www.cnblogs.com/shaoxiaohuan/p/8514854.html
Copyright © 2011-2022 走看看