没有针对命名空间struts 2映射的操作

时间:2015-11-04 17:51:56

标签: jsp struts2 javabeans

我将INR转换为美元,反之亦然。当我跑步时,我收到了这个警告:

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 个答案:

答案 0 :(得分:1)

评论太多了,所以这是您的代码审核。

<强> ConverterAction

  • 不要导入你不必要的东西。
  • 把重要的东西放在顶部(吸气剂和制定者并不重要)。
  • 与缩进和间距保持一致
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>

<强> 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>

(配置来自内存,已经有一段时间了。)

当您第一次请求页面时,您也会遇到问题,因为没有值,因此您将获得null所有地方。