输入中包含空格时输出错误

时间:2015-06-27 15:22:30

标签: java hibernate spring-mvc browser spacing

在浏览器中,我有一个框,我可以在其中输入多个输入,例如PQRS-CD-12345(PQRS-CD默认情况下只有数字12345不断变化),用逗号分隔。当我点击输入它应该说成功提交。

问题是,只有当我输入如下PQRS-CD-12345,PQRS-CD-56478,PQRS-CD-75631(逗号后面没有空格)的值时,我才能看到成功消息。

如果我输入PQRS-CD-12345,PQRS-CD-86452之类的东西,                              PQRS-CD-22551(输入和值之间的空格)然后它给我一条错误信息说:

@PreAuthorize("hasAnyRole('jobdetail_admin')")
    @RequestMapping(value="/hub/showBulkResendPage")
    public ModelAndView showBulkResendPage(HttpServletRequest request, HttpServletResponse response)

    {
        ModelMap map = new ModelMap();
        map.put("maxLimitOrdTextarea", maxLimitOrderTextarea);

        return new ModelAndView("bulkResend", map);
    }

    @PreAuthorize("hasAnyRole('jobdetail_admin')")
    @RequestMapping(value="/hub/bulkResendStatus")
    public ModelAndView bulkResendStatus(HttpServletRequest request, HttpServletResponse response) throws ServletRequestBindingException
    {
         String successView = "redirect:/ipod/hub/searchOrderOrJob";
         ***String[] prtrOrdLocs = ServletRequestUtils.getRequiredStringParameter(request, "prtrOrdLocs").split(",");*** (i added this part commenting the below line)
         // String[] prtrOrdLocs = request.getParameter("prtrOrdLocs").split(","); 

        System.out.println(prtrOrdLocs);
        ModelAndView modelAndView = null;

        ModelMap map = new ModelMap();

        try 
        {
            for(String orderLocator : prtrOrdLocs)
            {
                orderLocator = orderLocator.replaceAll("[\\n\\s]", ""); // i added this line
                orderService.resendStatus(orderLocator);
            }
            map = new ModelMap();
            map.put("success", "resendstatus.hub.success");
            modelAndView = new ModelAndView(successView, map);    
        } 
        catch(IPODException e) 
        { e.printStackTrace();
            modelAndView = handleException(e,"resendstatus.hub.error");         
        } 
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return modelAndView;
    }

我目前的部分代码是

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<head>
<script type="text/javascript">
setupValidateBulkResendTextbox();
</script>
<link rel="stylesheet" type="text/css" href="/css/menuItem-utilities.css" />
</head>
<%@include file="/jsp/layout/submenu-utilities.jsp"%>
<div id="main-content">
<h2>Bulk Resend Status</h2>
<div align="center">
<div align="center" class="errortxt" id="error-message">${usermsg}</div>
<div align="center" id="success-message">${success}</div>
<sec:authorize access="hasAnyRole('jobdetail_admin')" >
<form name="formBulkResendPrtrOrdLocator" id="formBulkResendPrtrOrdLocator" action="/ipod/hub/bulkResendStatus" method="post">
Enter valid, distinct partner order locator IDs separated by commas:
<br /><input type="hidden" name="maxLimitOrdTextarea" value="${maxLimitOrdTextarea}" /> 
<textarea id="prtrOrdLocs" name="prtrOrdLocs" rows="20" cols="40" /></textarea><br />
<div id="errors" class="errortxt"></div>
<input type="submit" value="Bulk Resend" class="inputbutton" />
</form>
</sec:authorize>
</div>
</div>

尽管添加了这些新行,我仍然得到相同的输出。任何人都可以帮助我吗?

&#13;
&#13;
{{1}}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

更改orderLocator = orderLocator.replaceAll("[\\n\\s]", "");

orderLocator = orderLocator.replaceAll("\\s","");

或者你也可以给.replaceAll("\\s+", "");一个机会!

有关删除空格的更多信息here