Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to remove the redirect response header from ExceptionHandler? #159

Open
KonngExplorer opened this issue Dec 24, 2024 · 1 comment
Open

Comments

@KonngExplorer
Copy link

KonngExplorer commented Dec 24, 2024

@Controller
@RequestMapping("/htmx/admin/user/")
public class HxAuthController {
    @HxRequest
    @RequestMapping("doLogin")
    @HxRedirect("/admin/document/article/article-table")
    public String doLogin(@RequestBody LoginRequest request) {
        String password = request.getPassword();
        String username = request.getUsername();

        if (StrUtil.isBlank(email) && StrUtil.isBlank(username)) {
            throw new LoginException(USERNAME_INCORRECT, "Account or password error",);
        }

        if (StrUtil.isBlank(password)) {
            throw new LoginException(PASSWORD_INCORRECT, "Account or password error");
        }

        boolean success = authService.login(username , password);

        if (!success) {
            throw new LoginException(INTERNAL_ERR, "login failed", false);
        }

        return "none";
    }
}
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler({LoginException.class})
    @ResponseBody
    public Object handleException(LoginException e, HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView();

        if (request.getHeader(HtmxRequestHeader.HX_REQUEST.getValue()) != null) {
            modelAndView.addObject("msg", e.getMessage());
            response.addHeader(HtmxResponseHeader.HX_RESWAP.getValue(), HxSwapType.BEFORE_END.getValue());
            response.addHeader(HtmxResponseHeader.HX_RESELECT.getValue(), "#alert-warning");
            response.addHeader(HtmxResponseHeader.HX_RETARGET.getValue(), "body");
            modelAndView.setViewName("admin/component/alert-warning");
            return modelAndView;
        }
        return RestResult.failure(e.getMessage());
    }
}

According to the above code, I set the redirect url for successful login using HxRedirect annotation in Controller, I handle the login exception in Handler method of exception handling class, when the exception occurs I want to prompt instead of redirect, after testing whether I set HtmxResponseHeader.HX_REDIRECT in Handler exception handling method or not, it will redirect to "/htmx/admin/user/", is there something wrong with me?

@KonngExplorer
Copy link
Author

KonngExplorer commented Dec 24, 2024

By the way, the version of the dependency I use is as follows

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependency>
            <groupId>io.github.wimdeblauwe</groupId>
            <artifactId>htmx-spring-boot</artifactId>
            <version>4.0.1</version>
    </dependency>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant