【发布时间】:2011-02-11 02:38:30
【问题描述】:
我知道 struts2 默认配置会修剪所有从表单获取的字符串。
例如:
我输入
"whatever"在表单中并提交,我会得到
"whatever"字符串已被自动修剪
spring mvc也有这个功能吗?谢了。
【问题讨论】:
标签: spring spring-mvc
我知道 struts2 默认配置会修剪所有从表单获取的字符串。
例如:
我输入
"whatever"在表单中并提交,我会得到
"whatever"字符串已被自动修剪
spring mvc也有这个功能吗?谢了。
【问题讨论】:
标签: spring spring-mvc
使用 Spring 3.2 或更高版本:
@ControllerAdvice
public class ControllerSetup
{
@InitBinder
public void initBinder ( WebDataBinder binder )
{
StringTrimmerEditor stringtrimmer = new StringTrimmerEditor(true);
binder.registerCustomEditor(String.class, stringtrimmer);
}
}
使用 MVC 测试上下文进行测试:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration
public class ControllerSetupTest
{
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup ( )
{
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void stringFormatting ( ) throws Exception
{
MockHttpServletRequestBuilder post = post("/test");
// this should be trimmed, but only start and end of string
post.param("test", " Hallo Welt ");
ResultActions result = mockMvc.perform(post);
result.andExpect(view().name("Hallo Welt"));
}
@Configuration
@EnableWebMvc
static class Config
{
@Bean
TestController testController ( )
{
return new TestController();
}
@Bean
ControllerSetup controllerSetup ( )
{
return new ControllerSetup();
}
}
}
/**
* we are testing trimming of strings with it.
*
* @author janning
*
*/
@Controller
class TestController
{
@RequestMapping("/test")
public String test ( String test )
{
return test;
}
}
并且 - 正如 LppEdd 所要求的 - 它也适用于密码,因为在服务器端输入 [type=password] 和 input[type=text] 之间没有区别
【讨论】:
.setControllerAdvice(...) 显式添加它
注册这个属性编辑器:
org.springframework.beans.propertyeditors.StringTrimmerEditor
AnnotionHandlerAdapter 示例:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
...
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="propertyEditorRegistrar">
<bean class="org.springframework.beans.propertyeditors.StringTrimmerEditor" />
</property>
</bean>
</property>
...
</bean>
【讨论】:
您还可以使用 Spring 的转换服务,它具有使用 <mvc:annotation-driven/> 和 Spring Webflow 的额外好处。与其他答案一样,主要缺点是这是一个全局更改,并且不能针对某些表单禁用。
你需要一个转换器来做修剪
public class StringTrimmingConverter implements Converter<String, String> {
@Override
public String convert(String source) {
return source.trim();
}
}
然后定义一个了解您的转换器的转换服务。
<bean id="applicationConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="mypackage.util.StringTrimmingConverter"/>
</list>
</property>
</bean>
并将其绑定到 mvc。
<mvc:annotation-driven conversion-service="applicationConversionService"/>
如果你使用 Spring Webflow,那么它需要一个包装器
<bean id="defaultConversionService" class="org.springframework.binding.convert.service.DefaultConversionService">
<constructor-arg ref="applicationConversionService"/>
</bean>
以及您的流程构建器上的设置
<flow:flow-builder-services id="flowBuilderServices" conversion-service="defaultConversionService" development="true" validator="validator" />
【讨论】:
您可以使用 Spring-MVC 拦截器
public class TrimInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Enumeration<String> e = request.getParameterNames();
while(e.hasMoreElements()) {
String parameterName = e.nextElement();
request.setParameter(parameterName, request.getParameter(parameterName).trim());
}
return true;
}
并设置您的 HandlerMapping 拦截器属性
<bean id="interceptorTrim" class="br.com.view.interceptor.TrimInterceptor"/>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" p:interceptors-ref="interceptorTrim"/>
}
或者使用 Servlet 过滤器
【讨论】:
只是为了适应Spring Boot定制了上面的代码,如果你想对表单中的某些字段进行显式修剪功能,可以如下所示:
@Component
@ControllerAdvice
public class ControllerSetup {
@InitBinder({"dto", "newUser"})
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
binder.registerCustomEditor(String.class, "userDto.username", new StringTrimmerEditor(false));
binder.registerCustomEditor(String.class, "userDto.password", new DefaultStringEditor(false));
binder.registerCustomEditor(String.class, "passwordConfirm", new DefaultStringEditor(false));
}
}
【讨论】:
首先,修剪requestparam为String,你可以创建一个类并实现WebBingdingInitializer
@ControllerAdvice
public class CustomWebBindingInitializer implements WebBindingInitializer {
@InitBinder
@Override
public void initBinder(WebDataBinder webDataBinder, WebRequest webRequest) {
webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
}
请使用 componentScan 使这个 Class 成为一个 Spring Bean。
但是,我不知道如何修剪 requestBody JSON 数据中的字符串值。
【讨论】: