在算法设计与分析的课本上学到了编辑距离算法,回到宿舍后,用Java实现了,但当我要放到网上时,就出现了问题。
1.先是输入两个字符串的页面,editdistance.jsp.
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%><html>
  <head>
  </head>
  
  <body>
       <h3>确定两字符串的编辑距离</h3>
         <form action="EditDistance" method="post">
           <s:textfield name="s" label="输入字符串一" required="true"></s:textfield><br/>
           <s:textfield name="t" label="输入字符串一" required="true"></s:textfield><br/>
           <s:submit value="提交"></s:submit>
    
         </form>
   
  </body>
</html>
接下来是web.xml和struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app><?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>
<package name="linyu" extends="struts-default">
<action name="EditDistance" class="linyu.action.EditDistance">
  <result>/success.jsp</result>
</action>
</package>
</struts>
接下来是EditDistance.java
package linyu.action;
import com.opensymphony.xwork2.ActionSupport; public class EditDistance extends ActionSupport{
public String s="a";
public String t="b";
public String getS() {
return s;
} public void setS(String s) {
this.s = s;
} public String getT() {
return t;
} public void setT(String t) {
this.t = t;
}

    private static int Minimum(int a, int b, int c) {
        int mi;         mi = a;
        if (b < mi) {
            mi = b;
        }
        if (c < mi) {
            mi = c;
        }
        return mi;
    }     
    public String excute() throws Exception{
        int d[][]; // matrix
        int n; // length of s
        int m; // length of t
        int i; // iterates through s
        int j; // iterates through t
        char s_i; // ith character of s
        char t_j; // jth character of t
        int cost; // cost
        String editdistance;
        
                 // Step 1         n = s.length();
        m = t.length();
        if (n == 0) {
         editdistance=String.valueOf(m);
        }
        if (m == 0) {
         editdistance=String.valueOf(n);
        }
        d = new int[n + 1][m + 1];         // Step 
        for (i = 0; i <= n; i++) {
            d[i][0] = i;
        }         for (j = 0; j <= m; j++) {
            d[0][j] = j;
        }         // Step 3         for (i = 1; i <= n; i++) {
            s_i = s.charAt(i - 1);
            // Step 4
            for (j = 1; j <= m; j++) {
                t_j = t.charAt(j - 1);
                // Step 5
                if (s_i == t_j) {
                    cost = 0;
                } else {
                    cost = 1;
                }
                // Step 6
                d[i][j] = Minimum(d[i - 1][j] + 1, d[i][j - 1] + 1,
                        d[i - 1][j - 1] + cost);
            }
        }
        // Step 7
        editdistance=String.valueOf(d[n][m]);
        return SUCCESS;     }
}
最后是返回成功页面,success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%><html>
  <head>
  </head>
  <body>       <br> 您输入的字符串是<s:property value="s"/>和<s:property value="t"/><br/>
        他们的编辑距离是:<s:property value="editdistance"/>
   
  </body>
</html>
完成后,进入editdistance.jsp页面,输入两个字串,点击提交后,就显示The requested resource (/suanfashejiyufenxi/EditDistance) is not available.我知道我的Action写错了,但错在哪里呢?要怎么改。积分不多,只能七十分喽。

解决方案 »

  1.   

    struts.xml文件要放到src目录下,发布后在WEB-INF/classes/
      

  2.   

    是不是EditDistance.java没有编译啊?提示找不到这个文件,那就顺藤摸瓜,看看路径和配置,代码好像没有什么问题。
      

  3.   

    哦。我在EditDistance.java中直接赋值其实是我后来的行为,如果这个不看的话,也就是我能从页面中读取s,t的值,但是我还是照样不能求出编辑距离呀,也就是EditDistance.java,execute方法不能执行呀。求解。
      

  4.   

    貌似配置的问题,试下访问http://localhost:8080/应用名称/EditDistance.do
      

  5.   

    <form action="/EditDistance.action" method="post">