日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring MVC-08循序渐进之国际化(基于Session-SessionLocaleResolver)

發布時間:2025/3/21 javascript 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring MVC-08循序渐进之国际化(基于Session-SessionLocaleResolver) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  • 概述
  • 工程結構
  • 測試
  • 源碼

概述

在Spring MVC中選擇語言區域,可以使用語言解析器Bean,它包括幾個實現,如下

  • AcceptHeaderLocaleResolver
  • SessionLocaleResolver
  • CookieLocaleResolver
  • 其中上篇博文 已經已經講解了 Spring MVC-08循序漸進之國際化(AcceptHeaderLocaleResolver)

    接下來我們來通過SessionLocaleResolver來實現國際化


    工程結構

    實體類 標注了JSR303校驗

    package com.artisan.domain; import java.io.Serializable;import javax.validation.constraints.Size;import org.hibernate.validator.constraints.NotBlank;public class Product implements Serializable {private static final long serialVersionUID = 78L;@NotBlank@Size(min=1, max=10)private String name;private String description;private Float price;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public Float getPrice() {return price;}public void setPrice(Float price) {this.price = price;} }

    控制層

    package com.artisan.controller;import javax.validation.Valid;import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping;import com.artisan.domain.Product;@Controller @RequestMapping("/product") public class ProductController {private static final Log logger = LogFactory.getLog(ProductController.class);@RequestMapping(value="/product_input")public String inputProduct(Model model) {model.addAttribute("product", new Product());return "ProductForm";}@RequestMapping(value="/product_save")public String saveProduct(@Valid @ModelAttribute Product product, BindingResult bindingResult,Model model) {// 校驗if (bindingResult.hasErrors()) {FieldError fieldError = bindingResult.getFieldError();logger.info("Code:" + fieldError.getCode() + " ,field:" + fieldError.getField());return "ProductForm";}// save product heremodel.addAttribute("product", product);return "ProductDetails";}}

    spring mvc配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 掃描控制層的注解,使其成為Spring管理的Bean --><context:component-scan base-package="com.artisan.controller" /><!-- 靜態資源文件 --><mvc:annotation-driven /><mvc:resources mapping="/css/**" location="/css/" /><mvc:resources mapping="/*.jsp" location="/" /><!-- 視圖解析器 --><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!-- 國際化資源配置,資源文件綁定器--><bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><!-- 多語言配置的路徑--><property name="basename" value="/WEB-INF/resource/labels" /><!-- 如果在國際化資源文件中找不到對應代碼的信息,就用這個代碼作為名稱 --><property name="useCodeAsDefaultMessage" value="true" /></bean><!--cookie方式 --> <!-- <bean id="cookieLocaleResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" /> --> <!-- 動態切換國際化 ,國際化放在session中 --><bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/><!-- 國際化操作攔截器 如果采用基于(請求/Session/Cookie)則必需配置 --><mvc:interceptors><bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"><!-- 通過這個參數來決定獲取那個配置文件 同頁面?lang --><property name="paramName" value="lang" /></bean></mvc:interceptors></beans>

    國際化資源文件

    labels_en.properties

    label.productName=Product Name label.description=Description label.price=Price button.reset=Reset button.submit=Add Product form.name=Product Form page.productform.title=Add Product

    labels_zh.properties

    label.productName=\u4ea7\u54c1\u540d\u79f0 label.description=\u63cf\u8ff0 label.price=\u4ef7\u683c button.reset=\u91cd\u7f6e button.submit=\u63d0\u4ea4 form.name=\u4ea7\u54c1\u8868\u5355 page.productform.title=\u4EA7\u54C1\u8868\u5355

    labels.properties

    label.productName=Product Name label.description=Description label.price=Price button.reset=Reset button.submit=Add Product form.name=Product Form page.productform.title=(default)New Product Form

    頁面

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML> <html> <head> <title><spring:message code="page.productform.title"/></title> <style type="text/css">@import url("<c:url value="/css/main.css"/>");</style> </head> <body><div id="global">Current Locale : ${pageContext.response.locale} <br/>Language : <a href="?lang=en">English</a>|<a href="?lang=zh">Chinese</a><form:form commandName="product" action="product_save" method="post"><fieldset><legend><spring:message code="form.name" /></legend><p><label for="name"><spring:message code="label.productName" text="default text" />:</label><form:input id="name" path="name" cssErrorClass="error"/><form:errors path="name" cssClass="error"/></p><p><label for="description"><spring:message code="label.description"/>: </label><form:input id="description" path="description"/></p><p><label for="price"><spring:message code="label.price" text="default text" />: </label><form:input id="price" path="price" cssErrorClass="error"/></p><p id="buttons"><input id="reset" type="reset" tabindex="4"value="<spring:message code="button.reset"/>"><input id="submit" type="submit" tabindex="5" value="<spring:message code="button.submit"/>"></p></fieldset> </form:form> </div> </body> </html>

    測試

    tomcat中運行后,進行驗證

    選擇英文

    選擇中文


    源碼

    代碼已提交到github

    https://github.com/yangshangwei/SpringMvcTutorialArtisan

    總結

    以上是生活随笔為你收集整理的Spring MVC-08循序渐进之国际化(基于Session-SessionLocaleResolver)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。