Wildcard mapping on Struts2
I followed an online tutorial on how to use the Wildcard method in Struts2 but it doesn't work as shown. When the command is executed, it only calls the method execute()
and never calls any other methods. I'm not sure what the problem is, but any help would be awesome.
Below is my action file and struts file linking action with JSP calls in 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>
+3
source to share
2 answers