我正在实现一个功能,以法语或英语自动翻译我的所有 JSP 页面。用户在导航中每时每刻都有两个按钮来更改语言。我使用了一个 servlet 来处理这种语言更改,但我不知道每次调用这个 servlet 时如何自动重定向到正确的 JSP 页面。这可能是微不足道的,但我不知道该怎么做,所以请帮助我......
我创建了一个 JSP 文件,只是为了存储一个标题,该标题将在几乎所有其他 JSP 中使用,以便为用户提供翻译服务:
在 header.jsp 中:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<div id="settings">
<p><fmt:message key="connection"/><c:out value=" ${sessionScope.firstname} ${sessionScope.name}"></c:out> <a href="../../disconnect"><fmt:message key = "disconnect"/></a></p>
<p id="translateText"><fmt:message key="translation"/> :
<a href="../../language?lang=fr" onclick=""><img src="../css/images/icone_fr.gif" alt="Français"></a>
<a href="../../language?lang=en" onclick=""><img src="../css/images/icone_en.gif" alt="English"></a>
</p>
</div>
</html>
然后我将此 jsp 包含在所有其他使用这些翻译功能的 JSP 文件中(例如:example.jsp):
示例.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!doctype html>
<html>
<!-- To initialize the language of the page -->
<c:set var="language" value="${not empty sessionScope.language ? sessionScope.language : not empty language ? language : pageContext.request.locale}" scope="session" />
<fmt:setLocale value="${language}" />
<c:choose>
<c:when test="${language=='fr'}"><fmt:setBundle basename="translation/message_fr"/></c:when>
<c:otherwise><fmt:setBundle basename="translation/message_en"/></c:otherwise>
</c:choose>
<head>
<meta charset="utf-8" />
<title><fmt:message key="platform"/>!</title>
<link rel="stylesheet" href="../css/styles.css" type="text/css" media="screen" />
<link rel="stylesheet" type="../text/css" href="css/print.css" media="print" />
<!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"> </script><![endif]-->
</head>
<body>
<div id="wrapper"><!-- #wrapper -->
<%@ include file = "header.jsp" %>
<section id="main"><!-- #main content and sidebar area -->
<section id="content"><!-- #content -->
<h1><fmt:message key="newProject"/></h1>
<article>
<p>blabla</p>
</article>
</section>
</section>
</div><!-- #wrapper -->
</body>
</html>
当用户单击“header.jsp”中的链接更改语言时,将调用“Language”servlet 并更改 sessionScope 中语言参数的值。这可以正常工作,但是我的 servlet 必须重定向(转发?)对原始 JSP 的响应,以便它再次以正确的语言显示,这对我不起作用。
我的问题是:我怎样才能找到或传递这个 JSP URL?我尝试了 request.getRequestURI() 但它返回的是 Servlet URI,而不是发送请求的 JSP。
这是我的小服务程序:
/**
* Servlet implementation class Language
*/
@WebServlet("/language")
public class Language extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Language() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String lang = request.getParameter("lang");
HttpSession session = request.getSession();
session.setAttribute("language", lang);
System.out.println(request.getRequestURI());//give the servlet URI
response.sendRedirect(//where ??????);
}
}
提前感谢您的帮助!