如何在JSP中将XMLHttpResponse从java类转换为JS并解析它

时间:2015-02-06 10:31:44

标签: javascript ajax jsp struts2 tiles

当用户点击超链接时,控件会转到scipt并从那里转到动作文件。

showregistrations.jsp

<s:iterator value="list">
<table
<td><a href="#" class="userName" id='<s:property value="userName"/>'><s:property value="userName"/></a></td>
</table>
</s:iterator>

JavaScript(位于showregistrations.jsp

<script type="text/javascript">
    $('.userName').click(function(){
        var userName= $(this).attr('id');
        var ajax    = new XMLHttpRequest();
        ajax.open("POST","getUserDetails",false);
        ajax.send(userName);
    });
</script>

struts.xml中

<action name="getUserDetails" class="com.ActionClasses.GetUserDetailsAction">
    <result name="success"  type="tiles">showUserDetails</result>
</action>

GetUserDetailsAction.java 正在返回&#34;成功&#34;。

tiles.xml

<definition name="showUserDetails" extends="home">
    <put-attribute name="Title" value="User Details"/>
    <put-attribute name="Menu"  value="/mainMenu.jsp"/>
    <put-attribute name="Body"  value="/showUserDetails.jsp"/>
</definition>

但这不是重定向到showUserDetails.jsp

编辑:

我发现控件来自JS,我称之为动作。有人可以告诉我如何从类GetUserDetailsAction获取响应showregistrations.jsp(从这里我称之为操作文件)。

这是我的GetUserDetailsAction.java

HttpServletRequest req=null;

GetUserDetailsBean getUserDetailsBean = new GetUserDetailsBean();
private String userName;


public String execute(){
    System.out.println("came to getuserdetailsaction");
    try {
        InputStream is = req.getInputStream();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] dataBuffer = new byte[32];
        int dataLength=0;

        while( dataLength >= 0 ) {
            dataLength = is.read(dataBuffer);
            if( dataLength >= 0 ) os.write(dataBuffer, 0, dataLength);
        }

        userName = new String(os.toByteArray(), "UTF-8");

    } catch (IOException e) {
        e.printStackTrace();
    }

    GetUserDetailsDao getUserDetailsDao = new GetUserDetailsDao();
    getUserDetailsDao.getDetails(userName, getUserDetailsBean);
    //getUserDetailsDao.getDetails is filling the details into getUserDetailsBean. How can I send this bean to the JS and print it there.
    System.out.println("returning success from getuserdetails action");
    return SUCCESS;

1 个答案:

答案 0 :(得分:0)

对于问题的第一部分,即,对于重定向到另一个jsp,您可以在使用struts和tiles时使用window.location="actionName.action";。另外,你可以直接写window.location=something.jsp

我不知道如何将java类的响应发送到jsp。