我的会话对象未被清除

时间:2018-01-19 11:25:41

标签: java spring-boot

在我的控制器中,我想清除我的物体"停车" :

@Controller
@SessionAttributes(value = "parking", types = { Parking.class })
@RequestMapping("/parking")
public class ParkingController {

    /**
     * vue parking.html
     */
    private static final String VIEW_PARKING = "parking";

@RequestMapping(method = RequestMethod.POST, params = { "clearForm" })
public String clearForm(@ModelAttribute("parking") Parking parking,
        final HttpSession httpSession, SessionStatus sessionStatus) {
    sessionStatus.setComplete();    
    return VIEW_PARKING;
}

我已经尝试了sessionStatus.setComplete();,但也尝试了httpSession.invalidate()但是当重新加载页面时,我的对象仍然填充了我以前的条目。

以下是停车内容填写的html代码示例:

<form action="#" th:action="@{/parking}"
    th:object="${parking}" method="post" class="form-horizontal">

<div class="col-sm-offset-2 col-sm-10">
  <button type="submit" class="btn btn-primary btn-lg"
    name="clearForm">Remise à zéro du formulaire</button>
</div>

<input type="text" th:field="*{email.destinataires}"
    class="form-control" />

emailparking对象的属性

在调试模式下,我已经检查过所显示的代码是否已执行。

3 个答案:

答案 0 :(得分:0)

您可能会将@SessionScope@RequestScope混淆。后一页在页面重新加载之间自动失效。

答案 1 :(得分:0)

这是原创

package org.springframework.web.bind.support;

/**
 * Simple interface that can be injected into handler methods, allowing them to
 * signal that their session processing is complete. The handler invoker may
 * then follow up with appropriate cleanup, e.g. of session attributes which
 * have been implicitly created during this handler's processing (according to
 * the
 * {@link org.springframework.web.bind.annotation.SessionAttributes @SessionAttributes}
 * annotation).
 *
 * @author Juergen Hoeller
 * @since 2.5
 * @see org.springframework.web.bind.annotation.RequestMapping
 * @see org.springframework.web.bind.annotation.SessionAttributes
 */
public interface SessionStatus {

    /**
     * Mark the current handler's session processing as complete, allowing for
     * cleanup of session attributes.
     */
    void setComplete();

    /**
     * Return whether the current handler's session processing has been marked
     * as complete.
     */
    boolean isComplete();

}

SessionStatus.setComplete()将当前处理程序的会话处理标记为完成,允许清除会话属性。

SessionStatus.isComplete()返回当前处理程序的会话处理是否已标记为完成。

来源:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/support/SessionStatus.html

你可以这样做

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.context.request.WebRequest;

@Controller
@SessionAttributes(value = "parking", types = {Parking.class})
@RequestMapping("/parking")
public class ParkingController {

    private static final String VIEW_PARKING = "parking";

    @RequestMapping(method = RequestMethod.GET, params = {"clearForm"})
    public String clearForm(@ModelAttribute("parking") Parking parking, WebRequest request, SessionStatus sessionStatus) {
        sessionStatus.setComplete();
        request.removeAttribute("parking", WebRequest.SCOPE_SESSION);
        return VIEW_PARKING;
    }

}

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionAttributeStore;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.context.request.WebRequest;

@Controller
@SessionAttributes(value = "parking", types = {Parking.class})
@RequestMapping("/parking")
public class ParkingController {

    private static final String VIEW_PARKING = "parking";

    @RequestMapping(method = RequestMethod.GET, params = {"clearForm"})
    public String clearForm(@ModelAttribute("parking") Parking parking, WebRequest request, SessionAttributeStore store, SessionStatus sessionStatus) {
        sessionStatus.setComplete();
        // store = ...
        store.cleanupAttribute(request, "parking");
        return VIEW_PARKING;
    }

}

答案 2 :(得分:0)

Nhu Vy解决方案几乎是好的,我只需添加一个get重定向,以便解决方案正常工作:

app.component.html
相关问题