JSP的代码
<div class="language">
<a id="ch" <%if (Locale.US.equals(session.getAttribute("locale"))) {%>
href="#" <%} else {%> class="ok" <%}%>>中文</a>
<a id="en"<%if (Locale.US.equals(session.getAttribute("locale"))) {%>
class="ok" <%} else {%> href="#" <%}%>>English</a>
</div>
JS代码
/***先找到class=language下的a标签,然后绑定click事件,function是是处理click事件的逻辑***/
$(".language a").bind("click", function(e) {
var loc = "";
if ($(this).attr("id") == "ch") {/**获取id的值是en还是ch**/
loc = "zh_CN";
}
if ($(this).attr("id") == "en") {
loc = "en_US";
}
$.ajax({/**利用Ajax调用异步请求,**/
url : vdi_base + "/sys/changeLanguage.do",
data : {locale:loc},
cache:false,
ifModified :true,
success : function(rs) {
window.location.reload();
}
});
});
Controller的处理 ,参考版的java代码
@Controller
@RequestMapping("/sys/*")
public class LoginController {
@RequestMapping
@ResponseBody
public String changeLanguage(HttpServletRequest request, HttpServletResponse response) {
String lang = request.getParameter("locale");
if ( lang != null) {
HttpSession session = request.getSession();
if (null != session) {
if ("zh_CN".equals(lang)) {
session.removeAttribute("locale");
session.setAttribute("locale", Locale.SIMPLIFIED_CHINESE);
} else if ("en_US".equals(lang)) {
session.removeAttribute("locale");
request.getSession().setAttribute("locale", Locale.US);
}
}
return "success";
} else {
return "lang is null";
}
}
}