栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring MVC可以修剪从表单获取的所有字符串吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring MVC可以修剪从表单获取的所有字符串吗?

使用Spring 3.2或更高版本:

@ControllerAdvicepublic class ControllerSetup{    @InitBinder    public void initBinder ( WebDataBinder binder )    {        StringTrimmerEditor stringtrimmer = new StringTrimmerEditor(true);        binder.registerCustomEditor(String.class, stringtrimmer);    }}

使用MVC测试环境进行测试:

@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfigurationpublic 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();        }    }}@Controllerclass TestController{    @RequestMapping("/test")    public String test ( String test )    {        return test;    }}

而且-正如LppEdd所要求的-它也可以使用密码,因为在服务器端,input [type = password]和input [type =
text]之间没有区别



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/615691.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号