JSP + Eclipse + TomCat:无法解析为类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11534156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSP + Eclipse + TomCat: cannot be resolved to a type
提问by hinafu
I know this question has been asked a lot of times, but so far, none of the answers previously given have worked for me, here is my code:
我知道这个问题已经被问过很多次了,但到目前为止,以前给出的答案都没有对我有用,这是我的代码:
<html>
<head>
<title>Programa</title>
</head>
<body>
<div>
Introduzca una palabra a buscar:
<input type="text" id="INPUT">
<%@ page import="java.net.URL" %>
<%@ page import="com.google.gson.*" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.io.InputStreamReader" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.Reader" %>
<%@ page import="javax.swing.*" %>
<%@ page import="java.awt.event.*;" %>
<%!
int min(int a,int b) {
return (a>b)? b:a;
}
int edit_distance(String a,String b) {
int n = a.length(), m = b.length(),costo;
int[][] mat = new int[n+1][m+1];
for(int i=0;i<=n;++i) mat[i][0] = i;
for(int j=0;j<=m;++j) mat[0][j] = j;
for(int i=1;i<=n;++i) {
for(int j=1;j<=m;++j) {
costo = a.charAt(i-1) == b.charAt(j-1)? 1 : 0;
mat[i][j] = min(min(mat[i-1][j] + 1,mat[i-1][j-1] + costo),mat[i][j-1] + 1);
}
}
return mat[n][m];
}
String resultados_de_la_busqueda(String search) { //Básicamente lo que hace esta función es devolverte una cadena con los resultados de la búsqueda
StringBuffer RES = new StringBuffer("<html>");
String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="; //El enlace para buscar en google
String charset = "UTF-8";
URL url;
Reader reader;
try {
url = new URL(google + URLEncoder.encode(search, charset));
try {
reader = new InputStreamReader(url.openStream(), charset);
GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
for(int i=0;i<3;++i) {
RES.append(results.getResponseData().getResults().get(i).getTitle());
RES.append("<br/>");
RES.append("<a href=\"");
RES.append(results.getResponseData().getResults().get(i).getUrl());
RES.append("\">" + results.getResponseData().getResults().get(i).getUrl() + "</a>");
RES.append("<br/><br/>");
}
} catch(Exception e) {}
} catch(Exception e) {}
return RES.toString();
}
%>
<%
//Se supone que acá debo mostrar el resultado de la búsqueda...
out.println(resultados_de_la_busqueda("taringa"));
%>
</div>
</body>
</html>
And here is the error I'm getting:
这是我得到的错误:
org.apache.jasper.JasperException: No se puede compilar la clase para JSP:
Ha tenido lugar un error en la línea: 47 en el archivo jsp: /Programa.jsp
GoogleResults cannot be resolved to a type
Gson cannot be resolved to a type
Any idea on what is causing the problem?
关于导致问题的原因有什么想法吗?
Edit 1:
编辑1:
This is the GoogleResults class:
这是 GoogleResults 类:
import java.util.List;
public class GoogleResults {
private ResponseData responseData;
public ResponseData getResponseData() { return responseData; }
public void setResponseData(ResponseData responseData) { this.responseData = responseData; }
public String toString() { return "ResponseData[" + responseData + "]"; }
static class ResponseData {
private List<Result> results;
public List<Result> getResults() { return results; }
public void setResults(List<Result> results) { this.results = results; }
public String toString() { return "Results[" + results + "]"; }
}
static class Result {
private String url;
private String title;
public String getUrl() { return url; }
public String getTitle() { return title; }
public void setUrl(String url) { this.url = url; }
public void setTitle(String title) { this.title = title; }
public String toString() { return "Result[url:" + url +",title:" + title + "]"; }
}
}
回答by chandhooguy
Seems like its not picking up your class...
好像它没有接你的课...
Do a Project>Clean and then Refresh it (F5).
做一个项目>清理然后刷新它(F5)。
Hoped this helped, here is where I got it from:
希望这有帮助,这是我从这里得到的:
回答by jatin kashyap
Sometimes if you are importing your project into a new workspace, this problem is encountered if JRE was different of the previous workspace frm the new workspace, try changing the jre system library in build path. It works out
有时,如果您将项目导入到新工作区,如果 JRE 与新工作区的先前工作区不同,则会遇到此问题,请尝试更改构建路径中的 jre 系统库。它有效
回答by Meltzer
This Happen to me after my java was automatically updated. Check The JRE is correctly set: RClick on the project , choose Build Path ->configure Build Path , Go to libraries Path and make sure JRE System library is properly configured.
这发生在我的 java 自动更新后。检查 JRE 是否正确设置: R单击项目,选择 Build Path ->configure Build Path ,转到 libraries Path 并确保正确配置了 JRE System 库。
回答by nitind
Always put your classes in packages.
始终将您的课程放在包中。
回答by Alex Beaupré
Make sure your GoogleResults.class is being compiled under WEB-INF/classes/
确保您的 GoogleResults.class 正在 WEB-INF/classes/ 下编译
Also it seems to me like you should import this file at the beginning of your .jsp.
在我看来,您应该在 .jsp 的开头导入这个文件。
Hope this help
希望这有帮助
回答by vitralyoz
Create new project. Copy all files in this new project from old project. Delete old one and rename new project.
创建新项目。从旧项目复制这个新项目中的所有文件。删除旧项目并重命名新项目。
Thats it. It will work fine
就是这样。它会正常工作