• DMI ( Dynamic Method Invocation )


    功能:

    点击 hello , 调用 execute 函数

    点击 update , 调用 update 函数

    1.项目结构

    2.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    3.UserAction.java

    package com.action;
    import com.opensymphony.xwork2.ActionSupport;
    
    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="hello world bb!";
            return "hello";
        }
        public String update() throws Exception{
            info="ok,updated!";
            return "update";
        }
    }

    4.index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!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>Insert title here</title>
    </head>
    <body>
        <center>
            <a href="user_execute">hello</a><br>
            <a href="user_update">update</a>
        </center>
    </body>
    </html>

    5.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.action.UserAction" method="{1}">
                <result name="hello">/hello.jsp</result>
                <result name="update">/update.jsp</result>
            </action>
        </package>
    </struts>

    6.hello.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!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>Insert title here</title>
    </head>
    <body>
        <center>
            <s:property value="info" />
        </center>
    </body>
    </html>

    7.update.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!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>Insert title here</title>
    </head>
    <body>
        <center>
            <s:property value="info" />
        </center>
    </body>
    </html>
  • 相关阅读:
    文件上传
    大三寒假学习进度笔记Day16
    大三寒假学习进度笔记Day15
    大三寒假学习进度笔记Day14
    大三寒假学习进度笔记Day13
    大三寒假学习进度笔记Day12
    大三寒假学习进度笔记Day11
    大三寒假学习进度笔记Day10
    Java命名规范
    架构漫谈阅读笔记03
  • 原文地址:https://www.cnblogs.com/c0liu/p/5495676.html
Copyright © 2020-2023  润新知