在 Spring MVC 的配置文件中,可以使用 'RedirectView' 对象来配置重定向的规则。

首先,需要在配置文件中声明 'RedirectView' 的 bean。可以使用以下代码来配置:

<bean id="redirectView" class="org.springframework.web.servlet.view.RedirectView">
    <property name="url" value="redirectUrl" />
    <property name="contextRelative" value="true" />
</bean>

其中,'redirectUrl' 是要重定向的 URL,'contextRelative' 用于指定是否相对于应用程序的上下文进行重定向。

然后,在控制器中,可以使用 'RedirectView' 对象来进行重定向。可以使用以下代码示例:

@Controller
public class MyController {

    @RequestMapping("/redirect")
    public RedirectView redirect() {
        RedirectView redirectView = new RedirectView();
        redirectView.setUrl("redirectUrl");
        redirectView.setContextRelative(true);
        return redirectView;
    }
}

在上面的代码中,'redirectUrl' 是要重定向的 URL,'contextRelative' 用于指定是否相对于应用程序的上下文进行重定向。

另外,还可以使用 'RedirectAttributes' 对象来在重定向过程中传递属性。可以使用以下代码示例:

@Controller
public class MyController {

    @RequestMapping("/redirect")
    public String redirect(RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("attributeName", "attributeValue");
        return "redirect:redirectUrl";
    }
}

在上面的代码中,'addFlashAttribute' 方法用于添加要传递的属性,'redirect:redirectUrl' 指定要重定向的 URL。

以上是在 Spring MVC 的配置文件中正确配置重定向的规则的方法。

Spring MVC 配置文件重定向规则指南

原文地址: https://www.cveoy.top/t/topic/o7FK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录