0

我想用 AJAX 设置一个会话变量并在 jsp 页面中访问它。

这是 AJAX 请求的 serveResource 方法:

@Override
public void serveResource(ResourceRequest resourceRequest,
        ResourceResponse resourceResponse) throws IOException,
        PortletException {

    /**
     * Read form data sent by AJAX
     */


    String emailAddress = ParamUtil.getString(resourceRequest, "emailAddress");

    System.out.println("Email: " + emailAddress);
    resourceResponse.setContentType("text/html");
    PrintWriter writer = resourceResponse.getWriter();
    JSONObject jsonObject =  JSONFactoryUtil.createJSONObject();

    jsonObject.put("fullName", emailAddress);


    resourceRequest.getPortletSession().setAttribute("myAttribute", emailAddress, PortletSession.PORTLET_SCOPE);


    //System.out.println("ATRIBUTSSE: " + resourceRequest.getPortletSession().getAttribute("myAttribute", PortletSession.PORTLET_SCOPE));

    writer.print(jsonObject.toString());
    writer.flush();
    writer.close();
    getPortletContext().getRequestDispatcher("/view.jsp").include(resourceRequest, resourceResponse);
    super.serveResource(resourceRequest, resourceResponse);

}

会话属性myAttribute必须可以访问 view.jsp 页面。AJAX 请求和会话属性myAttribute的值在同一个页面(view.jsp)上,所以只有一个页面。当我尝试像这样访问属性的值时:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="theme" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<theme:defineObjects/>
<portlet:defineObjects />
<portlet:resourceURL var="resourceURL"/>
<script type="text/javascript">
//$('#_rrr_WAR_rrrportlet_ttext').keyup(function(){
//callServeResource('trei');
//});
function callServeResource(a){
AUI().use('aui-io-request', function(A){
    A.io.request('<%=resourceURL.toString()%>', {
           method: 'post',
           dataType: 'json',
           form: {
               id: 'fm'
           },
           on: {
                success: function() {
                    if(a === 'trei'){
                        document.getElementById('_rrr_WAR_rrrportlet_ttextaj').value = this.get('responseData').fullName;
                    } if(a === 'unu'){
                        document.getElementById('_rrr_WAR_rrrportlet_ttextaj').value = this.get('responseData').fullName;
                    }

                }
           }
        });
});
}
</script>
<form name="fm" id="fm">
Full Name:<aui:input type="text" id="ttext" name="emailAddress"/>
Full Name:<aui:input type="text" id="ttextaj" name="emailAddress"/>
Full Name:<aui:input type="text" id="ttextajx" name="emailAddressx"/>
<%=request.getAttribute("myAttribute") %>
<br/>
<input type="button" value="Submit Form Data"        onclick="callServeResource('unu')">
</form>

我总是得到null

4

0 回答 0