Struts2 jquery Plugin responds to ajax requests with HTML of the entire page

女生的网名这么多〃 提交于 2020-01-22 15:47:24

问题


I am trying to use the Struts2 jquery plugin for ajax requests on some of my forms, but I am having an issue with the response to the page. The struts action is validated and executed properly, but when jquery gets a response, it sets the response as the HTML of the entire page... It is putting the response in the correct place, but it is not responding with the right thing at all... Here is the jsp form:

<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<div class="columnbox">
    <h2>Contact Us</h2>
    <div id="contact">
        <s:form id="contact_form" action="contact" method="post" cssClass="clearfix">

            <label for="contact_user">Username / In-game name:</label>
            <input type="text" id="contact_user" name="contactBean.username" class="field"
                        data-enter-click="sendbutton" maxlength="16" size="16" />

            <div id="contact_response" class="response">

            </div>

            <sj:submit 
                    formIds="contact_form"
                    id="sendbutton"
                    targets="contact_response" 
                    value="Send" 
                    button="true"
                    />

        </s:form>
    </div>
</div>

And here is a snipit of the contactAction class:


public class ContactAction extends ActionSupport {

    private static final long serialVersionUID = -5484167219440627408L;

    private static final Log log = LogFactory.getLog(ContactAction.class);

    private ContactBean contactBean;


    @Override
    public String execute() throws Exception {
        log.info("TEST 4");
        //Do email stuff

        addActionMessage(Constants.EMAIL_SENT);

        log.info(this.getActionMessages());

        return Action.SUCCESS;
    }

    @Override
    public void validate() {
        System.out.println("TEST");
        log.info("TEST 2");
                //do validation
        if (contactBean == null) {
            addActionError("");
        }
        else if (contactBean.getUsername() == null || contactBean.getUsername().isEmpty()) {
            addActionError(Constants.NO_USERNAME);
        }
        log.info(this.getActionErrors());
    }

    public ContactBean getContactBean() {
        return contactBean;
    }

    public void setContactBean(ContactBean contactBean) {
        this.contactBean = contactBean;
    }

}

I am pretty sure that the tag is exactly how it should be. The request is being sent properly, and the response is being recieved. This looks like a problem with the jquery ... Thanks for your help


回答1:


The response is exactly the thing that returned by the result after its execution. Whatever result is configured to return the result as a response, whichever result type and result code is actually returned it's all depends on your action configuration.

The problem you have that returns a whole page as a result is because the INPUT result is returned when the validation fails. You have to either remove the validation interceptor from the stack or if it's a dispatcher result type, update the location attribute of the result config before the result is executed.



来源:https://stackoverflow.com/questions/18939057/struts2-jquery-plugin-responds-to-ajax-requests-with-html-of-the-entire-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!