我有一个JSP问题(我使用Tomcat),我想在浏览器窗口中将标题设置为我的Config类中的字符串,您可以在下面看到,问题是我收到错误。我已经在互联网上搜索过这个问题,但对我来说没什么用。
索引文件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="test.*" %>
<%
Config cfg = new Config();
%>
<html>
<head>
<title><%= cfg.getSiteTitle() %></title>
</head>
<body>
$END$
</body>
</html>
配置类: 包装测试;
public class Config {
private String siteTitle = "Test";
public String getSiteTitle() {
return siteTitle;
}
public void setSiteTitle(String siteTitle) {
this.siteTitle = siteTitle;
}
}
Tomcat错误日志:
15-May-2016 12:35:52.483 SEVERE [http-nio-8080-exec-10]
org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service()
for servlet [jsp] in context with path [] threw exception [Unable to compile class for JSP:
An error occurred at line: 4 in the jsp file: /CMS/web/index.jsp
Config cannot be resolved to a type
1: <%@ page contentType="text/html;charset=UTF-8" language="java" %>
2: <%@ page import="test.*" %>
3: <%
4: Config cfg = new Config();
5: %>
6: <html>
7: <head>
An error occurred at line: 4 in the jsp file: /CMS/web/index.jsp
Config cannot be resolved to a type
1: <%@ page contentType="text/html;charset=UTF-8" language="java" %>
2: <%@ page import="test.*" %>
3: <%
4: Config cfg = new Config();
5: %>
6: <html>
7: <head>
Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 4 in the jsp file: /CMS/web/index.jsp
Config cannot be resolved to a type
1: <%@ page contentType="text/html;charset=UTF-8" language="java" %>
2: <%@ page import="test.*" %>
3: <%
4: Config cfg = new Config();
5: %>
6: <html>
7: <head>
An error occurred at line: 4 in the jsp file: /CMS/web/index.jsp
Config cannot be resolved to a type
1: <%@ page contentType="text/html;charset=UTF-8" language="java" %>
2: <%@ page import="test.*" %>
3: <%
4: Config cfg = new Config();
5: %>
6: <html>
7: <head>
Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:198)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java :61)
at java.lang.Thread.run(Thread.java:745)
我希望我能给你足够的信息,提前谢谢你!
答案 0 :(得分:0)
您的配置类无法解析为类型。这意味着 Config 类要么不在您的运行时类路径中,要么不在您的jsp中导入。
我注意到你导入了测试。*
如果 Config 类不直接在包测试下,则jsp将无法识别它。
在您导入测试的同一位置明确导入配置类。*应解决您的问题,如下所示:
<%@ page import="test.whatever.Config" %>
请,请不要在你的jsp中包含java代码。使用jsp作为视图,EL,servlet作为控制器。您将在Servlet中创建Config实例,然后将实例保存在请求/会话范围中,然后您可以使用EL访问jsp中的配置。搜索“MVC jsp servlet”将为您提供有关如何操作的大量信息。