2015-11-04 93 views
0

我將INR轉換爲USD,反之亦然。當我跑,我得到這樣的警告:沒有爲名稱空間struts 2映射的動作

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] 

操作文件是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類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> 

enter image description here

+1

在考慮更進一步之前,我會考慮快速瀏覽一個簡單的教程;除了格式化問題之外,這裏還有很多事情不會按原樣工作。 –

回答

1

太多的評論,所以這裏是你的代碼審查。

ConverterAction

  • 不導入的東西,你不必。
  • 把重要的東西放在最上面(getter和setter不重要)。
  • 要縮進和間距
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

  • 使用非常描述性的變量名稱一致。
  • 在語言關鍵字周圍使用空格。
  • 請勿插入隨機空白行。
  • 提前回來。
  • 處理所有情況。
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種的性質可以通過屬性名稱,例如,小寫露出。
  • 格式很重要。
  • 操作不是類名,它們是操作名稱:您目前甚至沒有有效的struts.xml,因爲您的服務器日誌必須指出。
<!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> 

支柱。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> 

(該配置是從內存中,它已經一段時間。)

您也將有一個問題,當你第一次請求的頁面,因爲不會有任何價值,所以你」我會得到null到處都是。

+0

謝謝Dave抽出時間幫助我......但我仍然收到同樣的錯誤:無法找到操作或結果 沒有爲命名空間/和操作名稱映射的操作netbeans-tomcat-status-test。 - [未知位置] –

+0

@VishalTorne我會用你的實際代碼和配置打開一個新的問題;舊信息無法診斷。 –

+0

嗨戴夫我感謝您的幫助謝謝..我剛剛更新了問題,並添加了我的'result.jsp'文件。我得到相同的警告和值似乎沒有綁定屬性。 –

相關問題