【问题标题】:How to get data from action class to jsp using ajax in struts2?如何在struts2中使用ajax从动作类获取数据到jsp?
【发布时间】:2017-02-20 11:34:48
【问题描述】:

我是 ajax 新手,想从动作类获取数据到 jsp。

文件:getRole.jsp

<head>
<script type="text/javascript">
var request;
function loaddata()
{
    var id = document.getElementById("newRole").value;
    alert("hi : "+ id);
    var xhttp;
      xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("display_info").innerHTML = this.responseText;
          }
      };
      xhttp.open("GET", "loadData?id="+id, true);
      xhttp.send();
}

</script>
</head>
<body>
<div class="container-fluid">
    <div class="col-sm-2"></div>
    <div class="col-sm-8">
        <label for="focusedInput" id="abc"><h4>Enter Id : </h4></label>
        <input type="text" class="form-control" name="newRole" id="newRole" onkeyup="loaddata();">
        <div id="display_info" ></div>
    </div>
    <div class="col-sm-2"></div>
 </div>
</body>

文件:loadData.java

public class loadData extends ActionSupport implements ServletRequestAware, ServletResponseAware{

private static final long serialVersionUID = 1L;
String url = "jdbc:mysql://localhost:3306/struts";
String user = "username";
String pass = "password";
HttpServletRequest request;
HttpServletResponse response;

@Override
public void setServletRequest(HttpServletRequest request) {
    // TODO Auto-generated method stub
    this.request = request;
}

public HttpServletRequest getServletRequest() {
    return request;
}

@Override
public void setServletResponse(HttpServletResponse response) {
    // TODO Auto-generated method stub
    this.response = response;
}

public HttpServletResponse getServletResponse()
{
    return response;
}

public String execute()
{
    try{
        String id = request.getParameter("id");
        String name = "null.";
        System.out.println("in servlet : " + id);

        Class.forName("com.mysql.jdbc.Driver");
        java.sql.Connection con =DriverManager.getConnection(url, user, pass);

        String s = "select * from tempemp where id="+id;
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(s);

        while(rs.next())
        {
            name = rs.getString("role");
            System.out.println(name);
        }
        PrintWriter out = response.getWriter();
        out.println("Name: "+name);
        con.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return SUCCESS;
}
}

我想从用户那里获取 ID,然后根据该 ID 打印角色。 我只想使用 ajax 而不是 json。所以请只回答我的ajax代码。

【问题讨论】:

    标签: java ajax jsp struts2


    【解决方案1】:

    您必须在操作中返回 null 而不是成功。 返回null 不会做进一步处理,它只会向客户端发送当前响应。

    返回 SUCCESS 将在 struts.xml 文件中搜索为该结果定义的特定 jsp 页面。

    this.responseText 你会得到你的name。 更多参考https://struts.apache.org/docs/ajax.html

    【讨论】:

    • 我也试试这个代码。 response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(name);
    • 感谢@udaybhaskar。我将“成功”更改为“空”并且它有效。你能解释一下为什么结果类型为“null”吗??
    • 我想发送数组而不是单个字符串。那你知道我是怎么传数组的吗?
    • 使用 json 你可以做到这一点。使用 gson 或 jackson 库。
    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 2013-10-11
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    相关资源
    最近更新 更多