如何在ajax完成后重定向页面?

时间:2016-08-26 12:42:47

标签: spring spring-mvc spring-4

我的ajax成功执行,因为它删除了数据库中的行:

    @Controller
    ...
    @ResponseBody
    @RequestMapping(value = "/ajaxDeleteUser", method = RequestMethod.POST)
    public ModelAndView ajaxDelUser(HttpServletRequest request) {
        int userId = Integer.parseInt(request.getParameter("id"));
        userDao.delete(userId);
        return new ModelAndView("redirect:/");
    }
    ...

在视图中:

<c:url value="/" var="home" scope="request" />
$.ajax({
        data: {"id":data},
        type: "POST",
        url:  "${home}"+"ajaxDeleteUser",
        async: false
});

问题是ajax完成后我看到了列表中的行!那么如何在ajax完成后重定向到列表实际页面?

更新:

这是servlet-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.ambre.hib" />

</beans:beans>

这是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

3 个答案:

答案 0 :(得分:2)

考虑使用“成功”事件:http://api.jquery.com/Ajax_Events/

$.ajax({
        data: {"id":data},
        type: "POST",
        url:  "${home}"+"ajaxDeleteUser",
        async: false,
        success: function(result) {
            location.href = "your_location"
        }
});

答案 1 :(得分:1)

你在这里有两个选择。首先,您可以在ajax完成后使用下面的ajax成功函数重定向前端:

success: function(result) {
            location.href = "your_location"
        }

或者最好你可以使用spring mvc在后端完成这项工作。为了这个目的,删除@Responsebody因为你不需要它(你没有返回任何东西)并且还将返回类型更改为如下所示的字符串。

@RequestMapping(value = "/ajaxDeleteUser", method = RequestMethod.POST)
public String ajaxDelUser(HttpServletRequest request) {
    //your stuff goes here
    ....
    //important line
    return "redirect:/index.html";
}

这里我使用的是index.html,但是将名称更改为首选页面。只要您正确配置了Spring视图解析器,它就可以自行运行。如果您在视图解析器中添加了后缀(例如.jsp),那么您应该只返回没有后缀的页面名称,spring将自动找出页面,如下所示

return "redirect:/index";  //equals index.jsp

答案 2 :(得分:0)

您可以使用此功能。这将在响应到达后重定向。

$.ajax({
    data:{"id":data},
    type: "POST",  
    url:  "${home}"+"ajaxDeleteUser",  
    async: false,  
    success: function(result) {  
        var link = "http://www.sample.com";    
        $(location).attr('href',link);  
    });