通常我的jsps在开始时包含以下内容
<%@ page contentType="text/html"%>
<%@ page errorPage="error.jsp"%>
<%! String language ="en"; %>
<%@ include file="/layout/inc/pagestart.jsp" %>
和pagestart.jsp包含
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="<%=language%>">
工作正常。
但是如果主jsp不包含语言变量defn,则编译失败并带有
An error occurred at line: 2 in the jsp file: /layout/inc/pagestart2.jsp
language cannot be resolved to a variable
1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2: <html lang="<%=language%>">
3: <%@ include file="/layout/inc/head.jsp" %>
4: <%@ include file="/layout/inc/bodystart.jsp" %>
5: <%@ include file="/layout/inc/header.jsp" %>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:469)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
如何修改pagestart.jsp,使其默认为输出
<html lang="en">
如果变量尚未设置
解决方案基于Alex Clems的回答 如果调用页面没有设置属性
,这仍然有效pagestart.jsp
<%! String language = "en"; %>
<%
String lang = (String) request.getAttribute("language");
if(lang != null) {
language = lang;
}
out.print("<html lang=\""+language+"\">");
%>
callingjsp.jsp
<%@ page contentType="text/html"%>
<%@ page errorPage="error.jsp"%>
<% request.setAttribute("language", "de"); %>
<%@ include file="/layout/inc/pagestart.jsp" %>
答案 0 :(得分:0)
只检查变量是否存在,如果存在,则输出您需要的内容,如果它没有输出您的默认值。
在您的第一个JSP中更改您设置language
的方式:
request.setAttribute("language", "en");
因此,在第二个JSP中,您可以访问language
:
String language = (String) request.getAttribute("language");
<% if(language != null) {
out.print("<html lang=\"" + language + "\">");
} else {
out.print("<html lang=\"en\">");
}
%>
答案 1 :(得分:0)
AFAIK ..不,除非在运行时出现编译错误,否则无法检查重复。
在包含页面的场景中,最好的解决方案是将varible命名为,它包含前缀(区分大小写)的文件名,如下所示。
示例:myvariable.jsp
<%@ page contentType="text/html"%>
<%@ page errorPage="error.jsp"%>
<%! String language ="en"; %>
<%@ include file="/layout/inc/pagestart.jsp" %>
pagestart.jsp包含
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="<%=language%>">
重命名myvariable.jsp中的变量
<%@ page contentType="text/html"%>
<%@ page errorPage="error.jsp"%>
<%! String myvariable_jsp_Language ="en"; %>
<%@ include file="/layout/inc/pagestart.jsp" %>
pagestart.jsp包含
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="<%=myvariable_jsp_Language>">
注意:由于声誉不佳,我无法添加评论,因此不得不将此作为答案。不要低估这一点,而是通过评论建议更正。
答案 2 :(得分:0)
尝试使用pageContext
:
calling.jsp:
<% pageContext.setAttribute("language", "en"); %>
<jsp:directive.include file="pagestart.jsp"/>
pagestart.jsp:
<html lang="${language}">
或使用jsp:include
calling.jsp:
<jsp:include page='pagestart.jsp'>
<jsp:param name="language" value="en"/>
</jsp:include>
pagestart.jsp:
<html lang="${param.language}">