這些天一直在自己開發(fā)一個(gè)項(xiàng)目審計(jì)系統(tǒng),自開發(fā)項(xiàng)目真是心酸多多啊,從早上7點(diǎn)起床,到晚上11點(diǎn)睡覺.幾乎沒怎么離開電腦,處理掉了N多問題我的企業(yè)管理模塊終于出爐了.回想一下困擾我時(shí)間最長的就是Struts的中文處理問題,和用<hmlt:link>標(biāo)簽傳遞頁面上用<bean:write>輸出的Bean的屬性的問題.用純Struts實(shí)現(xiàn)真是很難.這是我第一次開發(fā)項(xiàng)目,所以要把其中的酸甜苦辣都寫出來.呵呵先說說中文問題把.我解決的時(shí)候可是遍布網(wǎng)絡(luò),通過各種方式尋找解決方案,終于工夫不負(fù)有心人我看到了一個(gè)帖子給我了極大的激發(fā).這樣解決不但可是讓Action直接接收的Form的數(shù)據(jù)就是gb2312,而且還可以讓數(shù)據(jù)庫也能很好的解決中文問題,真是一石二鳥啊哈哈!
首先在web.xml上配置如下:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.huahang.tj.struts.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
然后寫一個(gè)JAVA類,是一個(gè)過濾器,讓然如果自己不愛寫直接把下面的源代碼copy上也可以.
package com.huahang.tj.struts.filters;
import javax.servlet.*;
import java.io.IOException;
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
配置完了.在加上了這個(gè)過濾器類就在action中就可以直接從form中接收gb2312編碼的數(shù)據(jù)了,返回時(shí)自然也是gb2312了。但是這個(gè)好像需要servlet 2.2以上的容器
我的環(huán)境是
windowsXp-MyEclipse4.0.3-Tomcat5.5-JDK1.5-Struts1.2
還有一個(gè)問題就是
<%@page contentType="text/html;charset=gb2312"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html:html>
<body>
您的帳戶是:<bean:write name="user" property="username"/>
<br><br><br>
<h1>歡迎使用項(xiàng)目申報(bào)管理系統(tǒng)</h1>
<br><br>
<html:link page="/select.do" paramId="username" paramName="user" paramProperty="username">企業(yè)信息</html:link>
</body>
</html:html>
向上面那樣的一個(gè)簡(jiǎn)單的JSP頁面,用Struts的標(biāo)簽,<bean:write>輸出了我在別的Action里存入Session的一個(gè)Bean的屬性,這個(gè)屬性用來作會(huì)話用的.然后我想把這個(gè)屬性當(dāng)作參數(shù)加入到一個(gè)<html:link>中.方法找了N多都沒能很少的實(shí)現(xiàn),因?yàn)轫?xiàng)目要求是用純Struts寫,所以我沒用JSP代碼.最后一個(gè)朋友幫我找到了這個(gè)方法:
<html:link page="/select.do" paramId="username" paramName="user" paramProperty="username">
雖然看上去不怎么難,但是我以前看過的幾本Struts書和上學(xué)的時(shí)候?qū)W的Struts都沒涉及到這樣傳遞參數(shù)的方法.這樣在請(qǐng)求后面用paramId聲明一個(gè)參數(shù),要傳遞的參數(shù).然后用paramName找到一個(gè)具體的Bean,然后是具體的屬性用paramName實(shí)現(xiàn).哈哈問題就解決了.因?yàn)槭堑谝淮螌戫?xiàng)目所以我感覺自己處理問題的方法還是不夠簡(jiǎn)單明了.通過日夜奮戰(zhàn)我的項(xiàng)目的企業(yè)模塊的編碼工作也都搞定了.初步測(cè)試通過.哈哈
posted on 2005-11-29 15:47
我心依舊 閱讀(1897)
評(píng)論(5) 編輯 收藏