使用超链接在jsp中对类变量进行类型转换

时间:2014-04-03 16:02:46

标签: jsp casting hyperlink

我正在JSP中创建一个超链接

<c:url var="viewweeklycertificationdetailsinquiry" value="/cwedetailsLink.do">
  <c:param name="forwardName" value="weeklycertificationdetailsinquiry" />
  <c:param name="cweId" value="${WorkSearchDetailsForm.cweID}" />
  <c:param name="claimId" value="${cininqform.claimId}" />
</c:url>

<tr>                                                                 
 <td>
  <html:link href="${viewweeklycertificationdetailsinquiry}">
     <bean:message key="access.cin.inq.monetary.view.weeklycertificationfilingdetails" />   
  </html:link>
 </td>  
</tr>

struts-config.xml中,转发的操作名称为:weeklycertificationdetailsinquiryAction。 在此操作中,使用load()方法

Long cweId = new Long(request.getParameter("cweId"));
Long claimId = new Long(request.getParameter("claimId"));

现在,当我点击超链接时,我得到例外,

java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Long.parseLong(Long.java:431)
    at java.lang.Long.<init>(Long.java:678)
    at gov.state.uim.cin.struts.inq.WeeklyCertificationDetailsInquiryAction.load(WeeklyCertificationDetailsInquiryAction.java:107)
    at gov.state.uim.framework.struts.CustomRequestProcessor.processLoadActionPerform(CustomRequestProcessor.java:681)
    at gov.state.uim.framework.struts.CustomRequestProcessor.processload(CustomRequestProcessor.java:610)
    at gov.state.uim.framework.struts.CustomRequestProcessor.processForwardConfig(CustomRequestProcessor.java:518)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
    at gov.state.uim.framework.struts.CustomActionServlet.process(CustomActionServlet.java:130)
    at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)

我是否必须在JSP中对变量进行类型转换?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:1)

java.lang.NumberFormatException: For input string: "" 
传递空字符串或者传递给Long构造函数的字符串无法解析为long时,将引发

因此,在您的情况下,cweIdclaimId可能为null或为空。更好地使用条件来符合它,改为:

Long cweId = null;
Long claimId = null;

String cweIdStr = request.getParameter("cweId");
String claimIdStr = request.getParameter("cweId");

if(cweIdStr!=null && !cweIdStr.isEmpty())       
    cweId = new Long(cweIdStr);
if(claimIdStr!=null && !claimIdStr.isEmpty())
    claimId = new Long(claimIdStr);

<小时/> 另见:

Long(String s)

ServletRequest#getParameter(String name)