顯示具有 MVC 標籤的文章。 顯示所有文章
顯示具有 MVC 標籤的文章。 顯示所有文章

2011年3月23日 星期三

Spring MVC View 中文亂碼問題

最近在學習 Spring MVC 的過程中, 發現前端顯示中文會亂碼,

後來找到了解決的方式,

1. 就是在 web.xml 檔中, 新增 filter 機制

...
<filter>
  <filter-name>SetCharacterEncoding</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>SetCharacterEncoding</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
...

2. JSP 網頁也必須設定 contentType
<%@page contentType="text/html; charset=UTF-8" %>

這樣子就可以解決中文亂碼的問題!!!

2011年1月4日 星期二

.Net MVC2 的 Html.TextBoxFor 加入 Html Attributes

一般在使用 .Net MVC2 時, 都會使用 Html.TextBoxFor 方式產生前端的

HTML Tag, 如果想要加入 Html Tag 的 Attributes 參數時, 可以使用以

下的做法

...
<%: Html.TextBoxFor(m => m.UserName, new { @class = "ui-widget-content ui-corner-all" })%>
...

2010年12月28日 星期二

Spring MVC Demo

最近研究了一下 Spring MVC 的架構

大該了解基本的用法

在此分享 Spring MVC Demo 程式

DemoWeb 架構以
Spring MVC + Hibernate + Annotation
為主架構, 是目前業界還滿流行的架構之一, 如果前端想使用JQuery也是沒問題
的, 至於後端 DB 的話因為是使用 Hibernate 做為 ORM, 所以基本上並不會有
Databases Dependency 的問題, 只要找到相關 DB 的 JDBC 就可以切換了

請點選下載 DemoWeb 程式
或至 DemoWeb (github) 下載程式

開發工具為 SpringSource Tool Suite

DemoWeb 所用的 Databases 目前有二種, 可任選其中一種作測試
1. 設定 SQLite (DemoWeb程式預設)
  a. 將 DemoWeb 下載解壓後, 資料夾下有一個 SQLite DB 檔 demowebdb.db3
  b. 將 demowebdb.db3 檔放至 C:\DemoWebDB\ 資料夾下就可以了

2. 設定 MySQL
  a. 將 DemoWeb 下載解壓後, 資料夾下有一個 demowebdb_schema.sql
  b. 執行此 SQL 檔後, 將會建立 demowebdb
  c. 開啟 WEB-INF\jdbc.properties 檔, 更改 MySQL 登入的 UserName 跟
      Password 參數
# MySQL Setting
datasource.mysql.driverClassName=com.mysql.jdbc.Driver
datasource.mysql.dialect=org.hibernate.dialect.MySQLDialect
datasource.mysql.demowebdb.url=jdbc:mysql://localhost:3306/demowebdb?useUnicode=true&characterEncoding=UTF-8
datasource.mysql.demowebdb.username=xxxxx
datasource.mysql.demowebdb.password=xxxxx
...
   d. 開啟 WEB-INF\spring\appServlet\servlet-context.xml 檔, 更改
       DataSource 的參數, 先找到以下區段
...
<beans:bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
  <beans:property name="driverClass" value="${datasource.sqlite.driverClassName}"/>
  <beans:property name="jdbcUrl" value="${datasource.sqlite.demowebdb.url}"/>
  <beans:property name="user" value="${datasource.sqlite.demowebdb.username}"/>
  <beans:property name="password" value="${datasource.sqlite.demowebdb.password}"/>
</beans:bean>
...
      修改成
...
<beans:bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
  <beans:property name="driverClass" value="${datasource.mysql.driverClassName}"/>
  <beans:property name="jdbcUrl" value="${datasource.mysql.demowebdb.url}"/>
  <beans:property name="user" value="${datasource.mysql.demowebdb.username}"/>
  <beans:property name="password" value="${datasource.mysql.demowebdb.password}"/>
</beans:bean>
...

    再來就是執行程式作測試啦!!!!