开发者

Dynamically importing a file in a JSP page

开发者 https://www.devze.com 2023-01-24 08:37 出处:网络
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\");

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.getRequestDispat开发者_JAVA百科cher(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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消