すべての JSP ページを自動的にフランス語または英語に翻訳する機能を実装しています。ユーザーは、言語を変更するために、ナビゲーションで常に 2 つのボタンを使用します。この言語の変更に対処するためにサーブレットを使用しましたが、このサーブレットが呼び出されるたびに適切な 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 ファイル (例: example.jsp) にこの jsp を含めます。
example.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」サーブレットが呼び出され、sessionScope の言語パラメータの値が変更されます。これは適切に機能しますが、サーブレットは元の JSP への応答をリダイレクト (転送?) して、適切な言語で再度表示する必要があり、それがうまくいきません。
私の質問は次のとおりです。この JSP URL を検索または渡すにはどうすればよいですか? request.getRequestURI() を試しましたが、リクエストを送信する JSP ではなく、サーブレット URI が返されます。
ここに私のサーブレットがあります:
/**
* 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 ??????);
}
}
助けてくれてありがとう!