Struts2学习笔记

有三种方法可以使一个Action处理多个请求

  1. 动态方法调用DMI
  2. 定义逻辑Acton
  3. 在配置文件中使用通配符

这里就说一下在配置文件中使用通配符,这里的关键就是struts.xml配置文件,在最简单的配置当中,我们只需要写一个Action,但是一个Action只能对应一种处理方式,我们只需在Action的类中实现execute的方法即可,如下面的代码

package struts2.action;

import com.opensymphony.xwork2.ActionSupport;

public class Sample extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    @Override
    public String execute() throws Exception {
        //
        return SUCCESS;
    }

}

如果需要一个Action类处理多个业务,那就需要用到以上三种方法,第一种方法只需要关注请求语句就行,第二种方法需要关注struts.xml配置文件,而这篇文章关注的也是配置文件

只需要把通配符写上就能实现多任务处理,如下面的配置,在这段配置可以看到一个test_*,{1}和result_{1}.jsp,例如:请求是test_page1,那么它对应的方法就是page1,对应返回的页面是result_page1.jsp,就这么简单(注意!!!,这里是区分大小写的,意思就是,例如:在页面当中aciton="Hello",那么在action类当中的方法名也要大写,public void Hello(),返回页面也要对应大写,Hello.jsp,如果这里使用hello.jsp,会报404找不到页面)

<action name="test_*" class="struts2.action.TestAction" method="{1}">
     <result name="success">result_{1}.jsp</result>
</action>

接下来我们来看看完整的例子:

1.请求页面,这里有两个按钮,一个是执行test_page1的action,另一个是执行test_page2的action

//ResultTest.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> <h1>測試頁面</h1> <s:form action="test_page1"> <s:submit value="测试页面1"/> </s:form> <s:form action="test_page2"> <s:submit value="测试页面2"/> </s:form> </body> </html>

2.action类

这个类很简单,就实现了两个方法,page1和page2的方法,返回值都是success

package struts2.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public String page1() {
        return SUCCESS;
    }
    
    public String page2() {
        return SUCCESS;
    }
    
}

3.struts.xml配置

当请求test_page1的时候,就会调用page1的方法,如果是success就返回,result_page1的方法

当请求test_page2的时候,就会调用page2的方法,如果是success就返回,result_page2的方法

<?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" extends="struts-default">
        <action name="test_*" class="struts2.action.TestAction" method="{1}">
            <result name="success">result_{1}.jsp</result>
        </action>
    </package>
</struts>

4.返回页面

这里有两个返回页,一个是result_page1.jsp,另一个是result_page2.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>
    <h1>测试成功页1</h1>
</body>
</html>
原文地址:https://www.cnblogs.com/oscar1987121/p/6078209.html