You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Pham Anh Tuan <an...@ichi-corp.jp> on 2006/09/26 09:09:38 UTC

[HELP] Can't not submit unicode form by ajax action in Struts + Velocity framework

Hi all,

I got a trouble when I submit the unicode form by ajax.

when I test the submitted url, it gives me the right unicode value for each 
parameter I sent. ex: xxxx.do?st1=tốivề&st2=ngàyvề

but when I retrieve it from bean, it was not unicode value :(

this trouble happens only when I submit one form by ajax action, when I do 
it with normal struts action, it's okie, I mean I get all unicode values.

I think that there's something wrong when the Struts servlet receive the url 
(GET methods)

help me, thanks in advance

bowlkhin,

PS:
*****************************************************
++++ in web.xml, I use SetCharacterEncodingFilter class to convert string to 
utf8

 <!-- Filter to set character encoding on each request -->
 <filter>
  <filter-name>Set Character Encoding</filter-name>
  <filter-class>filters.SetCharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>

 <filter-mapping>
  <filter-name>Set Character Encoding</filter-name>
  <url-pattern>*.do</url-pattern>
 </filter-mapping>

 <filter-mapping>
  <filter-name>Set Character Encoding</filter-name>
  <servlet-name>*.vm</servlet-name>
 </filter-mapping>

*****************************************************
in velocity.properties:
input.encoding=UTF-8
output.encoding=UTF-8
default.contentType=text/html; charset=UTF-8

*****************************************************
in vm file:
.........

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

</header>

......

          <form name="searchQuickFormBean" 
action="javascript:retrieveURL('$link.setAction('/t/search')', 
'searchQuickFormBean','wpm');" >
           <table border="0" cellpadding="0" cellspacing="0" width="100%" 
id="table3">
            <tr>
             <td width="20"></td>
             <td width="80">
              <select size="1" name="searchConstraint.searchType">
               <option selected 
value="0">[$text.get("guest.index.top_banner.search.select")]</option>
               <option 
value="1">$text.get("guest.index.top_banner.search.company")</option>
               <option 
value="2">$text.get("guest.index.top_banner.search.product")</option>
               <option 
value="3">$text.get("guest.index.top_banner.search.member")</option>
               <option 
value="4">$text.get("guest.index.top_banner.search.news")</option>
              </select>
             </td>
             <td width="10"></td>
             <td width="99">
              <input type="text" name="searchConstraint.keyWord" size="17" 
maxlength="50" style="font-size: 11px; font-weight: 500; float:left">
             </td>
             </tr>
           </table>
           ##end simple search
          </form>

*****************************************************
below is my ajax.js

  function retrieveURL(url,nameOfFormToPost, spanTagName) {
    // set name of span tag
    curSpanTagName = spanTagName;

    //get the (form based) params to push up as part of the get request
    url=url+"?noPara=yes"+getFormAsString(nameOfFormToPost);

    //Do the Ajax call
    if (window.XMLHttpRequest) { // Non-IE browsers
      req = new XMLHttpRequest();
      req.onreadystatechange = processStateChange;
      try {
       req.open("GET", url, true); //was get
      } catch (e) {
        alert("Problem Communicating with Server\n"+e);
      }
      req.send(null);
    } else if (window.ActiveXObject) { // IE

      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req) {
        req.onreadystatechange = processStateChange;
        req.open("GET", url, true);
        req.send();
      }
    }
  }

 function getFormAsString(formName){

  //Setup the return String
  returnString ="";

   //Get the form values
  formElements=document.forms[formName].elements;

  //loop through the array , building up the url
  //in the form /strutsaction.do&name=value

  for ( var i=formElements.length-1; i>=0; --i ){
   //we escape (encode) each value
   returnString=returnString+"&"+formElements[i].name+"="+formElements[i].value;
   }

  //return the values
  return returnString;
 } 


Re: [HELP] Can't not submit unicode form by ajax action in Struts + Velocity framework

Posted by Martin Gainty <mg...@hotmail.com>.
just out of curiosity have you implemented your Ajax Control with RPC or doc-literal or some other type of transport?
Thanks,
M-
*********************************************************************
This email message and any files transmitted with it contain confidential
information intended only for the person(s) to whom this email message is
addressed.  If you have received this email message in error, please notify
the sender immediately by telephone or email and destroy the original
message without making a copy.  Thank you.



----- Original Message ----- 
From: "Pham Anh Tuan" <an...@ichi-corp.jp>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Tuesday, September 26, 2006 5:47 AM
Subject: Re: [HELP] Can't not submit unicode form by ajax action in Struts + Velocity framework


> ah haaaaaaaaaaaaa, yeahhhhhh, thanks a lot Antonio, I solved my problem! 
> again, thank u (^-^)
> 
> bowlkhin
> ----- Original Message ----- 
> From: "Antonio Petrelli" <ap...@apache.org>
> To: "Struts Users Mailing List" <us...@struts.apache.org>
> Sent: Tuesday, September 26, 2006 4:41 PM
> Subject: Re: [HELP] Can't not submit unicode form by ajax action in Struts + 
> Velocity framework
> 
> 
>> Pham Anh Tuan ha scritto:
>>> Antonio, would you mind showing an example how to use POST method with 
>>> AJAX ?
>>
>> Your question is too generic. Maybe you need simply JavaScript, in this 
>> case I suggest to read this piece of tutorial
>>
>> http://www.w3schools.com/htmldom/dom_obj_form.asp
>>
>> HTH
>> Antonio
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
>

Re: [HELP] Can't not submit unicode form by ajax action in Struts + Velocity framework

Posted by Pham Anh Tuan <an...@ichi-corp.jp>.
ah haaaaaaaaaaaaa, yeahhhhhh, thanks a lot Antonio, I solved my problem! 
again, thank u (^-^)

bowlkhin
----- Original Message ----- 
From: "Antonio Petrelli" <ap...@apache.org>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Tuesday, September 26, 2006 4:41 PM
Subject: Re: [HELP] Can't not submit unicode form by ajax action in Struts + 
Velocity framework


> Pham Anh Tuan ha scritto:
>> Antonio, would you mind showing an example how to use POST method with 
>> AJAX ?
>
> Your question is too generic. Maybe you need simply JavaScript, in this 
> case I suggest to read this piece of tutorial
>
> http://www.w3schools.com/htmldom/dom_obj_form.asp
>
> HTH
> Antonio
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [HELP] Can't not submit unicode form by ajax action in Struts + Velocity framework

Posted by Antonio Petrelli <ap...@apache.org>.
Pham Anh Tuan ha scritto:
> Antonio, would you mind showing an example how to use POST method with 
> AJAX ?

Your question is too generic. Maybe you need simply JavaScript, in this 
case I suggest to read this piece of tutorial

http://www.w3schools.com/htmldom/dom_obj_form.asp

HTH
Antonio


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [HELP] Can't not submit unicode form by ajax action in Struts + Velocity framework

Posted by Pham Anh Tuan <an...@ichi-corp.jp>.
Antonio, would you mind showing an example how to use POST method with AJAX 
?

thanks in advace

bowlkhin
----- Original Message ----- 
From: "Pham Anh Tuan" <an...@ichi-corp.jp>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Tuesday, September 26, 2006 4:16 PM
Subject: Re: [HELP] Can't not submit unicode form by ajax action in Struts + 
Velocity framework


> oh ... :O ...
>
> I just forgot Post method :( ... thank you for reminding me ... I will try 
> it.
>
> Again, thanks for ur feedback :)
>
> ----- Original Message ----- 
> From: "Antonio Petrelli" <ap...@apache.org>
> To: "Struts Users Mailing List" <us...@struts.apache.org>
> Sent: Tuesday, September 26, 2006 4:13 PM
> Subject: Re: [HELP] Can't not submit unicode form by ajax action in Struts 
> + Velocity framework
>
>
>> Pham Anh Tuan ha scritto:
>>> when I test the submitted url, it gives me the right unicode value for 
>>> each
>>> parameter I sent. ex: xxxx.do?st1=tốivề&st2=ngàyvề
>>>
>>> but when I retrieve it from bean, it was not unicode value :(
>>>
>>> st1: "tốivề" ---> "tối về"
>>> st2: "ngàyvề" ---> "ngà y về"
>>>
>>> I don't know what's wrong?
>>
>> If you want to send special characters in the URL you have to encode 
>> them.
>> Anyway I know how to do it in Java, using URLEncoder.encode(myString, 
>> "UTF-8"). I don't know if there is such a feature in JavaScript...
>> Just a question, why don't you "post" those parameters?
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [HELP] Can't not submit unicode form by ajax action in Struts + Velocity framework

Posted by Pham Anh Tuan <an...@ichi-corp.jp>.
oh ... :O ...

I just forgot Post method :( ... thank you for reminding me ... I will try 
it.

Again, thanks for ur feedback :)

----- Original Message ----- 
From: "Antonio Petrelli" <ap...@apache.org>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Tuesday, September 26, 2006 4:13 PM
Subject: Re: [HELP] Can't not submit unicode form by ajax action in Struts + 
Velocity framework


> Pham Anh Tuan ha scritto:
>> when I test the submitted url, it gives me the right unicode value for 
>> each
>> parameter I sent. ex: xxxx.do?st1=tốivề&st2=ngàyvề
>>
>> but when I retrieve it from bean, it was not unicode value :(
>>
>> st1: "tốivề" ---> "tối về"
>> st2: "ngàyvề" ---> "ngà y về"
>>
>> I don't know what's wrong?
>
> If you want to send special characters in the URL you have to encode them.
> Anyway I know how to do it in Java, using URLEncoder.encode(myString, 
> "UTF-8"). I don't know if there is such a feature in JavaScript...
> Just a question, why don't you "post" those parameters?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [HELP] Can't not submit unicode form by ajax action in Struts + Velocity framework

Posted by Antonio Petrelli <ap...@apache.org>.
Pham Anh Tuan ha scritto:
> when I test the submitted url, it gives me the right unicode value for 
> each
> parameter I sent. ex: xxxx.do?st1=tốivề&st2=ngàyvề
>
> but when I retrieve it from bean, it was not unicode value :(
>
> st1: "tốivề" ---> "tối về"
> st2: "ngàyvề" ---> "ngà y về"
>
> I don't know what's wrong?

If you want to send special characters in the URL you have to encode them.
Anyway I know how to do it in Java, using URLEncoder.encode(myString, 
"UTF-8"). I don't know if there is such a feature in JavaScript...
Just a question, why don't you "post" those parameters?

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [HELP] Can't not submit unicode form by ajax action in Struts + Velocity framework

Posted by Pham Anh Tuan <an...@ichi-corp.jp>.
when I test the submitted url, it gives me the right unicode value for each
parameter I sent. ex: xxxx.do?st1=tốivề&st2=ngàyvề

but when I retrieve it from bean, it was not unicode value :(

st1: "tốivề" ---> "tối về"
st2: "ngàyvề" ---> "ngà y về"

I don't know what's wrong?

----- Original Message ----- 
From: "Pham Anh Tuan" <an...@ichi-corp.jp>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Tuesday, September 26, 2006 2:09 PM
Subject: [HELP] Can't not submit unicode form by ajax action in Struts + 
Velocity framework


> Hi all,
>
> I got a trouble when I submit the unicode form by ajax.
>
> when I test the submitted url, it gives me the right unicode value for 
> each
> parameter I sent. ex: xxxx.do?st1=tốivề&st2=ngàyvề
>
> but when I retrieve it from bean, it was not unicode value :(
>
> this trouble happens only when I submit one form by ajax action, when I do
> it with normal struts action, it's okie, I mean I get all unicode values.
>
> I think that there's something wrong when the Struts servlet receive the 
> url
> (GET methods)
>
> help me, thanks in advance
>
> bowlkhin,
>
> PS:
> *****************************************************
> ++++ in web.xml, I use SetCharacterEncodingFilter class to convert string 
> to
> utf8
>
> <!-- Filter to set character encoding on each request -->
> <filter>
>  <filter-name>Set Character Encoding</filter-name>
>  <filter-class>filters.SetCharacterEncodingFilter</filter-class>
>  <init-param>
>   <param-name>encoding</param-name>
>   <param-value>UTF-8</param-value>
>  </init-param>
> </filter>
>
> <filter-mapping>
>  <filter-name>Set Character Encoding</filter-name>
>  <url-pattern>*.do</url-pattern>
> </filter-mapping>
>
> <filter-mapping>
>  <filter-name>Set Character Encoding</filter-name>
>  <servlet-name>*.vm</servlet-name>
> </filter-mapping>
>
> *****************************************************
> in velocity.properties:
> input.encoding=UTF-8
> output.encoding=UTF-8
> default.contentType=text/html; charset=UTF-8
>
> *****************************************************
> in vm file:
> .........
>
> <head>
>
> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
>
> </header>
>
> ......
>
>          <form name="searchQuickFormBean"
> action="javascript:retrieveURL('$link.setAction('/t/search')',
> 'searchQuickFormBean','wpm');" >
>           <table border="0" cellpadding="0" cellspacing="0" width="100%"
> id="table3">
>            <tr>
>             <td width="20"></td>
>             <td width="80">
>              <select size="1" name="searchConstraint.searchType">
>               <option selected
> value="0">[$text.get("guest.index.top_banner.search.select")]</option>
>               <option
> value="1">$text.get("guest.index.top_banner.search.company")</option>
>               <option
> value="2">$text.get("guest.index.top_banner.search.product")</option>
>               <option
> value="3">$text.get("guest.index.top_banner.search.member")</option>
>               <option
> value="4">$text.get("guest.index.top_banner.search.news")</option>
>              </select>
>             </td>
>             <td width="10"></td>
>             <td width="99">
>              <input type="text" name="searchConstraint.keyWord" size="17"
> maxlength="50" style="font-size: 11px; font-weight: 500; float:left">
>             </td>
>             </tr>
>           </table>
>           ##end simple search
>          </form>
>
> *****************************************************
> below is my ajax.js
>
>  function retrieveURL(url,nameOfFormToPost, spanTagName) {
>    // set name of span tag
>    curSpanTagName = spanTagName;
>
>    //get the (form based) params to push up as part of the get request
>    url=url+"?noPara=yes"+getFormAsString(nameOfFormToPost);
>
>    //Do the Ajax call
>    if (window.XMLHttpRequest) { // Non-IE browsers
>      req = new XMLHttpRequest();
>      req.onreadystatechange = processStateChange;
>      try {
>       req.open("GET", url, true); //was get
>      } catch (e) {
>        alert("Problem Communicating with Server\n"+e);
>      }
>      req.send(null);
>    } else if (window.ActiveXObject) { // IE
>
>      req = new ActiveXObject("Microsoft.XMLHTTP");
>      if (req) {
>        req.onreadystatechange = processStateChange;
>        req.open("GET", url, true);
>        req.send();
>      }
>    }
>  }
>
> function getFormAsString(formName){
>
>  //Setup the return String
>  returnString ="";
>
>   //Get the form values
>  formElements=document.forms[formName].elements;
>
>  //loop through the array , building up the url
>  //in the form /strutsaction.do&name=value
>
>  for ( var i=formElements.length-1; i>=0; --i ){
>   //we escape (encode) each value
> 
> returnString=returnString+"&"+formElements[i].name+"="+formElements[i].value;
>   }
>
>  //return the values
>  return returnString;
> }
>
>


--------------------------------------------------------------------------------


> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org