简体   繁体   中英

There is no action mapped for namespace struts 2

I am converting INR to USD and visa-versa. When I run, I get this warning:

WARNING [http-nio-8084-exec-22] com.opensymphony.xwork2.util.logging.commons.CommonsLogger.warn Could not find action or result
 There is no Action mapped for namespace / and action name netbeans-tomcat-status-test. - [unknown location]

Action file is ConverterAction.java

package com.vishal;

import com.opensymphony.xwork2.ActionSupport;
import java.math.BigDecimal;

public class ConverterAction extends ActionSupport {
private String from;
private String to;
private BigDecimal amount;
private BigDecimal result;

public String excecute() {
ConverterBean n = new ConverterBean();
result = n.convert(from, to, amount);
return SUCCESS;
}

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;
}

public BigDecimal getAmount() {
return amount;
}

public void setAmount(BigDecimal amount) {
this.amount = amount;
}

public BigDecimal getResult() {
return result;
}

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

Bean class ConverterBean.java

package com.vishal;

import java.math.BigDecimal;

public class ConverterBean {
private BigDecimal INR = new BigDecimal(0.02291);
private BigDecimal USD = new BigDecimal(46.58);

public BigDecimal convert(String fromCurrency, String toCurrency, BigDecimal     amount) {
if (fromCurrency.equals(toCurrency)) {
  return amount;
}

BigDecimal toRate = findRate(toCurrency);
BigDecimal result = toRate.multiply(amount);
return result.setScale(2, BigDecimal.ROUND_UP);
}

public BigDecimal findRate(String currencySymbol) {
  BigDecimal returnValue = null;

  if (currencySymbol.equals("INR")) {
      returnValue=INR;
  }

  if (currencySymbol.equals("USD")) {
    returnValue=USD;
  }
  return returnValue;  
  }
  }

Struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts  Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package extends="struts-default" name="/">
<action name="convert">
  <result name="success">/result.jsp</result>
</action>
</package>
</struts>

Index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<body>
<s:form action="convert">
  Enter amount to convert: <s:textfield default="0" name="amount"/>
  <br/><br/>

  From:
  <s:textfield name="from"/>
  <br/><br/>

  To:
  <s:textfield name="to"/>
  <br/><br/>

  <s:submit value="submit"/>
  </s:form>
 </body>
</html>

Result.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>

<body>    
        <h2>Hello</h2>hi
        <s:property value="result" default="0" />

</body>   
</html>

在此处输入图片说明

Too much for a comment, so here's your code review.

ConverterAction

  • Don't import stuff you don't have to.
  • Put the important stuff at the top (getters and setters are not important).
  • Be consistent with indentation and spacing
package com.vishal;

import com.opensymphony.xwork2.ActionSupport;
import java.math.BigDecimal;

public class ConverterAction extends ActionSupport {
  private String from;
  private String to;
  private BigDecimal amount;
  private BigDecimal result;

  public String excecute() {
    ConverterBean n = new ConverterBean();
    result = n.convert(from, to, amount);
    return SUCCESS;
  }

  public String getFrom() {
    return from;
  }

  public void setFrom(String from) {
    this.from = from;
  }

  public String getTo() {
    return to;
  }

  public void setTo(String to) {
    this.to = to;
  }

  public BigDecimal getAmount() {
    return amount;
  }

  public void setAmount(BigDecimal amount) {
    this.amount = amount;
  }

  public BigDecimal getResult() {
    return result;
  }

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

ConverterBean

  • Use very descriptive variable names.
  • Use spaces around language keywords.
  • Don't insert random blank lines.
  • Return early.
  • Handle all cases.
package com.vishal;

import java.math.BigDecimal;

public class ConverterBean {
  private BigDecimal INR = new BigDecimal(0.02291);
  private BigDecimal USD = new BigDecimal(46.58);

  public BigDecimal convert(String fromCurrency, String toCurrency, BigDecimal amount) {
    if (fromCurrency.equals(toCurrency)) {
      return amount;
    }

    BigDecimal toRate = findRate(to);
    BigDecimal result = toRate.multiply(amount);
    return result.setScale(2, BigDecimal.ROUND_UP);
  }

  public BigDecimal findRate(String currencySymbol) {
      BigDecimal returnValue = null;

      if (currencySymbol.equals("INR")) {
          return INR;
      }

      if (currencySymbol.equals("USD")) {
        return USD;
      }

      throw new UnsupportedOperation("Unknown Conversion");
  }
}

JSP

  • Struts 2 properties exposed by their property name, eg, lower-case.
  • Formatting is important.
  • Actions are not classnames, they're action names: You don't even have a valid struts.xml at the moment, as your server log must indicate.
<!DOCTYPE html>
<html>
  <body>
    <s:form action="convert">
      Enter amount to convert: <s:textfield default="0" name="amount"/>
      <br/><br/>

      From:
      <s:textfield name="from"/>
      <br/><br/>

      To:
      <s:textfield name="to"/>
      <br/><br/>

      <s:submit value="submit"/>
    </s:form>
  </body>
</html>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
  <package extends="struts-default" name="/">
    <action name="convert">
      <result name="success">/result.jsp</result>
    </action>
  </package>
</struts>

(The config is from memory, it's been awhile.)

You'll also have an issue when you first request the page since there will be no values, so you'll get null all over the place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM