zoukankan      html  css  js  c++  java
  • struts2 DMI

         由于在struts 对象对URL请求的处理都是调用默认execute()方法,对于单一的业务请求这种方式比较合适.但在实际开发中业务请求类型方法多种多样,对一个Action可能有不同的请求,通过建立action能够解决这个问题,但会使程序变得越来越复杂,且编码越来越多,修一处可能动全身麻烦不容易维护.根据请求的数量不断增多这种方式显然不太合适.

       在struts2中提供了解决这类问题的方法,称之为DMI(动态调用).通过请求action对象的方法,可以实现对某一个业务逻辑处理.DMI处理方式是通过请求Aciton对象中的一个具体方法来实现动态操作.具体就是在请求action的url地址后加上请求的字符串,与aciton对象的方法进行匹配.其中action对象名称和方法之间用"!"区分开.下面以实例的方式进行说明.

    例:配置环境变量请参照前面介绍的环境配置方法.

    1. 在Eclipse中创建一个java web 项目名称为:Struts2DMIDemo,添加struts2支持库击开发库等.

    建立文件包括:UserAction.java,struts.xml,web.xml,add.jsp,index.jsp,update.jsp文件.

    Action类 UserAction.java 内容如下:

    package com.northeasttycoon;
    
    import com.opensymphony.xwork2.ActionSupport;
    /**
     * @author NorthEastTycoon
     *
     */
    public class UserAction extends ActionSupport {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private String info;
    	public String getInfo() {
    		return info;
    	}
    	public void setInfo(String info) {
    		this.info = info;
    	}
    	
    	public String execute() throws Exception{
    		
    		info ="this is add operator!";
    		return "sucess";
    	}
    	public String update() throws Exception{
    		
    		info="this is update operator!";
    		return "update";
    	}
    	
    }
    

    web.xml 文件内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- northeasttycoon -->
    <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_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>Struts 2</display-name>
       <welcome-file-list>
          <welcome-file>index.jsp</welcome-file>
       </welcome-file-list>
       <filter>
          <filter-name>struts2</filter-name>
          <filter-class>
             org.apache.struts2.dispatcher.FilterDispatcher
          </filter-class>
       </filter>
    
       <filter-mapping>
          <filter-name>struts2</filter-name>
          <url-pattern>/*</url-pattern>
       </filter-mapping>
    </web-app>
    

    struts.xml 文件内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
       "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
       "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
       <package name="default" namespace="/" extends="struts-default">    
          <action name="user"   class="com.northeasttycoon.UserAction">
                <result name="sucess">/add.jsp</result>
                 <result name="update">/update.jsp</result>
          </action>
       </package>
    </struts>
    

    add.jsp 文件内容如下:

    <%@ page contentType="text/html; charset=UTF-8" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
    <head>
    <title>success</title>
    </head>
    <body> 
    <s:property value="info"/></br>
    </body>
    </html>
    

     index.jsp 文件内容如下:

    <%@ page contentType="text/html; charset=UTF-8" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
    <head>
    <title>Hello World</title>
    </head>
    <body>
    <center >
     <a href="user"><font size="3" color="red"> sucess  </font></a></br>
      <a href="user!update.action"><font size="3" color="red"> update </font></a>
    </center>
    </body>
    </html>
    

     update.jsp内容如下:

    <%@ page contentType="text/html; charset=UTF-8" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
    <head>
    <title>update</title>
    </head>
    <body> 
    <s:property value="info"/></br>
    </body>
    </html>
    

    程序目录结构如下:

    运行效果图:

    (1) 在浏览器中输入:http://localhost:8080/Struts2DMIDemo 出现以下界面

     

     (2)点击sucess 进入以下界面

     

     (3)点击udpate 进入以下界面

     

    以上为struts2动态调用实例.

    备注:

        作者:东北大亨

        博客:http://www.cnblogs.com/northeastTycoon/p/5618450.html                                           

    版权为个人所有,欢迎大家转载;但转载时必须注明文章来源,且在文章开头明显处给明链接。

  • 相关阅读:
    svg文件使用highmap显示
    动静分离
    angular 零碎
    使用doxmate生成文档
    javascript之console篇
    java 中String与StringBuilder 效率
    highcharts 组合chart
    js 攻坚克难
    html base 又一重大发现
    sql 分析 依赖beanutils
  • 原文地址:https://www.cnblogs.com/northeastTycoon/p/5618450.html
Copyright © 2011-2022 走看看