3

我遵循了一个关于如何在 Struts2 中使用通配符技术的在线教程,但它并没有如图所示工作。在执行命令时,它只调用该execute()方法,从不调用任何其他方法。我不确定问题是什么,但任何帮助都会很棒。

下面是我的动作文件和将动作链接到 JSP 页面上的 JSP 调用的 struts 文件。

CalculatorAction.java

package com.simplecode.action;

import com.opensymphony.xwork2.ActionSupport;

public class CalculatorAction extends ActionSupport
{
    private float number1;
    private float number2;
    private float result;
    private String methodName;




    public String add()
    {
        System.out.print("afsnjfbkjsdfhdskfhsdkjfhksdjfhkdsjfhksdjfhsdkjfhksjfhkjdsfhdsk");
        result=number1+number2;
        setMethodName("add Method");
        return SUCCESS;
    }

    public String subtract()
    {
        result = number1 - number2;
        setMethodName("subtract Method");
        return SUCCESS;
    }

    public String multiply()
    {
        result = number1 * number2;
        setMethodName("multiply Method");
        return SUCCESS;
    }

    public String divide()
    {
        if(number2!=0)
            result = number1/number2;
        else if(number1!=0)
            result = number2/number1;
        else
            result=0;

        setMethodName("divide Method");
        return SUCCESS;
    }

    public float getNumber1() {
        return number1;
    }

    public void setNumber1(float number1) {
        this.number1 = number1;
    }

    public float getNumber2() {
        return number2;
    }

    public void setNumber2(float number2) {
        this.number2 = number2;
    }

    public float getResult() {
        return result;
    }

    public void setResult(float result) {
        this.result = result;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }
}

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>
    <constant name="struts.enable.SlashesInActionNames" value="true"/>
    <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    <constant name="struts.devMode" value="true"></constant>
    <package name="default" extends="struts-default">
        <action name="*Calculator" method ="{1}"
                class="com.simplecode.action.CalculatorAction">
            <result name="success">curd.jsp</result>
        </action>
    </package>

</struts>

凝乳.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
  <title>Dispatch Action</title>
</head>
<body>
<s:form action="Calculator">
  <table>
    <tr><td><s:textfield name="number1" label="Number 1 " id="number1"/></td></tr>
    <tr><td><s:textfield name="number2" label="Number 2 " id="number2"/></td></tr>
    <tr><td><s:textfield name="result" label="Result " readonly="true"/></td></tr>
    <tr><td><s:textfield name="methodName" label="Method involked " readonly="true" /></td></tr>
    <tr>
      <td><s:submit value="addCalculator" align="left" /></td>
      <td><s:submit action="subtractCalculator" value="Subtract" align="left"/></td>
      <td><s:submit action="divideCalculator" value="Divide" align="left"/></td>
      <td><s:submit action="multiplyCalculator" value="Multiply" align="left"/></td>
      <td><s:submit align="left"/></td>

    </tr>
  </table>
</s:form>
</body>
</html>
4

2 回答 2

1

您还需要设置action prefix enabled常量,例如,

<constant name="struts.mapper.action.prefix.enabled" value="true"/>

这是一种黑客行为;允许访问任意操作方法可能有点冒险。

于 2015-05-01T00:48:09.090 回答
0

您可以将方法的名称传递给 action 标记中的方法属性。

例如

 <action name="abc" class="a.b.c.something" method="yourMethod">
            <result name="S">/something.jsp</result>
    </action>
于 2017-02-15T07:28:13.213 回答