posts - 70,comments - 408,trackbacks - 0

              這些天一直在自己開發一個項目審計系統,自開發項目真是心酸多多啊,從早上7點起床,到晚上11點睡覺.幾乎沒怎么離開電腦,處理掉了N多問題我的企業管理模塊終于出爐了.回想一下困擾我時間最長的就是Struts的中文處理問題,和用<hmlt:link>標簽傳遞頁面上用<bean:write>輸出的Bean的屬性的問題.用純Struts實現真是很難.這是我第一次開發項目,所以要把其中的酸甜苦辣都寫出來.呵呵先說說中文問題把.我解決的時候可是遍布網絡,通過各種方式尋找解決方案,終于工夫不負有心人我看到了一個帖子給我了極大的激發.這樣解決不但可是讓Action直接接收的Form的數據就是gb2312,而且還可以讓數據庫也能很好的解決中文問題,真是一石二鳥啊哈哈!

          首先在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>

           

          然后寫一個JAVA類,是一個過濾器,讓然如果自己不愛寫直接把下面的源代碼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);

          }

          }

           

          配置完了.在加上了這個過濾器類就在action中就可以直接從form中接收gb2312編碼的數據了,返回時自然也是gb2312了。但是這個好像需要servlet 2.2以上的容器

          我的環境是

          windowsXp-MyEclipse4.0.3-Tomcat5.5-JDK1.5-Struts1.2

           

          還有一個問題就是

          <%@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>歡迎使用項目申報管理系統</h1>

             <br><br>

             <html:link page="/select.do" paramId="username" paramName="user" paramProperty="username">企業信息</html:link>

            </body>

          </html:html>

           

              向上面那樣的一個簡單的JSP頁面,用Struts的標簽,<bean:write>輸出了我在別的Action里存入Session的一個Bean的屬性,這個屬性用來作會話用的.然后我想把這個屬性當作參數加入到一個<html:link>中.方法找了N多都沒能很少的實現,因為項目要求是用純Struts寫,所以我沒用JSP代碼.最后一個朋友幫我找到了這個方法:

          <html:link page="/select.do" paramId="username" paramName="user" paramProperty="username">

              雖然看上去不怎么難,但是我以前看過的幾本Struts書和上學的時候學的Struts都沒涉及到這樣傳遞參數的方法.這樣在請求后面用paramId聲明一個參數,要傳遞的參數.然后用paramName找到一個具體的Bean,然后是具體的屬性用paramName實現.哈哈問題就解決了.因為是第一次寫項目所以我感覺自己處理問題的方法還是不夠簡單明了.通過日夜奮戰我的項目的企業模塊的編碼工作也都搞定了.初步測試通過.哈哈
          posted on 2005-11-29 15:47 我心依舊 閱讀(1903) 評論(5)  編輯  收藏

          FeedBack:
          # re: Struts中文問題&&link標簽參數問題(第一次項目紀念)
          2006-11-21 11:42 | mark
          html:link page="/select.do" paramId="username" paramName="user" paramProperty="username">

          你好,我也是用這種方式傳遞參數, 然后通過一個actoin處理這個username ,再轉向另一個頁面, 但,這樣做后,我在action中讀取username時,輸出為null,請問你是怎么解決的呢?

          request.getAttribute("username"); 返回為空,不知何故??

            回復  更多評論
            
          # re: Struts中文問題&&link標簽參數問題(第一次項目紀念)
          2006-12-01 15:55 | zzyy163163
          樓上,不要用
          request.getAttribute("username");
          用:request.getParameter("username");   回復  更多評論
            
          # re: Struts中文問題&&link標簽參數問題(第一次項目紀念)
          2006-12-22 20:46 | 聰聰
          多謝你的經驗,真實受益匪淺呀!  回復  更多評論
            
          # re: Struts中文問題&&link標簽參數問題(第一次項目紀念)
          2006-12-22 20:55 | 聰聰
          再請問高手一下,在action中如何得到session,我的代碼是
          HttpSession session =request.getSession(true);
          String username=(String)session.getAttribute(Constant.USER_KEY);
          String uu="";
          if(username.equals(uu)) return mapping.findForward("signtip");
          else return mapping.findForward("bid");

          運行后說第二句有問題 ,怎么回事啊?  回復  更多評論
            
          # re: Struts中文問題&&link標簽參數問題(第一次項目紀念)
          2008-01-31 15:32 | NOFUYUN
          我照你的方法建了一個過濾器,配置也是和你的相同,為什么傳遞過去的還亂碼啊?

          a.jsp
          %@page contentType="text/html;charset=gb2312"%>
          <html:link page="/form/gq_play.jsp" paramId="aa" paramName="list" paramProperty="gq_adress" target="_blank">
          試聽</html:link>
          b.jsp
          %@page contentType="text/html;charset=gb2312"%>
          <%=request.getParameter("aa") %>
          不知道怎么辦了。  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 乌拉特后旗| 和田市| 盱眙县| 新干县| 松原市| 兰州市| 连南| 泗阳县| 白银市| 黑河市| 含山县| 上栗县| 井冈山市| 陆川县| 增城市| 修文县| 开阳县| 巩义市| 大余县| 寿阳县| 白银市| 沈丘县| 涟水县| 贵德县| 八宿县| 罗田县| 平乡县| 永昌县| 登封市| 武定县| 龙口市| 遵义县| 通河县| 安康市| 晋州市| 曲沃县| 阳信县| 周宁县| 蒙自县| 墨竹工卡县| 扎鲁特旗|