环境:struts2_spring_hiber + jquery
用:
        $.post("http://localhost/test/test?username="
         +encodeURI(encodeURI(username)),function(data){
         alert("Data Loaded: " + data);
        }); 能把username传到Action中,能在后台打印显示,但用:
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(username);死活不在页面上有alert怀疑我的环境搭建得有问题,之前使用纯action向jsp传值,也是无论如何得不到值。
求神人赐教!!(考虑从头开始另搭建环境)

解决方案 »

  1.   

    纯action向jsp传值,也是无论如何得不到值。
    ???
    贴代码出来撒,你后台都拿到值了,前台拿不到,说明代码有问题然后你的ajax取值,你的代码呢,也不贴,调用的方法有没错????
      

  2.   


    package test.s2sh.action;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException; 
    import java.net.URLDecoder; 
    import com.opensymphony.xwork2.ActionSupport; 
    public class TestAction extends BaseAction 

      private String username; 
      public String getUsername() { 
      return username; 

    public void setUsername(String username) { 
      this.username = username; 

    public String test() throws IOException 

           try { 
            String username2 = URLDecoder.decode(username, "UTF-8"); 
            System.out.println(username2); 
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write("dddddd"+username);
           } 
          catch (UnsupportedEncodingException e) { 
                 // TODO Auto-generated catch block 
                 e.printStackTrace(); 
            } 
           return null; 或return SUCCESS;都一样 } 

    package test.s2sh.action;import java.util.Map;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;public class BaseAction extends ActionSupport {   
        private static final long serialVersionUID = 7620009925942346125L;   
        ActionContext context = ActionContext.getContext();   
        HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);   
        //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);   
        HttpServletResponse response = ServletActionContext.getResponse();
        Map session = context.getSession();   
    }  struts.xml   <package name="test" extends="struts-default"> 
            <action name="test" class="testAction" method="test"> 
                <result name="success"> 
                    /Save_success.jsp  #不跳转,这个其实没用的
                </result> 
            </action> 
        </package> 
    index.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <% 
    String path = request.getContextPath(); 
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html> 
      <head> 
        <base href="<%=basePath%>"> 
        <title>Test</title> 
    <meta http-equiv="pragma" content="no-cache"> 
    <meta http-equiv="cache-control" content="no-cache"> 
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
    <meta http-equiv="description" content="This is my page"> 
    <script type="text/javascript" src="js/jquery.js"></script> 
    <!--<script type="text/javascript" src="js/test.js"></script> -->
    <script>
    $(document).ready(function(){ 
       $("#userBtt").click(function(){ 
          var username = $("#username").val(); 
          if(username=="") 
          { 
             alert("不能为空"); 
          } 
          else 
          { 
            $.post("http://localhost/test/test?username="
             +encodeURI(encodeURI(username)),function(data){
             alert("Data Loaded: " + data);
            }); 
          } 
       }); 
    }
    ); </script>  </head> 
      
      <body> 
        <table> 
            <tr> 
               <td>请输入用户名:<input type="text" name="username" id="username"></td> 
               <td><input type="button"  value="校验" id="userBtt"></td> 
               <td></td> 
            </tr> 
        </table> 
      </body> 
    </html> 
      

  3.   


    request.setCharacterEncoding("utf-8");
    response.setContentType("text/html; charset=utf-8");
    response.getWriter().write(username);LZ,试试!
      

  4.   

    没看出你页面上哪里直接与action取值了
      

  5.   

    推荐LZ用 STRUTS2的 JSON返回类型 很方便
      

  6.   

    jsp取值我之前试过了一天,都是null。想用一下struts2咋这么难呢to 4楼哥们,我照做了,一样不行(看来是得完全新搭环境了,这五年的程序员白当了)
      

  7.   

    服务器端不用改,客户端改为:
    $.post("http://localhost/test/test",{'username':username},function(data){
        alert(......);
    });即可。
      

  8.   

    $.post("http://localhost/test/test?username="
                        +encodeURI(encodeURI(username)),function(data){
                            alert("Data Loaded: " + data);
                }); 
    试试这样看:var url ="http://localhost/test/test?username="
                        +encodeURI(encodeURI(username);
    $.ajax( {
    type : "post",
    url : url,
    dataType : "json",
    success :function(data){
                            alert("Data Loaded: " + data);
    ,
    async : false
    });
    然后:
     public String test() throws IOException 
        { 
             String username2 = URLDecoder.decode(username, "UTF-8"); 
            System.out.println(username2); 
           return SUCCESS;    } 
      

  9.   

    struts.xml文件中和Action的定义不符。
      

  10.   

    testAction是定义在spring里的bean,估计没关系
      

  11.   

    用Firefox测和用IE测,反应一个样,啥东西都不弹出来改成这样也不行,但可以在Action中打印到后台:
    <script>
    $(document).ready(function(){ 
       $("#userBtt").click(function(){ 
          var username = $("#username").val(); 
          var url="http://localhost/test/test?username="+username;
          $.ajax( {
           type : "post",
           url : url,
           success :function(data){
             alert("Data Loaded: " + data);
             },
           async : false
           });
       }); 
    }
    ); 
    </script>
    服了
      

  12.   

    虽然你已经结贴,好奇问一句,啥东西都弹不出来,包括
             if(username=="") 
              { 
                 alert("不能为空"); 
              } 
    这个么?