I followed an online tutorial on how to use Wildcard technique in Struts2 but it does not work as shown. On execution of the command, it only calls the execute()
method and never calls any other methods. I'm not sure what the issue is, but any help would be amazing.
Below are my action file and the struts file linking the action to the JSP calls on the JSP page.
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>
curd.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>
You need to also set the action prefix enabled
constant, e.g.,
<constant name="struts.mapper.action.prefix.enabled" value="true"/>
This is kind of a hack; it can be a little risky to allow access to arbitrary action methods.