我正在尝试国际化headerMessage,它在访问StudentAdmissionController
之后从AdmissionForm.jsp
传递到studentmessages_de.properties
,而studentmessages_en.properties
取决于浏览器语言,但我不这样做有任何想法如何为传递的headerMessage管理它?
我提供任何帮助。
StudentAdmissionController
package com.stack;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.validation.Valid;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class StudentAdmissionController {
@InitBinder
public void iniBinder(WebDataBinder binder){
//binder.setDisallowedFields(new String[] {"studentMobile"});
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
binder.registerCustomEditor(Date.class, "studentDOB", new CustomDateEditor(dateFormat, false));
//add to handle a specific data.
binder.registerCustomEditor(String.class,"studentName", new StudentNameEditor());
}
@RequestMapping(value = "/admissionForm.html", method = RequestMethod.GET)
public ModelAndView getAdmissionForm() {
ModelAndView model = new ModelAndView("AdmissionForm");
return model;
}
// Aded
// This method is being called by every request.
@ModelAttribute
public void addingCommonObjects(Model model1) {
model1.addAttribute("headerMessage", "University of Berlin, Germany ");
}
@RequestMapping(value = "/submitAdmissionForm.html", method = RequestMethod.POST)
public ModelAndView submitAdmissionForm(@Valid @ModelAttribute("student1") Student student1, BindingResult result) {
if (result.hasErrors()) {
ModelAndView model = new ModelAndView("AdmissionForm");
return model;
}
ModelAndView model = new ModelAndView("AdmissionSuccess");
return model;
}
}
AdmissionForm.jsp
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<body>
<h1>${headerMessage}</h1>
<h1> <spring:message code="label.admissionForm" /></h1>
</body>
</html>
studentmessages_en.properties
label.admissionForm = STUDENT ADMISSION FROM ENGINEERING COURSES
label.headerMessage = University of Berlin, Germany
studentmessages_de.properties
label.admissionForm = STUDENTEN ZULASSUNG FÜR ENGINEERING Lehrveranstaltungen
label.headerMessage = Universität zu Berlin, Deutschland
答案 0 :(得分:0)
使用ResourceBundle,您可以加载适合当前用户区域设置的特定于语言环境的资源包。
1.根据servlet的语言环境加载ResourceBundle:
ResourceBundle messages = ResourceBundle.getBundle("studentmessages_en");
ServletContext ctx = request.getSession().getServletContext();
ctx.setAttribute("resourceBundle", messages);
2.获取JSP
<% ServletContext ctx=this.getServletContext();
ResourceBundle res=(ResourceBundle)ctx.getAttribute("resourceBundle"); %>
<%=res.getString("label.headerMessage") %>