在JSP页面中动态导入文件
我希望我的 JSP 页面包含基于请求中的值的另一个页面。 所以我设置了以下内容:
request.setAttribute("chosenLang", "NL");
RequestDispatcher dispatcher = request.getRequestDispatcher(address);
dispatcher.forward(request, response);
当我到达“地址”页面时,我尝试执行以下操作:
<c:choose>
<c:when test="${chosenLang eq 'NL'}">
<%@include file="/Localization/NL_Localization.jsp" %>
</c:when>
<c:otherwise>
<%@include file="/Localization/EN_Localization.jsp" %>
</c:otherwise>
</c:choose>
所以,我知道 selectedLang 找到“NL”属性,因为当我将 和 标签之间的内容更改为 < ;c:out value="Test" />
它有效,我看到我的页面上提到了测试,当我在那里放入 2 个值(一个用于 NL,一个用于其他语言)时,它会发生变化以及..
不过,真正的错误是当我尝试使用 Localization.jsp 文件之一中定义的字符串时出现的,如下所示:
<%
String welcomeStr="this is defining String variable";
%>
当我在页面下方的某个位置调用该字符串时,例如
<%=welcomeStr%>
我的 IDE 不会警告我有问题错误的。但是当我编译并运行时,出现以下错误:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 48 in the jsp file: /index3.jsp
welcomeStr cannot be resolved
45: <li>My courses</li>
46: <li>My messages [1]</li>
47: <li>My details</li>
48: <li> <%=welcomeStr%></li>
49: </ul>
50: </div>
51: <div id="menuHolder">
那么,我在这里缺少什么? 或者以其他方式完成更方便?
提前致谢!
I want my JSP page to include another page based on a value in the Request.
So i set the following:
request.setAttribute("chosenLang", "NL");
RequestDispatcher dispatcher = request.getRequestDispatcher(address);
dispatcher.forward(request, response);
And when i get to the 'address' page i try to do the following:
<c:choose>
<c:when test="${chosenLang eq 'NL'}">
<%@include file="/Localization/NL_Localization.jsp" %>
</c:when>
<c:otherwise>
<%@include file="/Localization/EN_Localization.jsp" %>
</c:otherwise>
</c:choose>
So, i know the chosenLang finds the 'NL' attribute, because when I change whats between the and tags to <c:out value="Test" />
it works, I see the test get mentioned on my page, and when i put 2 values in there, one for NL and one for some other language, it changes aswell..
The real error though, comes when I try to use a string that's defined in one of the Localization.jsp files, like so:
<%
String welcomeStr="this is defining String variable";
%>
And when I call that string somewhere down in my page like
<%=welcomeStr%>
my IDE doesnt warn me that there's something wrong. But when I compile and run I get the following error:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 48 in the jsp file: /index3.jsp
welcomeStr cannot be resolved
45: <li>My courses</li>
46: <li>My messages [1]</li>
47: <li>My details</li>
48: <li> <%=welcomeStr%></li>
49: </ul>
50: </div>
51: <div id="menuHolder">
So, what am I missing here?
Or is this done more convenient in another way?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试查看 ResourceBundle。这将允许您编写 1 个 JSP 文件,页面上的所有措辞都将在属性文件中进行控制。这将避免您必须在 jsp 文件中编写逻辑来控制语言。
您的 JSP 页面可以从属性文件中读取,每个属性文件都有不同语言/国家的措辞。
有很多关于如何使用 ResourceBundle 的教程。这是来自 Oracle 的一个
Try looking at ResourceBundle. What this will allow you to do is write 1 JSP file, and all the wording on the page will be controlled in property files. This will avoid you having to write logic in your jsp file to control the language.
Your JSP page can read from property files, each of which will have your wording for different languages/countries.
There are lots of tutorials out there on how to use ResourceBundle. Here is one from Oracle