天天看點

中文代碼示例之Spring Boot 1.3.3示範

源碼: program-in-chinese/jinxiaocun 由于這個示範項目成型于去年(詳見 中文程式設計的嘗試曆程小記 ), Spring Boot還是老版本. 尚未将其更新到最新版本, 先将其中的一些中文命名的部分小結在此.

URL

如: /商品表 /機關表

實作方式見最後附上的"機關控制器"源碼中的:

@RequestMapping(value = "/機關表")           

protected static final String URL = "機關表";           

類/方法/變量用中文命名

見後面附上的源碼

jsp檔案名, 以及jsp中的變量名

如"機關表.jsp"中的"${機關.名稱}".

這需要"機關"類中的屬性命名為"名稱". 由于命名約定, 需要将接口以get/set開頭.

資料庫的表/列命名

下面是mysqldump的結果("機關"部分)

DROP TABLE IF EXISTS `機關`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `機關` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `名稱` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;           

完整schema在

編碼相關注意點(現在想起的, 歡迎補遺)

需要在pom.xml中添加:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>           
  1. 資料庫(用的Mysql)指定編碼:
spring.datasource.url= jdbc:mysql://localhost:3306/jinxiaocun?useUnicode=true&characterEncoding=utf8
spring.datasource.sqlScriptEncoding=UTF-8           
  1. jsp檔案頭指定編碼:
<%@ page pageEncoding="UTF-8" %>           
  1. 如上面的mysqldump中所見, 表和列都設定為了utf8

附上機關相關部分源碼

機關類:

@Entity
public class 機關 {

  private long id;

  @NotEmpty(message = "機關名稱不可為空")
  @Size(max = 20, message = "機關長度不可超過20")
  private String 名稱;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String get名稱() {
    return 名稱;
  }

  public void set名稱(String 名稱) {
    this.名稱 = 名稱;
  }
}           

機關控制類:

@Controller
@RequestMapping(value = "/機關表")
public class 機關控制 {

  protected static final String URL = "機關表";
  protected static final String 表名 = "機關表";

  @Autowired
  private 機關庫 機關庫;

  @RequestMapping(method = RequestMethod.GET)
  public String 清單(Model 模型) {
    List<機關> 表 = 機關庫.findAll();
    if (表 != null) {
      模型.addAttribute(表名, 表);
    }
    // 需要初始化被校驗的對象
    模型.addAttribute("機關", new 機關());
    return URL;
  }

  @RequestMapping(method = RequestMethod.POST)
  public String 添加(@Valid 機關 機關, BindingResult 限制結果, Model 模型) {
    if (限制結果.hasErrors()) {
      return URL;
    }
    機關庫.save(機關);
    return 清單(模型);
  }
}           

機關表.jsp:

<body>
    <jsp:include page="置頂.jsp" />
    <h2>機關清單</h2>
    <c:forEach var="機關" items="${機關表}">
        <div>
            <c:out value="${機關.名稱}"/>
        </div>
    </c:forEach>
    
    <h3>添加機關</h3>
    <form:form method="POST" modelAttribute="機關">
        <form:errors path="*" cssClass="errorblock" element="div" />
        <label for="名稱">名稱:</label>
        <form:input type="text" path="名稱" size="50" />
        <form:errors path="名稱" cssClass="error"/>
        <br/>
        <input type="submit" value="送出"></input>
    </form:form>
</body>           

示範本身非常幼稚, Spring Boot當時也是摸索學習中, 後也沒有繼續深入. 使用jsp而不是Thymeleaf的原因是, 後者當時不支援中文變量命名. 緣由詳見

業餘小項目, 學用Spring boot (如對中文寫代碼本能排斥, 求放過)

22樓.

2017-11-25