網頁

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

2013年9月28日 星期六

Spring、J2EE學習筆記-submit中文的問題

同樣的錯又犯了一次,使用spring或j2ee開發的話,若submit的資料中有中文字,要在取得資料時,先執行以下指令,這邊做個紀錄,免得以後又犯

request.setCharacterEncoding("UTF-8");

另外查了下資料,也有幾個地方要注意
1. 在web.xml中要加入
<filter>

    <filter-name>characterEncodingFilter</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>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>


2. JSP中要加入
<%@page contentType="text/html" pageEncoding="UTF-8"%>

3. server.xml中的conntector要加入useBodyEncodingForURI
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" useBodyEncodingForURI="true"/>



參考網址:

2013年9月16日 星期一

Spring學習筆記-編譯spring 3.2.X

因為耍笨想要玩新玩意,莫名其妙變成在搞spring,spring目前不再提供jar檔,所以像我一樣老派的,就只能想辦法,最後找到這個Building a distribution with dependencies,還好其實還是有提供,大概步驟如下:

Step1. 將Github的master改成3.2.x再下載成zip或整個git下載
Step2. 解開zip後進入根目錄(例如我的是spring-framework-3.2.x)
Step3. 執行./gradlew depsZip(我是用mac,windows改用.bat)

再來就慢慢的等吧....最後在src下的每個專案都有各自的jar檔在目錄下

2013年8月29日 星期四

Spring、J2EE、Hibernate學習筆記-could not initialize proxy - no Session

記錄一種狀況,在spring中使用hibernate是很常有的事,尤其在hbm中將兩個table的資料做聯集,不過遇到個狀況,記錄如下:

先取得人員的資訊(其中聯集到此人員的角色資訊),但事後要用時卻出現Exception如下:
could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session


看起來是預設聯集另一個table時是用lazy的方式,加上用的是spring的
HibernateTemplate,會自動close session,在hbm file那個field的property attribute加上lazy="false",這樣就ok了

2012年11月21日 星期三

Spring、J2EE學習筆記-數字百分比呈現,是double? int?

不知道是不是太久沒寫java的程式了,今天只是想寫個呈現百分比的字串,結果....嗯~先這樣講好了,我寫成以下樣子

int x = 20;
int y = 100;
double z = x / y;
Numberformat nf = Numberformat.getPercentInstance();
nf.setMinimumFractionDigits(2);
Sytem.out.println(nf.format(z));

結果是:0.00%

一看就知道是有問題的 orz,改成底下這樣子


double x = 20;
double y = 100;
double z = x / y;
Numberformat nf = Numberformat.getPercentInstance();
nf.setMinimumFractionDigits(2);
Sytem.out.println(nf.format(z));

結果是:20.00%


這....我該說是我沒注意,還是compiler或run time是要寫明....

2012年11月20日 星期二

Spring、J2EE學習筆記-中文在input傳送的亂碼問題

在J2EE中,傳送input資料時,預設是使用ISO-8895-1編碼,英文傳送是沒問題,但中文傳送就會變成亂碼,解決方式就是在tomcat的server.xml中,收

<Connector SSLEnabled="true" clientAuth="false" keystoreFile="/XXX/.key" keystorePass="AAAA" maxThreads="150" port="8443" protocol="HTTP/1.1" scheme="https" secure="true" sslProtocol="TLS" />

改成

<Connector SSLEnabled="true" clientAuth="false" keystoreFile="/XXX/.key" keystorePass="AAAA" maxThreads="150" port="8443" protocol="HTTP/1.1" scheme="https" secure="true" sslProtocol="TLS" URIEncoding="UTF-8" />

不過,在我的狀況,是用8443,若我使用跟參考網址一樣的8080的那個部分,中文一樣會亂碼,猜測這部分也是要看你實際使用的port

參考網址:
http://wenku.baidu.com/view/4148a2e8f8c75fbfc77db25e.html

2012年11月13日 星期二

Spring、J2EE學習筆記-HttpOnly設定

有個案子安全檢測來了個底下這個低風險的問題


[1/2] 在階段作業 Cookie 中遺漏 HttpOnly 屬性 


說實在,第一眼看到時,完全不知道是什麼,總之,就是要我處理掉它(內心os:處理掉不知道會有什麼問題)。

查了下資料,Tomcat 7及spring 3後,都可以在web.xml中的session-config去設定,例如:

<session-config>
        <session-timeout>10</session-timeout>
        <cookie-config>
            <http-only>true</http-only>
            <secure>true</secure>
        </cookie-config>
</session-config>

只是,我用的好像是很舊的,但還是有法子,就是在server.xml中的Context加入

<Context docBase="{你的專案路徑}" path="{你的專案路徑}" reloadable="true" useHttpOnly="true"/>

這樣就ok了,查了下是說防止xss的方法之一,只能說年頭要防的東西太多了

參考網址:
Java EE 6.0 的 Cookie 类已经有设置 HttpOnly 的方法
Spring安全控制
How do you configure HttpOnly cookies in tomcat / java webapps?
HttpOnly介绍以及防止XSS攻击时的作用(转)
How do I set httpOnly and secure cookies with Spring Security?