Qing 1 anno fa
parent
commit
55eebe81b8

+ 100 - 15
springmvc-demo/.idea/workspace.xml

@@ -10,13 +10,15 @@
     <option name="autoReloadType" value="SELECTIVE" />
   </component>
   <component name="ChangeListManager">
-    <list default="true" id="d55c211f-cca4-4230-b149-ae8c1dab3c43" name="Changes" comment="0119 ssm全注解版">
+    <list default="true" id="d55c211f-cca4-4230-b149-ae8c1dab3c43" name="Changes" comment="0122 thymeleaf demo">
       <change afterPath="$PROJECT_DIR$/../.idea/VIPJAVA.iml" afterDir="false" />
       <change afterPath="$PROJECT_DIR$/../gn_oa_vip27/.idea/google-java-format.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/sql/org_fiction.sql" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/ScopeController.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/templates/testlist.html" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/templates/testscope.html" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/LoginController.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/resources/i18n/message.properties" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/resources/i18n/message_en_US.properties" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/resources/i18n/message_ko_KR.properties" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/resources/i18n/message_zh_CN.properties" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/templates/i18n/login.html" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/../02_JavaWeb/day06_mybatis/src/main/java/com/lc/mapper/UserMapper.java" beforeDir="false" afterPath="$PROJECT_DIR$/../02_JavaWeb/day06_mybatis/src/main/java/com/lc/mapper/UserMapper.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/../02_JavaWeb/day06_mybatis/src/main/resources/com/lc/mapper/UserMapper.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../02_JavaWeb/day06_mybatis/src/main/resources/com/lc/mapper/UserMapper.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/../02_JavaWeb/day07_mybatis/src/main/resources/com/lc/mapper/UserMapper.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../02_JavaWeb/day07_mybatis/src/main/resources/com/lc/mapper/UserMapper.xml" afterDir="false" />
@@ -25,7 +27,10 @@
       <change beforePath="$PROJECT_DIR$/../gn_oa_vip27/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../gn_oa_vip27/.idea/workspace.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/../gn_oa_vip27/src/main/java/com/sf/utils/JdbcUtil.java" beforeDir="false" afterPath="$PROJECT_DIR$/../gn_oa_vip27/src/main/java/com/sf/utils/JdbcUtil.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/main/java/com/sf/controller/HelloController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/HelloController.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/sf/anno/SpringMvcConfig.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/sf/anno/SpringMvcConfig.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/sf/dao/RoleDao.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/sf/dao/RoleDao.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/resources/spring-mvc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/spring-mvc.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/webapp/WEB-INF/templates/hello.html" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/templates/hello.html" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -44,10 +49,10 @@
         <option value="Spring Xml File" />
         <option value="Interface" />
         <option value="Mybatis Mapper Xml File" />
-        <option value="Properties File" />
-        <option value="Class" />
-        <option value="HTML File" />
         <option value="Thymeleaf HTML File" />
+        <option value="HTML File" />
+        <option value="Class" />
+        <option value="Properties File" />
       </list>
     </option>
   </component>
@@ -64,6 +69,9 @@
     <option name="FILTER_DEBUG" value="true" />
     <option name="CUSTOM_FILTER" />
   </component>
+  <component name="MarkdownSettingsMigration">
+    <option name="stateVersion" value="1" />
+  </component>
   <component name="ProjectColorInfo">{
   &quot;associatedIndex&quot;: 4
 }</component>
@@ -86,7 +94,7 @@
     "Tomcat Server.Tomcat 8.5.93.executor": "Run",
     "git-widget-placeholder": "master",
     "kotlin-language-version-configured": "true",
-    "last_opened_file_path": "/Users/Qing/IdeaProjects/SiFu/VIPJAVA/springmvc-demo/sql",
+    "last_opened_file_path": "/Users/Qing/IdeaProjects/SiFu/VIPJAVA/springmvc-demo/src/main/webapp/WEB-INF/templates/demo",
     "node.js.detected.package.eslint": "true",
     "node.js.detected.package.tslint": "true",
     "node.js.selected.package.eslint": "(autodetect)",
@@ -95,17 +103,17 @@
     "project.structure.last.edited": "Artifacts",
     "project.structure.proportion": "0.17833698",
     "project.structure.side.proportion": "0.2150838",
-    "settings.editor.selected.configurable": "preferences.pluginManager",
+    "settings.editor.selected.configurable": "File.Encoding",
     "vue.rearranger.settings.migration": "true"
   }
 }]]></component>
   <component name="RecentsManager">
     <key name="CopyFile.RECENT_KEYS">
+      <recent name="$PROJECT_DIR$/src/main/webapp/WEB-INF/templates/demo" />
       <recent name="$PROJECT_DIR$/sql" />
       <recent name="$PROJECT_DIR$" />
       <recent name="$PROJECT_DIR$/src/main/java/com/sf" />
       <recent name="$PROJECT_DIR$/src/main/resources" />
-      <recent name="$PROJECT_DIR$/src/main/webapp/WEB-INF" />
     </key>
     <key name="MoveFile.RECENT_KEYS">
       <recent name="$PROJECT_DIR$/src/main/java/com/sf/anno/init" />
@@ -248,7 +256,14 @@
       <workItem from="1705471908780" duration="12452000" />
       <workItem from="1705492873471" duration="19225000" />
       <workItem from="1705593304178" duration="747000" />
-      <workItem from="1705625830502" duration="15568000" />
+      <workItem from="1705625830502" duration="19845000" />
+      <workItem from="1705682750234" duration="2481000" />
+      <workItem from="1705748230368" duration="24000" />
+      <workItem from="1705845379372" duration="1255000" />
+      <workItem from="1705850774957" duration="649000" />
+      <workItem from="1705852976185" duration="1600000" />
+      <workItem from="1705897573911" duration="12996000" />
+      <workItem from="1705972424180" duration="5488000" />
     </task>
     <task id="LOCAL-00001" summary="0117 springmvc">
       <option name="closed" value="true" />
@@ -282,7 +297,23 @@
       <option name="project" value="LOCAL" />
       <updated>1705638546742</updated>
     </task>
-    <option name="localTasksCounter" value="5" />
+    <task id="LOCAL-00005" summary="0119 sql">
+      <option name="closed" value="true" />
+      <created>1705646050971</created>
+      <option name="number" value="00005" />
+      <option name="presentableId" value="LOCAL-00005" />
+      <option name="project" value="LOCAL" />
+      <updated>1705646050971</updated>
+    </task>
+    <task id="LOCAL-00006" summary="0122 thymeleaf demo">
+      <option name="closed" value="true" />
+      <created>1705916126882</created>
+      <option name="number" value="00006" />
+      <option name="presentableId" value="LOCAL-00006" />
+      <option name="project" value="LOCAL" />
+      <updated>1705916126882</updated>
+    </task>
+    <option name="localTasksCounter" value="7" />
     <servers />
   </component>
   <component name="TypeScriptGeneratedFilesManager">
@@ -291,11 +322,63 @@
   <component name="Vcs.Log.Tabs.Properties">
     <option name="OPEN_GENERIC_TABS">
       <map>
+        <entry key="2796529a-5c91-4ade-865d-9ec14a0a9732" value="TOOL_WINDOW" />
+        <entry key="8b8e1bf6-a623-40d3-b7bc-4bc664637900" value="TOOL_WINDOW" />
         <entry key="a504f1b5-4d9a-4e77-842b-fa8776f962fa" value="TOOL_WINDOW" />
       </map>
     </option>
     <option name="TAB_STATES">
       <map>
+        <entry key="2796529a-5c91-4ade-865d-9ec14a0a9732">
+          <value>
+            <State>
+              <option name="FILTERS">
+                <map>
+                  <entry key="branch">
+                    <value>
+                      <list>
+                        <option value="HEAD" />
+                      </list>
+                    </value>
+                  </entry>
+                  <entry key="structure">
+                    <value>
+                      <list>
+                        <option value="dir:/Users/Qing/IdeaProjects/SiFu/VIPJAVA/springmvc-demo" />
+                      </list>
+                    </value>
+                  </entry>
+                </map>
+              </option>
+              <option name="SHOW_ONLY_AFFECTED_CHANGES" value="true" />
+            </State>
+          </value>
+        </entry>
+        <entry key="8b8e1bf6-a623-40d3-b7bc-4bc664637900">
+          <value>
+            <State>
+              <option name="FILTERS">
+                <map>
+                  <entry key="branch">
+                    <value>
+                      <list>
+                        <option value="HEAD" />
+                      </list>
+                    </value>
+                  </entry>
+                  <entry key="structure">
+                    <value>
+                      <list>
+                        <option value="dir:/Users/Qing/IdeaProjects/SiFu/VIPJAVA/springmvc-demo" />
+                      </list>
+                    </value>
+                  </entry>
+                </map>
+              </option>
+              <option name="SHOW_ONLY_AFFECTED_CHANGES" value="true" />
+            </State>
+          </value>
+        </entry>
         <entry key="a504f1b5-4d9a-4e77-842b-fa8776f962fa">
           <value>
             <State>
@@ -329,7 +412,9 @@
     <MESSAGE value="0118 ssm整合" />
     <MESSAGE value="0118 springmvc示例" />
     <MESSAGE value="0119 ssm全注解版" />
-    <option name="LAST_COMMIT_MESSAGE" value="0119 ssm全注解版" />
+    <MESSAGE value="0119 sql" />
+    <MESSAGE value="0122 thymeleaf demo" />
+    <option name="LAST_COMMIT_MESSAGE" value="0122 thymeleaf demo" />
   </component>
   <component name="XSLT-Support.FileAssociations.UIState">
     <expand />

+ 35 - 4
springmvc-demo/src/main/java/com/sf/anno/SpringMvcConfig.java

@@ -2,12 +2,12 @@ package com.sf.anno;
 
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
-import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import org.springframework.context.support.ReloadableResourceBundleMessageSource;
+import org.springframework.web.servlet.config.annotation.*;
 import org.springframework.context.annotation.Bean;
 import org.springframework.web.servlet.ViewResolver;
-import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
-import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
+import org.springframework.web.servlet.i18n.SessionLocaleResolver;
 import org.springframework.web.servlet.resource.PathResourceResolver;
 import org.springframework.web.servlet.resource.WebJarsResourceResolver;
 import org.thymeleaf.spring6.SpringTemplateEngine;
@@ -16,6 +16,8 @@ import org.thymeleaf.spring6.view.ThymeleafViewResolver;
 import org.thymeleaf.templatemode.TemplateMode;
 import org.thymeleaf.templateresolver.ITemplateResolver;
 
+import java.util.Locale;
+
 // 当前类用来代替springMVC配置文件
 // 将当前类标识为一个配置类
 @Configuration
@@ -76,4 +78,33 @@ public class SpringMvcConfig implements WebMvcConfigurer {
                 .addResolver(new WebJarsResourceResolver())
                 .addResolver(new PathResourceResolver());
     }
+
+    @Bean
+    public ReloadableResourceBundleMessageSource messageSource(){
+        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
+        messageSource.setBasename("classpath:i18n/message");
+        return messageSource;
+    }
+
+    @Bean
+    public SessionLocaleResolver localeResolver(){
+        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
+//        localeResolver.setDefaultLocale(Locale.CHINA);
+        localeResolver.setDefaultLocale(Locale.US);
+        return localeResolver;
+    }
+
+    @Bean
+    public LocaleChangeInterceptor localeChangeInterceptor(){
+        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
+        interceptor.setParamName("lang");
+        return interceptor;
+    }
+
+    //拦截器
+    @Override
+    public void addInterceptors(InterceptorRegistry registry) {
+        registry.addInterceptor(localeChangeInterceptor());
+    }
+
 }

+ 23 - 0
springmvc-demo/src/main/java/com/sf/controller/LoginController.java

@@ -0,0 +1,23 @@
+package com.sf.controller;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.MessageSource;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+
+import java.util.Locale;
+
+@Controller
+public class LoginController {
+
+    @Autowired
+    private MessageSource messageSource;
+
+    @GetMapping("/localeChange")
+    public String localeChange(Locale locale) {
+        String userName = messageSource.getMessage("userName", null, locale);
+        String passWord = messageSource.getMessage("passWord", null, locale);
+        System.out.println(userName + " " + passWord);
+        return "i18n/login";
+    }
+}

+ 1 - 0
springmvc-demo/src/main/java/com/sf/dao/RoleDao.java

@@ -7,6 +7,7 @@ import java.util.List;
 public interface RoleDao {
 
     List<Role> findAll();
+
     Role findById(int id);
 
     int delete(int id);

+ 2 - 0
springmvc-demo/src/main/resources/i18n/message.properties

@@ -0,0 +1,2 @@
+userName=\u7528\u6237\u540D\uFF08\u9ED8\u8BA4\uFF09
+passWord=\u5BC6\u7801\uFF08\u9ED8\u8BA4\uFF09

+ 2 - 0
springmvc-demo/src/main/resources/i18n/message_en_US.properties

@@ -0,0 +1,2 @@
+userName=username
+passWord=password

+ 2 - 0
springmvc-demo/src/main/resources/i18n/message_ko_KR.properties

@@ -0,0 +1,2 @@
+userName=\uC0AC\uC6A9\uC790 \uC774\uB984
+passWord=\uC554\uD638

+ 2 - 0
springmvc-demo/src/main/resources/i18n/message_zh_CN.properties

@@ -0,0 +1,2 @@
+userName=\u7528\u6237\u540D
+passWord=\u5BC6\u7801

+ 18 - 0
springmvc-demo/src/main/resources/spring-mvc.xml

@@ -36,4 +36,22 @@
             </mvc:resolvers>
         </mvc:resource-chain>
     </mvc:resources>
+
+    <bean id="messageSource"
+          class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
+        <!-- 指定配置文件路径及basename -->
+        <property name="basename" value="classpath:i18n/message" />
+        <property name="fileEncodings" value="utf-8" />
+        <!-- 缓存120s,可以动态加载messages.properties中的数据(修改文件数据,不需要重启) -->
+<!--        <property name="cacheSeconds" value="120" />-->
+    </bean>
+    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
+        <property name="defaultLocale" value="zh_CN"/>
+    </bean>
+
+    <mvc:interceptors>
+        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
+            <property name="paramName" value="lang"/>
+        </bean>
+    </mvc:interceptors>
 </beans>

+ 2 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/hello.html

@@ -12,5 +12,7 @@ Hello,
 <br>
 <a th:href="@{/testList}">testList</a><br>
 <a th:href="@{/role/list}">roleList</a><br>
+<br>
+<a th:href="@{/localeChange}">localeChange</a><br>
 </body>
 </html>

+ 19 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/i18n/login.html

@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html lang="en" xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>Title</title>
+</head>
+<body>
+<!-- ${} controller中传过来的对象或数据 -->
+<!-- *{} controller中传过来的对象或数据通过th:object=${role}先获取出来
+    再直接获取其中的属性时使用  *{name} *{desc} ..-->
+<!-- @{} 链接  @{/login} ==> localhost:8080/springmvc_demo/login  -->
+<!-- #{} 要从properties中取数据 -->
+<h2 th:text="#{userName}+' '+#{passWord}"></h2>
+
+<a th:href="@{/localeChange(lang=en_US)}">英文</a>
+<a th:href="@{/localeChange(lang=ko_KR)}">韩文</a>
+<a href="localeChange?lang=zh_CN">中文</a>
+</body>
+</html>