2015-11-04 71 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'文件。我得到相同的警告和值似乎没有绑定属性。 –

相关问题