gpt4 book ai didi

java - 在没有 in Spring MVC 3. 1 的情况下使用 Flash 范围(和 RedirectAttributes)

转载 作者:搜寻专家 更新时间:2023-11-01 03:27:23 25 4
gpt4 key购买 nike

在我的 Spring MVC 3.1 应用程序中,我想我不能使用“ ”。为什么?因为我想将拦截器应用于“ 所有 映射。所以我不能使用:

<mvc:annotation-driven />

<mvc:resources order="-10" mapping="/public/**" location="/public/" />
<mvc:resources order="-10" mapping="/favicon.ico" location="/public/favicon.ico" />
<mvc:resources order="-10" mapping="/robots.txt" location="/public/robots.txt" />
<mvc:resources order="-10" mapping="/humans.txt" location="/public/humans.txt" />

<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.my.Interceptor" />
</mvc:interceptor>
</mvc:interceptors>

因为我不希望拦截器应用于资源并且没有办法(我认为)指定将拦截器应用于除了这个和那个之外的所有内容的映射路径.

所以我必须添加自己的 RequestMappingHandlerMapping 才能在其上指定拦截器,而不是全局指定拦截器。因为thisthis ,看来我不能在保留 元素的同时简单地定义我自己的 RequestMappingHandlerMapping!

所以……用some help ,我已经能够摆脱 元素,现在几乎一切正常。我的拦截器应用于除我的资源以外的所有内容。一切正常,闪光灯除外!

@RequestMapping(value="/test", method=RequestMethod.POST)
public String test(Model model, RedirectAttributes redirectAttrs)
{
redirectAttrs.addFlashAttribute("myKey", "my message");
return "redirect:test2";
}

@RequestMapping(value="/test2")
public String test2(Model model, HttpServletRequest request)
{
Map<String, ?> map = RequestContextUtils.getInputFlashMap(request); // map is NULL
System.out.println(model.containsAttribute("myKey")); // Prints FALSE
}

Flash map 为 NULL,我的模型不包含我的变量。当我尝试使用 元素时,它运行良好!所以我的问题是:我的上下文中缺少什么来使 flash 示波器工作?

我也确实尝试将“org.springframework.web”设置为 DEBUG 日志记录级别,并且在重定向之后没有任何与 FlashMap 或 FlashMapManager 相关的日志记录。似乎肯定缺少某些必需的 bean。

以下是我的上下文文件中有趣的部分:

<!-- commented! -->
<!-- <mvc:annotation-driven /> -->

<bean id="baseInterceptor" class="com.my.Interceptor" />

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="order" value="0" />
<property name="interceptors">
<list>
<ref bean="conversionServiceExposingInterceptor" />
<ref bean="baseInterceptor" />
</list>
</property>
</bean>

<bean id="myRequestMappingHandlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" />
<property name="validator" ref="validator" />
</bean>
</property>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
</list>
</property>
</bean>

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<bean id="conversionServiceExposingInterceptor" class="org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor">
<constructor-arg ref="conversionService" />
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:/messages/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>

<bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"></bean>

<mvc:resources order="-10" mapping="/public/**" location="/public/" />
<mvc:resources order="-10" mapping="/favicon.ico" location="/public/favicon.ico" />
<mvc:resources order="-10" mapping="/robots.txt" location="/public/robots.txt" />
<mvc:resources order="-10" mapping="/humans.txt" location="/public/humans.txt" />

闪光示波器缺少什么才能工作?

更新:查看我的解决方案答案...实际上什么都没有丢失。只有 Session 没有正常工作,我找到了让它工作的方法。

最佳答案

我终于找到了闪光瞄准镜工作所缺少的东西!

在我访问 flash 变量的操作中(在重定向导致的页面上),我必须使用这个:

public String test2(Model model, HttpServletRequest request, HttpSession session)

而不是这个:

public String test2(Model model, HttpServletRequest request)

这似乎使 Session 正常工作,因此也使 flash 示波器正常工作!为什么?我不知道……

关于java - 在没有 <mvc :annotation-driven/> in Spring MVC 3. 1 的情况下使用 Flash 范围(和 RedirectAttributes),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9960410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com